One more fix #3
This commit is contained in:
parent
b730d8d5ac
commit
7497c4a93f
@ -1,11 +1,10 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.service.CodeService;
|
import com.example.nto.service.CodeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -14,7 +13,7 @@ public class CodeController {
|
|||||||
private final CodeService codeService;
|
private final CodeService codeService;
|
||||||
|
|
||||||
@PatchMapping("/api/{login}/open")
|
@PatchMapping("/api/{login}/open")
|
||||||
public Code update(@PathVariable String login, @RequestBody Code newCode) {
|
public Employee update(@PathVariable String login, @RequestBody Code newCode) {
|
||||||
return codeService.update(login, newCode);
|
return codeService.openDoor(login, newCode.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,12 @@ package com.example.nto.repository;
|
|||||||
|
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@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);
|
||||||
|
}
|
||||||
|
@ -12,5 +12,5 @@ public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
|||||||
Employee findByLogin(String login);
|
Employee findByLogin(String login);
|
||||||
|
|
||||||
@Query("select count(e) = 1 from Employee e where login = ?1")
|
@Query("select count(e) = 1 from Employee e where login = ?1")
|
||||||
boolean findExistByLogin(String login);
|
Boolean findExistByLogin(String login);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Employee;
|
||||||
|
|
||||||
public interface CodeService {
|
public interface CodeService {
|
||||||
|
|
||||||
Code update(String login, Code newCode);
|
Boolean findExistByValue(Long value);
|
||||||
|
Employee openDoor(String login, Long value);
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ public interface EmployeeService {
|
|||||||
|
|
||||||
Employee updateEmployee(long id, Employee newEmployee);
|
Employee updateEmployee(long id, Employee newEmployee);
|
||||||
Employee findByLogin(String login);
|
Employee findByLogin(String login);
|
||||||
Boolean findExistByLogin(String login);
|
boolean findExistByLogin(String login);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.entity.Code;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.repository.CodeRepository;
|
import com.example.nto.repository.CodeRepository;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
@ -45,7 +44,7 @@ public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean findExistByLogin(String login) {
|
public boolean findExistByLogin(String login) {
|
||||||
if (employeeRepository.findExistByLogin(login))
|
if (employeeRepository.findExistByLogin(login))
|
||||||
throw new ResponseStatusException(HttpStatus.OK, "Login is existing, processing");
|
throw new ResponseStatusException(HttpStatus.OK, "Login is existing, processing");
|
||||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED,
|
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED,
|
||||||
@ -54,31 +53,20 @@ public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Code update(String login, Code newCode) {
|
public Employee openDoor(String login, Long value) {
|
||||||
|
if (findByLogin(login) != null && findExistByValue(value)) {
|
||||||
Employee employee = findByLogin(login);
|
Employee employee = findByLogin(login);
|
||||||
long employeeId = employee.getId();
|
employee.setLastVisit(LocalDateTime.now());
|
||||||
|
return updateEmployee(employee.getId(), employee);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user