package com.example.nto.service.impl; import com.example.nto.entity.Code; import com.example.nto.entity.Employee; import com.example.nto.repository.CodeRepository; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.Optional; @Service public class EmployeeServiceImpl implements EmployeeService { private final EmployeeRepository employeeRepository; private final CodeRepository codeRepository; @Autowired public EmployeeServiceImpl(EmployeeRepository employeeRepository, CodeRepository codeRepository){ this.employeeRepository = employeeRepository; this.codeRepository = codeRepository; } /** * Проверить существование логина в базе данных * @param login логин убзера * @return Существует ли запись в бд */ @Override public boolean checkEmployeeExists(String login) { return employeeRepository.existsByLogin(login); } /** * Получить информацию о юбзере по логину * @param login логин юбзера * @return всю инфу по юзеру */ @Override public Optional getEmployeeInfo(String login) { return employeeRepository.getByLogin(login); } /** * Попытка открыть дверь * @param login Логин юзера * @return true если дверь открыта, иначе false */ @Override public boolean doorIsOpen(String login, long code) { if (employeeRepository.existsByLogin(login)) { Code codeObject = new Code(); codeObject.setValue(code); codeRepository.save(codeObject); return true; } return false; } /** * Эта темка обновит время последнего визита до текущего момента * @param login логин пользователя */ @Override public void updateLastVisit(String login) { Employee employee = employeeRepository.getByLogin(login).get(); employee.setLastVisit(LocalDateTime.now()); } }