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")
public ResponseEntity<?> getFree(@PathVariable String code) {
if (!isValidCode(code)) {
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.status(400).build(); // Статус 400
}
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);
@@ -49,38 +49,29 @@ public class BookingController {
.collect(Collectors.toList())
));
return ResponseEntity.ok(dto);
return ResponseEntity.ok(dto); // Статус 200
}
@PostMapping("/book")
public ResponseEntity<?> book(@PathVariable String code,
@RequestBody BookingRequestDto dto) {
if (!isValidCode(code)) {
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.status(400).build(); // Статус 400
}
if (employeeRepository.findByCode(code).isEmpty()) {
return ResponseEntity.status(401).body("Unauthorized");
return ResponseEntity.status(401).build(); // Статус 401
}
try {
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) {
String msg = ex.getMessage();
if ("Place not found".equals(msg)) {
return ResponseEntity.status(400).body("Bad request");
}
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.status(400).build(); // Ошибка для неверных данных (400)
} catch (IllegalStateException ex) {
return ResponseEntity.status(409).body("Conflict");
return ResponseEntity.status(409).build(); // Ошибка, если место уже забронировано (409)
} 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 org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.converter.HttpMessageNotReadableException;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -29,26 +28,26 @@ public class EmployeeController {
@GetMapping("/auth")
public ResponseEntity<?> auth(@PathVariable String code) {
if (!isCodeFormatValid(code)) {
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.status(400).build(); // Статус 400
}
boolean exists = employeeRepository.findByCode(code).isPresent();
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")
public ResponseEntity<?> info(@PathVariable String code) {
if (!isCodeFormatValid(code)) {
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.status(400).build(); // Статус 400
}
Employee employee = employeeRepository.findByCode(code).orElse(null);
if (employee == null) {
return ResponseEntity.status(401).body("Unauthorized");
return ResponseEntity.status(401).build(); // Статус 401 для несуществующего сотрудника
}
Map<String, BookingInfoDto> bookingMap = new LinkedHashMap<>();
@@ -65,11 +64,6 @@ public class EmployeeController {
bookingMap
);
return ResponseEntity.ok(dto);
}
@ExceptionHandler({IllegalArgumentException.class, HttpMessageNotReadableException.class})
public ResponseEntity<String> handleBadRequest() {
return ResponseEntity.status(400).body("Bad request");
return ResponseEntity.ok(dto); // Статус 200 для успешной обработки
}
}