forked from Olympic/NTO-2025-Backend-TeamTask
62 lines
2.2 KiB
Java
62 lines
2.2 KiB
Java
package com.example.nto.controller;
|
|
|
|
import com.example.nto.service.BookingService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Map;
|
|
/**
|
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
|
* =================================
|
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/{code}")
|
|
public class BookingController {
|
|
|
|
@Autowired
|
|
private BookingService bookingService;
|
|
|
|
@GetMapping("/booking")
|
|
public ResponseEntity<?> getAvailablePlaces(@PathVariable String code) {
|
|
try {
|
|
if (!bookingService.employeeExists(code)) {
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
}
|
|
|
|
Map<String, Object> availablePlaces = bookingService.getAvailablePlaces();
|
|
return ResponseEntity.ok(availablePlaces);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
}
|
|
}
|
|
|
|
@PostMapping("/book")
|
|
public ResponseEntity<Void> bookPlace(@PathVariable String code, @RequestBody Map<String, Object> request) {
|
|
try {
|
|
if (!bookingService.employeeExists(code)) {
|
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
}
|
|
|
|
String date = (String) request.get("date");
|
|
Integer placeId = (Integer) request.get("placeId");
|
|
|
|
if (date == null || placeId == null) {
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
}
|
|
|
|
boolean success = bookingService.createBooking(code, date, placeId);
|
|
if (!success) {
|
|
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
|
}
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
}
|
|
}
|
|
}
|