This commit is contained in:
@@ -1,6 +1,24 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.dto.BookingDTO;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.exception.CodeIsNotExistException;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +26,54 @@ import com.example.nto.service.BookingService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final BookingRepository bookingRepository;
|
||||
private final PlaceRepository placeRepository;
|
||||
|
||||
@Override
|
||||
public Map<String, List<BookingDTO>> getAvailablePlaces(String code) {
|
||||
Optional<Employee> optionalEmployee = employeeRepository.findByCode(code);
|
||||
|
||||
if (optionalEmployee.isEmpty()) {
|
||||
throw new CodeIsNotExistException("кода не существует");
|
||||
}
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
List<LocalDate> dates = IntStream.range(0, 4)
|
||||
.mapToObj(today::plusDays)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<String, List<BookingDTO>> result = new HashMap<>();
|
||||
|
||||
for (LocalDate date : dates) {
|
||||
String dateKey = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
|
||||
// Все забронированные placeId на эту дату
|
||||
List<Long> bookedPlaceIds = bookingRepository.findPlaceIdsByDate(date);
|
||||
|
||||
// Все доступные места (не забронированные)
|
||||
List<Place> allPlaces = placeRepository.findAll();
|
||||
List<Place> availablePlaces = allPlaces.stream()
|
||||
.filter(place -> !bookedPlaceIds.contains(place.getId()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Преобразуем в BookingSummary
|
||||
List<BookingDTO> dto = availablePlaces.stream()
|
||||
.map(place -> {
|
||||
BookingDTO bookingDTO = new BookingDTO();
|
||||
bookingDTO.setId(place.getId());
|
||||
bookingDTO.setPlace(place.getPlace());
|
||||
return bookingDTO;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
result.put(dateKey, dto);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user