I want to add REST endpoint that will add reservation for some room in hotel. What is the better way to do it. I have 2 solution:
@RequestMapping("api/v1/hotels/hotelId/rooms/roomId/reservations")
some Class Endpoint
@PostMapping
public ReservationApi save(final @RequestBody ReservationApi reservationApi,
final @PathVariable("hotelId") String hotelId,
final @PathVariable("roomId") String roomId) {
final Reservation reservation = reservationMapper.toDomain(reservationApi);
return reservationMapper.toApi(reservationService.save(reservation, hotelId, roomId));
}
and build Reservation on service side
or I have alternative solution where we do not use paths and only API class for creating
@RequestMapping("api/v1/reservations")
@PostMapping
public ReservationApi save(final @RequestBody ReservationApi reservationApi) {
final Reservation reservation = reservationMapper.toDomain(reservationApi);
return reservationMapper.toApi(reservationService.save(reservation));
}
CodePudding user response:
The suggestions in the comments are for sure helpful. I would suggest to go with calling the calling the service class for the implementation. You can autowire the service class inside the controller class and then implement the functionality. Additionally, I can suggest that you can use ResponseEntity object for sending the response.
CodePudding user response:
I think it depends on the structure of the database after all, although following pure REST rules, I would choose option 1, why? because if I want to review the booking list for a room later, I use the same endpoint as I used to create a booking for that room.
In addition, we do not know what is in the ReservationApi - do we also provide the hotel id and room id there, or only the information that is needed to create a reservation?
I agree with the previous comments that it's worth creating a ReservationResponse
if I had to create such an api, I would choose option 1