Done working with server

This commit is contained in:
A1pha 2024-11-26 11:14:49 +03:00
parent dba8a9d00d
commit 6023a897df
2 changed files with 20 additions and 7 deletions

View File

@ -3,9 +3,11 @@ package com.example.nto.controller;
import com.example.nto.entity.Employee; import com.example.nto.entity.Employee;
import com.example.nto.service.EmployeeService; import com.example.nto.service.EmployeeService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@RestController @RestController
@RequiredArgsConstructor @RequiredArgsConstructor
@ -19,7 +21,7 @@ public class EmployeeController {
} }
@GetMapping("api/{login}/auth") @GetMapping("api/{login}/auth")
public boolean findExistByLogin(@PathVariable String login) { public void findExistByLogin(@PathVariable String login) {
return employeeService.findExistByLogin(login); employeeService.findExistByLogin(login);
} }
} }

View File

@ -7,14 +7,16 @@ import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.CodeService; import com.example.nto.service.CodeService;
import com.example.nto.service.EmployeeService; import com.example.nto.service.EmployeeService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Optional; import java.util.Optional;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService, CodeService { public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
private final EmployeeRepository employeeRepository; private final EmployeeRepository employeeRepository;
private final CodeRepository codeRepository; private final CodeRepository codeRepository;
@ -22,7 +24,7 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService {
@Override @Override
public Employee updateEmployee(long id, Employee newEmployee) { public Employee updateEmployee(long id, Employee newEmployee) {
Optional<Employee> optionalEmployee = employeeRepository.findById(id); Optional<Employee> 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 employee = optionalEmployee.get();
employee.setName(newEmployee.getName()); employee.setName(newEmployee.getName());
@ -36,12 +38,19 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService {
@Override @Override
public Employee findByLogin(String login) { public Employee findByLogin(String login) {
if (employeeRepository.findExistByLogin(login))
return employeeRepository.findByLogin(login); return employeeRepository.findByLogin(login);
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED,
"There is no account with login " + login + " or it is incorrect");
} }
@Override @Override
public Boolean findExistByLogin(String login) { 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 @Override
@ -53,7 +62,9 @@ public class EmployeeServiceImpl implements EmployeeService, CodeService {
updateEmployee(employeeId, employee); updateEmployee(employeeId, employee);
Optional<Code> codeOptional = codeRepository.findById(employeeId); Optional<Code> 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 code = codeOptional.get();
code.setValue(newCode.getValue()); code.setValue(newCode.getValue());