This commit is contained in:
Yurchik-gitter
2025-12-11 23:30:05 +03:00
parent d703db0388
commit 2c2a8981ff
2 changed files with 15 additions and 30 deletions

View File

@@ -32,11 +32,11 @@ public class BookingController {
@GetMapping("/booking") @GetMapping("/booking")
public ResponseEntity<?> getFree(@PathVariable String code) { public ResponseEntity<?> getFree(@PathVariable String code) {
if (!isValidCode(code)) { if (!isValidCode(code)) {
return ResponseEntity.status(400).body("Bad request"); return ResponseEntity.status(400).build(); // Статус 400
} }
if (employeeRepository.findByCode(code).isEmpty()) { if (employeeRepository.findByCode(code).isEmpty()) {
return ResponseEntity.status(401).body("Unauthorized"); return ResponseEntity.status(401).build(); // Статус 401
} }
Map<LocalDate, List<Place>> free = service.getAvailablePlacesForRange(LocalDate.now(), 4); Map<LocalDate, List<Place>> free = service.getAvailablePlacesForRange(LocalDate.now(), 4);
@@ -49,38 +49,29 @@ public class BookingController {
.collect(Collectors.toList()) .collect(Collectors.toList())
)); ));
return ResponseEntity.ok(dto); return ResponseEntity.ok(dto); // Статус 200
} }
@PostMapping("/book") @PostMapping("/book")
public ResponseEntity<?> book(@PathVariable String code, public ResponseEntity<?> book(@PathVariable String code,
@RequestBody BookingRequestDto dto) { @RequestBody BookingRequestDto dto) {
if (!isValidCode(code)) { if (!isValidCode(code)) {
return ResponseEntity.status(400).body("Bad request"); return ResponseEntity.status(400).build(); // Статус 400
} }
if (employeeRepository.findByCode(code).isEmpty()) { if (employeeRepository.findByCode(code).isEmpty()) {
return ResponseEntity.status(401).body("Unauthorized"); return ResponseEntity.status(401).build(); // Статус 401
} }
try { try {
service.createBooking(code, LocalDate.parse(dto.getDate()), dto.getPlaceId()); service.createBooking(code, LocalDate.parse(dto.getDate()), dto.getPlaceId());
return ResponseEntity.status(201).body("Booking created"); return ResponseEntity.status(201).build(); // Статус 201 для успешного бронирования
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
String msg = ex.getMessage(); return ResponseEntity.status(400).build(); // Ошибка для неверных данных (400)
if ("Place not found".equals(msg)) {
return ResponseEntity.status(400).body("Bad request");
}
return ResponseEntity.status(400).body("Bad request");
} catch (IllegalStateException ex) { } catch (IllegalStateException ex) {
return ResponseEntity.status(409).body("Conflict"); return ResponseEntity.status(409).build(); // Ошибка, если место уже забронировано (409)
} catch (Exception ex) { } catch (Exception ex) {
return ResponseEntity.status(400).body("Bad request"); return ResponseEntity.status(400).build(); // Общая ошибка для других случаев (400)
} }
} }
@ExceptionHandler({org.springframework.http.converter.HttpMessageNotReadableException.class})
public ResponseEntity<String> handleBadRequest() {
return ResponseEntity.status(400).body("Bad request");
}
} }

View File

@@ -7,7 +7,6 @@ import com.example.nto.entity.Employee;
import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.EmployeeRepository;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.http.converter.HttpMessageNotReadableException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@@ -29,26 +28,26 @@ public class EmployeeController {
@GetMapping("/auth") @GetMapping("/auth")
public ResponseEntity<?> auth(@PathVariable String code) { public ResponseEntity<?> auth(@PathVariable String code) {
if (!isCodeFormatValid(code)) { if (!isCodeFormatValid(code)) {
return ResponseEntity.status(400).body("Bad request"); return ResponseEntity.status(400).build(); // Статус 400
} }
boolean exists = employeeRepository.findByCode(code).isPresent(); boolean exists = employeeRepository.findByCode(code).isPresent();
if (!exists) { if (!exists) {
return ResponseEntity.status(401).body("Unauthorized"); return ResponseEntity.status(401).build(); // Статус 401 для несуществующего сотрудника
} }
return ResponseEntity.ok().build(); return ResponseEntity.ok().build(); // Статус 200 для успешной авторизации
} }
@GetMapping("/info") @GetMapping("/info")
public ResponseEntity<?> info(@PathVariable String code) { public ResponseEntity<?> info(@PathVariable String code) {
if (!isCodeFormatValid(code)) { if (!isCodeFormatValid(code)) {
return ResponseEntity.status(400).body("Bad request"); return ResponseEntity.status(400).build(); // Статус 400
} }
Employee employee = employeeRepository.findByCode(code).orElse(null); Employee employee = employeeRepository.findByCode(code).orElse(null);
if (employee == null) { if (employee == null) {
return ResponseEntity.status(401).body("Unauthorized"); return ResponseEntity.status(401).build(); // Статус 401 для несуществующего сотрудника
} }
Map<String, BookingInfoDto> bookingMap = new LinkedHashMap<>(); Map<String, BookingInfoDto> bookingMap = new LinkedHashMap<>();
@@ -65,11 +64,6 @@ public class EmployeeController {
bookingMap bookingMap
); );
return ResponseEntity.ok(dto); return ResponseEntity.ok(dto); // Статус 200 для успешной обработки
}
@ExceptionHandler({IllegalArgumentException.class, HttpMessageNotReadableException.class})
public ResponseEntity<String> handleBadRequest() {
return ResponseEntity.status(400).body("Bad request");
} }
} }