One more fix #3

This commit is contained in:
A1pha 2024-12-06 13:21:59 +03:00
parent b730d8d5ac
commit 7497c4a93f
6 changed files with 27 additions and 34 deletions

View File

@ -1,11 +1,10 @@
package com.example.nto.controller;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
import com.example.nto.service.CodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
@RestController
@RequiredArgsConstructor
@ -14,7 +13,7 @@ public class CodeController {
private final CodeService codeService;
@PatchMapping("/api/{login}/open")
public Code update(@PathVariable String login, @RequestBody Code newCode) {
return codeService.update(login, newCode);
public Employee update(@PathVariable String login, @RequestBody Code newCode) {
return codeService.openDoor(login, newCode.getValue());
}
}

View File

@ -2,7 +2,12 @@ package com.example.nto.repository;
import com.example.nto.entity.Code;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface CodeRepository extends JpaRepository<Code, Long> {}
public interface CodeRepository extends JpaRepository<Code, Long> {
@Query("select count(e) = 1 from Code e where value = ?1")
boolean findExistByValue(Long value);
}

View File

@ -12,5 +12,5 @@ public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Employee findByLogin(String login);
@Query("select count(e) = 1 from Employee e where login = ?1")
boolean findExistByLogin(String login);
Boolean findExistByLogin(String login);
}

View File

@ -1,9 +1,10 @@
package com.example.nto.service;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
public interface CodeService {
Code update(String login, Code newCode);
Boolean findExistByValue(Long value);
Employee openDoor(String login, Long value);
}

View File

@ -6,5 +6,5 @@ public interface EmployeeService {
Employee updateEmployee(long id, Employee newEmployee);
Employee findByLogin(String login);
Boolean findExistByLogin(String login);
boolean findExistByLogin(String login);
}

View File

@ -1,6 +1,5 @@
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;
@ -45,7 +44,7 @@ public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
}
@Override
public Boolean findExistByLogin(String login) {
public boolean findExistByLogin(String login) {
if (employeeRepository.findExistByLogin(login))
throw new ResponseStatusException(HttpStatus.OK, "Login is existing, processing");
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED,
@ -54,31 +53,20 @@ public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
}
@Override
public Code update(String login, Code newCode) {
Employee employee = findByLogin(login);
long employeeId = employee.getId();
employee.setLastVisit(LocalDateTime.now());
updateEmployee(employeeId, employee);
if (newCode.getValue() == 0)
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
Optional<Code> codeOptional = codeRepository.findById(employeeId);
Code code;
if (codeOptional.isEmpty()) {
code = newCode;
code.setId(employeeId);
} else {
code = codeOptional.get();
if (code.getValue() != newCode.getValue()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
code.setValue(newCode.getValue());
public Employee openDoor(String login, Long value) {
if (findByLogin(login) != null && findExistByValue(value)) {
Employee employee = findByLogin(login);
employee.setLastVisit(LocalDateTime.now());
return updateEmployee(employee.getId(), employee);
}
return codeRepository.save(code);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
@Override
public Boolean findExistByValue(Long value) {
if (codeRepository.findExistByValue(value))
return codeRepository.findExistByValue(value);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
}