简体   繁体   中英

How to access list inside another function in the same class Java

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;

    }
}

```

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM