0

I made a post and get mapping inside my java controller layer. The post mapping gathers data such as the starting date, end date, and number of Guests from the front end.

Inside the service layer, the first function filters the available apartments. However, I want to make a get mapping which can access the list of available apartments so this can be displayed in the front end. I tried this in the service layer with the second function. Unfortunately, this function only shows the entire apartment list. Does anybody know how I can get a function that can access the list from the first function so that I can display the filtered available apartments?

Apartment Controller Layer

 @PostMapping(value = "api/availableapartments")
    public List<Apartment> getAvailableApartments(@RequestBody String request)throws JSONException, ParseException {
        JSONObject jsonObject = new JSONObject(request);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = format.parse(String.valueOf(jsonObject.getString("startDate")));
        Date endDate = format.parse(String.valueOf(jsonObject.getString("endDate")));
        Integer numberOfBeds =  Integer.parseInt(String.valueOf(jsonObject.getString("numberOfBeds")));
        return apartmentService.getAvailableApartments(numberOfBeds, startDate, endDate);
    }

    @GetMapping(value = "api/availableapartments")
    public List<Apartment> getAvailableApartments(){
        return apartmentService.getAllAvailableApartments();
    }


Apartment service layer

public class ApartmentService  {

 public List<Apartment> getAvailableApartments(Integer requestedBeds, Date startDate, Date endDate){

        List<Apartment> allApartments = apartmentRepository.findAll();

        List<Apartment> availableApartments = new ArrayList<>();
            //going through all apartments
            for(Apartment apartment : allApartments){


                if(apartment.getAvailableBeds() >= requestedBeds){
                    if(reservationService.checkAvailability(apartment, startDate, endDate)){
                        availableApartments.add(apartment);
                    }

                }

            }
            return availableApartments;
    }

    public List<Apartment> getAllAvailableApartments(){
        List<Apartment> allApartments = apartmentRepository.findAll();


        List<Apartment> allAvailableApartments = new ArrayList<>();

        for(Apartment apartments : allApartments){
            System.out.println(apartments.getApartmentId());
            allAvailableApartments.add(apartments);


        }

        return allAvailableApartments;

    }
}

```
lara-div
  • 5
  • 3

1 Answers1

0

Not sure I understood the question exactly, but in this scenario it's better not to use a POST request just to POST an object containing some search parameters.

Instead of the POST you should use a GET with parameters for the filtering. That way you do the filtering in the GET request and don't need to "access the POST" request.

Have a look here

Vlad L
  • 1,544
  • 3
  • 6
  • 20
  • However, if I want to send data to the backend such as start date and end date I need a post request right? The main goal for me is to send data with the post request and getting the data to the front end with the get request. Or is there another solution? – lara-div Dec 25 '20 at 14:14
  • Dates and number of beds is not really data, they are filters for your search query, right? So you can do GET availableapartments?beds=2&from=2020-12-01&to=2020-12-20. REST is not meant to have any state, but of course you could model it like the search parameters data is a resource, so you POST that and it gets written to the database along with user ID. Then your GET will look at database entries before returning the search result. – Vlad L Dec 25 '20 at 17:28