Files
NTO-2025-Backend-TeamTask/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java
v3less11 d530e32e44
Some checks failed
Android Test / validate-and-test (pull_request) Has been cancelled
changes
2025-12-08 21:20:18 +03:00

80 lines
2.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@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;
}
}