forked from Olympic/NTO-2025-Backend-TeamTask
Задания Backend нто
This commit is contained in:
@@ -1,10 +1,133 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import com.example.nto.service.PlaceService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/{code}")
|
||||
public class BookingController {
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
private final BookingService bookingService;
|
||||
private final PlaceService placeService;
|
||||
|
||||
public BookingController(EmployeeService employeeService, BookingService bookingService, PlaceService placeService) {
|
||||
this.employeeService = employeeService;
|
||||
this.bookingService = bookingService;
|
||||
this.placeService = placeService;
|
||||
}
|
||||
|
||||
@GetMapping("/auth")
|
||||
public ResponseEntity<Void> auth(@PathVariable String code) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isPresent()) {
|
||||
return ResponseEntity.ok().build();
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@GetMapping("/info")
|
||||
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
|
||||
Employee employee = employeeOpt.get();
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("name", employee.getName());
|
||||
response.put("photoUrl", employee.getPhotoUrl());
|
||||
|
||||
Map<String, List<Map<String, Object>>> bookingsMap = new HashMap<>();
|
||||
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
|
||||
|
||||
for (Booking b : bookingList) {
|
||||
String dateKey = b.getDate().toString();
|
||||
Map<String, Object> bookingData = new HashMap<>();
|
||||
bookingData.put("id", b.getId());
|
||||
bookingData.put("place", b.getPlace().getPlace());
|
||||
|
||||
bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
|
||||
}
|
||||
|
||||
response.put("booking", bookingsMap);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@GetMapping("/booking")
|
||||
public ResponseEntity<Map<String, List<Map<String, Object>>>> availableBooking(@PathVariable String code) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
|
||||
Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();
|
||||
LocalDate today = LocalDate.now();
|
||||
List<Place> allPlaces = placeService.getAllPlaces();
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
LocalDate date = today.plusDays(i);
|
||||
List<Booking> bookings = bookingService.getBookingsByDate(date);
|
||||
|
||||
Set<Long> bookedPlaceIds = bookings.stream()
|
||||
.map(b -> b.getPlace().getId())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<Map<String, Object>> available = allPlaces.stream()
|
||||
.filter(p -> !bookedPlaceIds.contains(p.getId()))
|
||||
.map(p -> {
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("id", p.getId());
|
||||
m.put("place", p.getPlace());
|
||||
return m;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
result.put(date.toString(), available);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/book")
|
||||
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
Employee employee = employeeOpt.get();
|
||||
|
||||
try {
|
||||
String dateStr = body.get("date");
|
||||
String placeIdStr = body.get("placeID");
|
||||
if (dateStr == null || placeIdStr == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
LocalDate date = LocalDate.parse(dateStr);
|
||||
Long placeId = Long.parseLong(placeIdStr);
|
||||
Place place = placeService.getPlaceById(placeId);
|
||||
|
||||
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
||||
}
|
||||
|
||||
bookingService.createBooking(employee, place, date);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user