Some fixes

This commit is contained in:
A1pha 2024-12-05 17:00:02 +03:00
parent 6023a897df
commit 2d88b38796
3 changed files with 12 additions and 9 deletions

View File

@ -3,7 +3,9 @@ package com.example.nto.controller;
import com.example.nto.entity.Code;
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
@ -13,6 +15,8 @@ public class CodeController {
@PatchMapping("/api/{login}/open")
public Code update(@PathVariable String login, @RequestBody Code newCode) {
if (newCode.getValue() == 0)
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
return codeService.update(login, newCode);
}
}

View File

@ -3,11 +3,9 @@ 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

View File

@ -62,13 +62,14 @@ public class EmployeeCodeServiceImpl implements EmployeeService, CodeService {
updateEmployee(employeeId, employee);
Optional<Code> codeOptional = codeRepository.findById(employeeId);
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());
Code code;
if (codeOptional.isEmpty()) {
code = newCode;
code.setId(employeeId);
} else {
code = codeOptional.get();
code.setValue(newCode.getValue());
}
return codeRepository.save(code);
}