forked from Olympic/NTO-2025-Backend-TeamTask
Win
This commit is contained in:
@@ -1,12 +1,64 @@
|
||||
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.excepation.EmployeeNotFoundException;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.PlaceRepository;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
|
||||
@Autowired
|
||||
private BookingRepository bookingRepository;
|
||||
|
||||
@Autowired
|
||||
private PlaceRepository placeRepository;
|
||||
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> findAvailableBookings(String employeeCode) {
|
||||
if (employeeCode == null || employeeCode.isEmpty()) {
|
||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
||||
}
|
||||
Employee employee = employeeService.getEmployeeWithBookings(employeeCode);
|
||||
if (employee == null) {
|
||||
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
||||
}
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate endDate = today.plusDays(3);
|
||||
List<Booking> bookings = bookingRepository.findByDateBetween(today, endDate);
|
||||
List<Place> places = placeRepository.findAll();
|
||||
Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
LocalDate date = today.plusDays(i);
|
||||
List<Map<String, Object>> freePlaces = new ArrayList<>();
|
||||
for (Place place : places) {
|
||||
boolean isBooked = bookings.stream()
|
||||
.anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId());
|
||||
if (!isBooked) {
|
||||
Map<String, Object> placeInfo = new LinkedHashMap<>();
|
||||
placeInfo.put("id", place.getId());
|
||||
placeInfo.put("place", place.getPlace());
|
||||
freePlaces.add(placeInfo);
|
||||
}
|
||||
}
|
||||
result.put(date.toString(), freePlaces);
|
||||
}
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user