From 9b2c4501e1b9376c0a6c48950f9095593a0cb0a9 Mon Sep 17 00:00:00 2001 From: Denis Oleynik Date: Thu, 20 Feb 2025 10:58:08 +0300 Subject: [PATCH] Add method `Forbidden` to Utils; change logic of endpoint `/employee/open` --- src/main/java/com/example/nto/Utils.java | 12 ++++++--- .../nto/controller/AdminController.java | 9 +++---- .../nto/controller/EmployeeController.java | 27 ++++++++++++------- .../example/nto/service/EmployeeService.java | 2 +- .../nto/service/impl/EmployeeServiceImpl.java | 7 ++--- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/example/nto/Utils.java b/src/main/java/com/example/nto/Utils.java index 1fe2552..784dbb2 100644 --- a/src/main/java/com/example/nto/Utils.java +++ b/src/main/java/com/example/nto/Utils.java @@ -6,15 +6,19 @@ import org.springframework.http.ResponseEntity; public class Utils { - public ResponseEntity NotFound() { - return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), "Login not found or invalid"), HttpStatus.UNAUTHORIZED); + public ResponseEntity NotFound(String message) { + return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), message), HttpStatus.UNAUTHORIZED); } - public ResponseEntity BadRequest() { - return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), "Something went wrong"), HttpStatus.BAD_REQUEST); + public ResponseEntity BadRequest(String message) { + return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), message), HttpStatus.BAD_REQUEST); } public ResponseEntity Ok(String message) { return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), HttpStatus.OK); } + + public ResponseEntity Forbidden(String message) { + return new ResponseEntity<>(new ResponseData(HttpStatus.FORBIDDEN.value(), message), HttpStatus.FORBIDDEN); + } } diff --git a/src/main/java/com/example/nto/controller/AdminController.java b/src/main/java/com/example/nto/controller/AdminController.java index 7ab6353..0f06af0 100644 --- a/src/main/java/com/example/nto/controller/AdminController.java +++ b/src/main/java/com/example/nto/controller/AdminController.java @@ -2,7 +2,6 @@ package com.example.nto.controller; import com.example.nto.Utils; import com.example.nto.entity.Employee; -import com.example.nto.entity.ResponseData; import com.example.nto.entity.Visits; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; @@ -19,7 +18,7 @@ public class AdminController { @Autowired private EmployeeService employeeService; - private Utils utils = new Utils(); + private final Utils utils = new Utils(); @GetMapping("/employees") public List getEmployees() { @@ -30,7 +29,7 @@ public class AdminController { public ResponseEntity getEmployeeInfo(@PathVariable("username") String username) { Optional employee = employeeService.findByLogin(username); if (employee.isEmpty()) { - return utils.NotFound(); + return utils.NotFound("EmployeeNotFound"); } return new ResponseEntity<>(employee.get(), HttpStatus.OK); @@ -40,7 +39,7 @@ public class AdminController { public ResponseEntity getEmployeeLastVisit(@PathVariable("username") String username) { Optional employee = employeeService.findByLogin(username); if (employee.isEmpty()) { - return utils.NotFound(); + return utils.NotFound("EmployeeNotFound"); } return new ResponseEntity<>(employee.get(), HttpStatus.OK); @@ -49,7 +48,7 @@ public class AdminController { @GetMapping("/{username}/visits") public ResponseEntity getEmployeeVisits(@PathVariable("username") String username) { if (!employeeService.existsByLogin(username)) { - return utils.NotFound(); + return utils.NotFound("EmployeeNotFound"); } Optional> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username)); diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 2937686..8413f2b 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,7 +1,6 @@ package com.example.nto.controller; import com.example.nto.Utils; -import com.example.nto.entity.Code; import com.example.nto.entity.CodeBody; import com.example.nto.entity.Employee; import com.example.nto.entity.ResponseData; @@ -16,12 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; -import org.springframework.stereotype.Repository; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.time.LocalDateTime; import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; @RestController @RequestMapping("/api/employee/") @@ -31,7 +31,7 @@ public class EmployeeController { @Autowired private CodeService codeService; - private Utils utils = new Utils(); + private final Utils utils = new Utils(); @Operation(summary = "Get employee info with login") @ApiResponses(value = { @@ -55,13 +55,13 @@ public class EmployeeController { Optional employee = employeeService.findByLogin(username); if (employee.isEmpty()) { - return utils.NotFound(); + return utils.NotFound("EmployeeNotFound"); } return new ResponseEntity<>(employee.get(), HttpStatus.OK); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); - return utils.BadRequest(); + return utils.BadRequest(e.getLocalizedMessage()); } } @@ -77,6 +77,10 @@ public class EmployeeController { schema = @Schema(implementation = ResponseData.class))}), @ApiResponse(responseCode = "400", description = "Something went wrong", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = ResponseData.class))}), + @ApiResponse(responseCode = "403", + description = "Employee is blocked", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseData.class))}) }) @@ -87,20 +91,23 @@ public class EmployeeController { Optional employee = employeeService.findByLogin(username); if (employee.isEmpty()) { - return utils.NotFound(); + return utils.NotFound("EmployeeNotFound"); + } + if (employee.get().getBlocked()) { + return utils.Forbidden("YouAreBlocked"); } if (!codeService.exists(body.getValue())) { - return utils.BadRequest(); + return utils.BadRequest("CodeIsInvalid"); } LocalDateTime time = LocalDateTime.now(); employeeService.setLastVisitEmployee(employee.get().getId(), time); - employeeService.addVisit(employee.get().getLogin(), time); - return utils.Ok("Door opened success"); + employeeService.addVisit(employee.get().getLogin(), time, "" + body.getValue()); + return utils.Ok("OpenedSuccess"); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); - return utils.BadRequest(); + return utils.BadRequest(e.getLocalizedMessage()); } } } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index 8a2b4e7..d4e904a 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -17,7 +17,7 @@ public interface EmployeeService { void setLastVisitEmployee(long Id, LocalDateTime lastVisit); - void addVisit(String username, LocalDateTime time); + void addVisit(String username, LocalDateTime time, String readerId); void setCodeEmployee(long Id, long code); diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java index c67cb99..40c204f 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -2,9 +2,9 @@ package com.example.nto.service.impl; import com.example.nto.entity.Code; import com.example.nto.entity.Employee; +import com.example.nto.entity.VisitType; import com.example.nto.entity.Visits; import com.example.nto.repository.CodeRepository; -import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.VisitsRepository; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; @@ -41,8 +41,9 @@ public class EmployeeServiceImpl implements EmployeeService { } @Override - public void addVisit(String username, LocalDateTime time) { - visitRepository.save(new Visits()); + public void addVisit(String username, LocalDateTime time, String readerId) { + Visits visit = new Visits(0, username, time, VisitType.SCANNER, readerId); + visitRepository.save(visit); } @Override