forked from Olympic/NTO-2025-Backend-TeamTask
116 lines
5.0 KiB
Java
116 lines
5.0 KiB
Java
package com.example.nto.service.impl;
|
|
|
|
import com.example.nto.entity.*;
|
|
import com.example.nto.repository.BookingRepository;
|
|
import com.example.nto.repository.EmployeeRepository;
|
|
import com.example.nto.repository.PlaceRepository;
|
|
import com.example.nto.service.EmployeeService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
|
* =================================
|
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
|
*/
|
|
|
|
@Service
|
|
public class EmployeeServiceImpl implements EmployeeService {
|
|
private final EmployeeRepository employeeRepository;
|
|
private final BookingRepository bookingRepository;
|
|
private final PlaceRepository placeRepository;
|
|
|
|
@Autowired
|
|
public EmployeeServiceImpl(EmployeeRepository employeeRepositoryToCath, PlaceRepository placeRepositoryToCatch, BookingRepository bookingRepositoryToCatch) {
|
|
employeeRepository = employeeRepositoryToCath;
|
|
placeRepository = placeRepositoryToCatch;
|
|
bookingRepository = bookingRepositoryToCatch;
|
|
}
|
|
|
|
@Override
|
|
public boolean CheckAuthorization(String code) {
|
|
List<Employee> allEmployees = employeeRepository.findAll();
|
|
for (int i = 0; i < allEmployees.size(); i++) {
|
|
if (allEmployees.get(i).getCode().equals(code))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public EmployeeInfo GetEmployee(String code) {
|
|
List<Employee> allEmployees = employeeRepository.findAll();
|
|
for (int i = 0; i < allEmployees.size(); i++) {
|
|
if (allEmployees.get(i).getCode().equals(code)) {
|
|
Employee selectedEmployee = allEmployees.get(i);
|
|
Map<String, Place> bookings = GetAllBookings(code);
|
|
return new EmployeeInfo(selectedEmployee.getName(), selectedEmployee.getPhotoUrl(), bookings);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public Map<String, Place> GetAllBookings(String code) {
|
|
List<Booking> allBookings = bookingRepository.findAll();
|
|
Map<String, Place> formatedBookings = new HashMap<>();
|
|
for (int i = 0; i < allBookings.size(); i++) {
|
|
if (allBookings.get(i).getEmployee().getCode().equals(code)) {
|
|
Place bookedPlace = allBookings.get(i).getPlace();
|
|
formatedBookings.put(allBookings.get(i).getDate().toString(), new Place(bookedPlace.getId(), bookedPlace.getPlace()));
|
|
}
|
|
}
|
|
return formatedBookings;
|
|
}
|
|
|
|
@Override
|
|
public Map<String, List<Place>> GetFreeBookings(String code) {
|
|
if (!CheckAuthorization(code))
|
|
return null;
|
|
|
|
Map<String, List<Place>> freeBookings = new HashMap<>();
|
|
List<Place> allPlaces = placeRepository.findAll();
|
|
List<Booking> allBookings = bookingRepository.findAll();
|
|
LocalDate today = LocalDate.now();
|
|
for (int i = 0; i < 4; i++) {
|
|
LocalDate currentDate = today.plusDays(i);
|
|
List<Place> currentDateFreePlaces = new ArrayList<>();
|
|
boolean[] placeBooked = new boolean[allPlaces.size()];
|
|
for (int j = 0; j < allBookings.size(); j++) {
|
|
if (allBookings.get(j).getDate().toString().equals(currentDate.toString()))
|
|
placeBooked[Math.toIntExact(allBookings.get(j).getPlace().getId() - 1)] = true;
|
|
}
|
|
for (int j = 0; j < allPlaces.size(); j++) {
|
|
if (!placeBooked[j]) {
|
|
currentDateFreePlaces.add(new Place(allPlaces.get(j).getId(), allPlaces.get(j).getPlace()));
|
|
}
|
|
}
|
|
freeBookings.put(currentDate.toString(), currentDateFreePlaces);
|
|
}
|
|
return freeBookings;
|
|
}
|
|
|
|
@Override
|
|
public ResponseEntity<String> SetNewBooking(String code, BookingInfo bookingInfo) {
|
|
if (!CheckAuthorization(code))
|
|
return new ResponseEntity<>("кода не существует", HttpStatus.UNAUTHORIZED);
|
|
|
|
List<Booking> allBookings = bookingRepository.findAll();
|
|
for (int i = 0; i < allBookings.size(); i++) {
|
|
if (allBookings.get(i).getDate().toString().equals(bookingInfo.date.toString()) && allBookings.get(i).getPlace().getId() == bookingInfo.placeId)
|
|
return new ResponseEntity<>("уже забронировано", HttpStatus.CONFLICT);
|
|
}
|
|
bookingRepository.save(Booking.builder()
|
|
.date(bookingInfo.date)
|
|
.place(placeRepository.findById(bookingInfo.placeId))
|
|
.employee(employeeRepository.findByCode(code).get())
|
|
.build());
|
|
return new ResponseEntity<>("бронирование успешно создано", HttpStatus.CREATED);
|
|
}
|
|
} |