Files
NTO-2025-Backend-TeamTask/src/main/java/com/example/nto/controller/BookingController.java
2025-11-30 19:49:37 +07:00

60 lines
2.1 KiB
Java

package com.example.nto.controller;
import com.example.nto.entity.Place;
import com.example.nto.service.BookingService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequestMapping("/api/{code}")
@RequiredArgsConstructor
public class BookingController {
private final BookingService bookingService;
@GetMapping("/booking")
public ResponseEntity<?> getAvailableBooking(@PathVariable String code) {
Map<LocalDate, List<Place>> places = bookingService.getAvailablePlaces(code);
Map<String, List<Map<String, Object>>> result = new TreeMap<>();
for (Map.Entry<LocalDate, List<Place>> e : places.entrySet()) {
List<Map<String, Object>> placeList = e.getValue().stream().map(p -> {
Map<String, Object> m = new HashMap<>();
m.put("id", p.getId());
m.put("place", p.getPlaceName());
return m;
}).toList();
result.put(e.getKey().toString(), placeList);
}
return ResponseEntity.ok(result);
}
@PostMapping("/book")
public ResponseEntity<?> createBooking(@PathVariable String code, @RequestBody Map<String, Object> request) {
// try {
String dateStr = (String) request.get("date");
Long placeID = Long.valueOf(String.valueOf(request.get("placeID")));
LocalDate date = LocalDate.parse(dateStr);
bookingService.createBooking(code, date, placeID);
return ResponseEntity.status(201).build();
// }
// catch (Exception ex) {
// return ResponseEntity.badRequest().body("Что-то пошло не так");
// }
}
}