main #9
@@ -2,9 +2,13 @@ package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.service.BookingService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -12,9 +16,22 @@ import java.time.LocalDate;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
//@RestController
|
||||
//@RequestMapping("/api")
|
||||
//public class BookingController {
|
||||
//
|
||||
//
|
||||
//}
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class BookingController {
|
||||
|
||||
private final BookingService bookingService;
|
||||
|
||||
@Autowired
|
||||
public BookingController(BookingService bookingService) {
|
||||
this.bookingService = bookingService;
|
||||
}
|
||||
|
||||
@GetMapping("/{code}/booking")
|
||||
public ResponseEntity<Map<String, List<Booking.AvailablePlaceDto>>> getAvailablePlaces(
|
||||
@PathVariable String code
|
||||
) {
|
||||
Map<String, List<Booking.AvailablePlaceDto>> response = bookingService.getAvailablePlaces(code);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,4 +37,12 @@ public class Booking {
|
||||
@JoinColumn(name = "employee_id")
|
||||
@JsonBackReference
|
||||
private Employee employee;
|
||||
|
||||
|
||||
|
||||
@Data
|
||||
public static class AvailablePlaceDto {
|
||||
private long id;
|
||||
private String place;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -18,7 +19,7 @@ import java.util.Optional;
|
||||
@Service
|
||||
@Repository
|
||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||
Optional<Booking> findById(long id);
|
||||
List<Booking> findByDateAndEmployeeByCode(List<LocalDate> dates, String code);
|
||||
|
||||
List<Booking> findByEmployee_Code(String code);
|
||||
Optional<Booking> findById(String code);
|
||||
}
|
||||
|
||||
@@ -13,5 +13,4 @@ import java.util.Optional;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
||||
Optional<Place> findPlaceById(long id);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ import java.util.Optional;
|
||||
*/
|
||||
public interface BookingService {
|
||||
List<Booking> getAll();
|
||||
Optional<Booking> getById(long id);
|
||||
Optional<Booking> getBookingById(String code);
|
||||
|
||||
Map<String, List<Booking.AvailablePlaceDto>> getAvailablePlaces(String code);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.repository.PlaceRepository;
|
||||
import com.example.nto.service.BookingService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -20,10 +26,8 @@ import java.util.Optional;
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
|
||||
private final BookingRepository bookingRepository;
|
||||
|
||||
public BookingServiceImpl(BookingRepository bookingRepository) {
|
||||
this.bookingRepository = bookingRepository;
|
||||
}
|
||||
private final PlaceRepository placeRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
@Override
|
||||
public List<Booking> getAll() {
|
||||
@@ -31,8 +35,72 @@ public class BookingServiceImpl implements BookingService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Booking> getById(long id) {
|
||||
return bookingRepository.findById(id);
|
||||
public Optional<Booking> getBookingById(String code) {
|
||||
return bookingRepository.findById(code);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public BookingServiceImpl(BookingRepository bookingRepository, PlaceRepository placeRepository,
|
||||
EmployeeRepository employeeRepository) {
|
||||
this.bookingRepository = bookingRepository;
|
||||
this.placeRepository = placeRepository;
|
||||
this.employeeRepository = employeeRepository;
|
||||
}
|
||||
public Map<String, List<Booking.AvailablePlaceDto>> getAvailablePlaces(String employeeCode) {
|
||||
// 1. Получаем сотрудника (для контекста)
|
||||
Employee employee = employeeRepository.findByCode(employeeCode)
|
||||
.orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
|
||||
// 2. Определяем диапазон дат: сегодня + 3 дня
|
||||
LocalDate today = LocalDate.now();
|
||||
List<LocalDate> targetDates = List.of(
|
||||
today,
|
||||
today.plusDays(1),
|
||||
today.plusDays(2),
|
||||
today.plusDays(3)
|
||||
);
|
||||
|
||||
// 3. Получаем все места из БД
|
||||
List<Place> allPlaces = placeRepository.findAll();
|
||||
|
||||
// 4. Находим все бронирования на целевые даты
|
||||
List<Booking> bookings = bookingRepository.findByDateAndEmployeeByCode(targetDates, employeeCode);
|
||||
|
||||
// 5. Группируем бронирования по датам
|
||||
Map<LocalDate, List<Booking>> bookingsByDate = bookings.stream()
|
||||
.collect(Collectors.groupingBy(Booking::getDate));
|
||||
|
||||
// 6. Формируем итоговый ответ
|
||||
Map<String, List<Booking.AvailablePlaceDto>> result = new HashMap<>();
|
||||
|
||||
for (LocalDate date : targetDates) {
|
||||
String dateKey = date.toString(); // Формат: "yyyy-MM-dd"
|
||||
|
||||
// Берём бронирования на эту дату
|
||||
List<Booking> dayBookings = bookingsByDate.getOrDefault(date, List.of());
|
||||
|
||||
// Собираем ID занятых мест
|
||||
Set<Long> occupiedPlaceIds = dayBookings.stream()
|
||||
.map(booking -> booking.getPlace().getId())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// Фильтруем свободные места
|
||||
List<Booking.AvailablePlaceDto> available = allPlaces.stream()
|
||||
.filter(place -> !occupiedPlaceIds.contains(place.getId()))
|
||||
.map(place -> {
|
||||
Booking.AvailablePlaceDto dto = new Booking.AvailablePlaceDto();
|
||||
dto.setId(place.getId());
|
||||
dto.setPlace(place.getPlace());
|
||||
return dto;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
result.put(dateKey, available);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user