diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index a9e251c..72e576e 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -3,9 +3,11 @@ package com.example.nto.controller; import com.example.nto.entity.Employee; import com.example.nto.service.EmployeeService; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; @RestController @RequiredArgsConstructor @@ -19,7 +21,7 @@ public class EmployeeController { } @GetMapping("api/{login}/auth") - public boolean findExistByLogin(@PathVariable String login) { - return employeeService.findExistByLogin(login); + public void findExistByLogin(@PathVariable String login) { + employeeService.findExistByLogin(login); } } diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeCodeServiceImpl.java similarity index 64% rename from src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java rename to src/main/java/com/example/nto/service/impl/EmployeeCodeServiceImpl.java index 637b2d8..ab55d38 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeCodeServiceImpl.java @@ -7,14 +7,16 @@ import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.CodeService; import com.example.nto.service.EmployeeService; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; import java.time.LocalDateTime; import java.util.Optional; @Service @RequiredArgsConstructor -public class EmployeeServiceImpl implements EmployeeService, CodeService { +public class EmployeeCodeServiceImpl implements EmployeeService, CodeService { private final EmployeeRepository employeeRepository; private final CodeRepository codeRepository; @@ -22,7 +24,7 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService { @Override public Employee updateEmployee(long id, Employee newEmployee) { Optional optionalEmployee = employeeRepository.findById(id); - if (optionalEmployee.isEmpty()) throw new RuntimeException("No such user with id" + id); + if (optionalEmployee.isEmpty()) throw new RuntimeException("No such user with id " + id); Employee employee = optionalEmployee.get(); employee.setName(newEmployee.getName()); @@ -36,12 +38,19 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService { @Override public Employee findByLogin(String login) { - return employeeRepository.findByLogin(login); + if (employeeRepository.findExistByLogin(login)) + return employeeRepository.findByLogin(login); + throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, + "There is no account with login " + login + " or it is incorrect"); } @Override public Boolean findExistByLogin(String login) { - return employeeRepository.findExistByLogin(login); + if (employeeRepository.findExistByLogin(login)) + throw new ResponseStatusException(HttpStatus.OK, "Login is existing, processing"); + throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, + "There is no account with login " + login + " or it is incorrect"); + } @Override @@ -53,7 +62,9 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService { updateEmployee(employeeId, employee); Optional codeOptional = codeRepository.findById(employeeId); - if (codeOptional.isEmpty()) throw new RuntimeException("Code with id " + employeeId + "is not found"); + if (codeOptional.isEmpty()) + throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, + "There is no account with login " + login + " or it is incorrect"); Code code = codeOptional.get(); code.setValue(newCode.getValue());