Problem with creating new objects on each RestController iterate spring boot

Viewed 16

My problem is that on each post method my Restcontroller creates new objects and I can't work on the first one that

@RestController
public class ControllerRoom {

    private final Room cinema;

    public ControllerRoom() {
        this.cinema = new Room(9,9);
    }

My main goal is to avoid it and check with post method if some of the json object was requested twice, if yes then exception will be thrown, but since it creates over and over, it can't throw a exception

@PostMapping("/purchase")
    public ResponseEntity<Seats> sellSeat(@RequestBody Seats seat){
        System.out.println("Requested body =" + seat.getRow() +" "+ seat.getColumn() + " " + seat.getPrice() );
        for (Seats s : cinema.getSeats()){
            if (s.getColumn() == seat.getColumn() && s.getRow() == seat.getRow()){
                if (!s.isTaken()) {
                    System.out.println(s.setTaken(true));
                    return new ResponseEntity<>(s, HttpStatus.OK);
                } if (s.isTaken()){
                    throw new SeatRequestException("The ticket has been already purchased!");
                }
            }
        }
        throw new SeatRequestException("The number of a row or a column is out of bounds!");
    }

In case if it's a problem, Room class add the objects to the ArrayList in this method of it class

@JsonGetter(value = "available_seats")
    public ArrayList<Seats> getSeats() {
        for (int i=1; i<=total_columns; i++){
            for (int j=1; j<=total_rows; j++){
                if (i<=4) {
                    available_seats.add(new Seats(i, j, false, 10));
                } else {
                    available_seats.add(new Seats(i,j, false,8));
                }
            }
        }
        System.out.println(available_seats.size());
        return available_seats;
    }
0 Answers
Related