forked from Olympic/NTO-2025-Backend-TeamTask
Commit changes
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
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: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +19,102 @@ import com.example.nto.service.EmployeeService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
|
||||
@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);
|
||||
|
||||
Map<String, List<Place>> freePlacesMap = GetFreeBookings(code);
|
||||
if (!freePlacesMap.containsKey(bookingInfo.date.toString()))
|
||||
return new ResponseEntity<>("что-то пошло не так", HttpStatus.BAD_REQUEST);
|
||||
List<Place> freePlaces = freePlacesMap.get(bookingInfo.date.toString());
|
||||
for (int i = 0; i < freePlaces.size(); i++) {
|
||||
if (freePlaces.get(i).getId() == bookingInfo.placeId) {
|
||||
bookingRepository.save(Booking.builder()
|
||||
.date(bookingInfo.date)
|
||||
.place(freePlaces.get(i))
|
||||
.employee(employeeRepository.findByCode(code).get())
|
||||
.build());
|
||||
return new ResponseEntity<>("бронирование успешно создано", HttpStatus.CREATED);
|
||||
}
|
||||
}
|
||||
return new ResponseEntity<>("уже забронировано", HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user