forked from Olympic/NTO-2025-Backend-TeamTask
80 lines
3.1 KiB
Java
80 lines
3.1 KiB
Java
package com.example.nto.service.impl;
|
|
|
|
import com.example.nto.dto.BookingCreateDTO;
|
|
import com.example.nto.dto.EmpInfoDTO;
|
|
import com.example.nto.entity.Booking;
|
|
import com.example.nto.entity.Employee;
|
|
import com.example.nto.exception.CodeIsNotExistException;
|
|
import com.example.nto.exception.PlaceAlreadyBookedException;
|
|
import com.example.nto.exception.UnknownException;
|
|
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 com.example.nto.until.EmployeeMapper;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
|
* =================================
|
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
|
*/
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class EmployeeServiceImpl implements EmployeeService {
|
|
|
|
private final EmployeeRepository employeeRepository;
|
|
private final BookingRepository bookingRepository;
|
|
private final PlaceRepository placeRepository;
|
|
|
|
@Override
|
|
public EmpInfoDTO getInfoByCode(String code) {
|
|
Optional<Employee> optionalEmployee = employeeRepository.findByCode(code);
|
|
|
|
if(optionalEmployee.isEmpty()){
|
|
throw new CodeIsNotExistException("кода не существует");
|
|
}
|
|
|
|
return optionalEmployee.map(EmployeeMapper::convertToDTO).orElseThrow(() -> new UnknownException("что-то пошло не так"));
|
|
}
|
|
|
|
@Override
|
|
public String employeeAuth(String code) {
|
|
Optional<Employee> optionalEmployee = employeeRepository.findByCode(code);
|
|
|
|
if(optionalEmployee.isEmpty()){
|
|
throw new CodeIsNotExistException("кода не существует");
|
|
}
|
|
|
|
return "данный код существует - можно пользоваться приложением";
|
|
}
|
|
|
|
@Override
|
|
public String createBooking(String code, BookingCreateDTO bookingCreateDTO) {
|
|
Optional<Employee> optionalEmployee = employeeRepository.findByCode(code);
|
|
|
|
if(optionalEmployee.isEmpty()){
|
|
throw new CodeIsNotExistException("кода не существует");
|
|
}
|
|
|
|
Employee employee = optionalEmployee.get();
|
|
|
|
if(bookingRepository.existsByDateAndPlaceId(bookingCreateDTO.getDate(), bookingCreateDTO.getPlaceId())){
|
|
throw new PlaceAlreadyBookedException("уже забронировано");
|
|
}
|
|
|
|
Booking booking = new Booking();
|
|
booking.setDate(bookingCreateDTO.getDate());
|
|
booking.setEmployee(employee);
|
|
booking.setPlace(placeRepository.findById(bookingCreateDTO.getPlaceId())
|
|
.orElseThrow(() -> new UnknownException("что-то пошло не так")));
|
|
|
|
bookingRepository.save(booking);
|
|
return "бронирование успешно создано";
|
|
}
|
|
}
|