From b5a88952ff2add998239a45b1ca25e85213655fa Mon Sep 17 00:00:00 2001 From: Denis Oleynik Date: Thu, 20 Feb 2025 12:40:15 +0300 Subject: [PATCH] Add employee endpoint `/visits` --- .../nto/controller/AdminController.java | 4 ++ .../nto/controller/EmployeeController.java | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/nto/controller/AdminController.java b/src/main/java/com/example/nto/controller/AdminController.java index 1a89976..ef10dba 100644 --- a/src/main/java/com/example/nto/controller/AdminController.java +++ b/src/main/java/com/example/nto/controller/AdminController.java @@ -53,6 +53,10 @@ public class AdminController { Optional> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username)); + if (visits.isEmpty()) { + return utils.BadRequest("VisitsIsEmpty"); + } + return new ResponseEntity<>(visits.get(), HttpStatus.OK); } diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 8413f2b..69e9879 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -4,6 +4,7 @@ import com.example.nto.Utils; import com.example.nto.entity.CodeBody; import com.example.nto.entity.Employee; import com.example.nto.entity.ResponseData; +import com.example.nto.entity.Visits; import com.example.nto.service.CodeService; import com.example.nto.service.EmployeeService; import io.swagger.v3.oas.annotations.Operation; @@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,7 +35,7 @@ public class EmployeeController { private final Utils utils = new Utils(); - @Operation(summary = "Get employee info with login") + @Operation(summary = "Get employee info") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "OK", @@ -65,6 +67,43 @@ public class EmployeeController { } } + + @Operation(summary = "Get employee entries") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", + description = "OK", + content = {@Content(mediaType = "application/json")}), + @ApiResponse(responseCode = "401", + description = "Login not found or invalid", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = ResponseData.class))}), + @ApiResponse(responseCode = "400", + description = "Something went wrong", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = ResponseData.class))}) + }) + @GetMapping("/visits") + public ResponseEntity getEmployeeVisits(Authentication authentication) { + try { + String username = authentication.getName(); + + Optional employee = employeeService.findByLogin(username); + if (employee.isEmpty()) { + return utils.NotFound("EmployeeNotFound"); + } + Optional> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username)); + + if (visits.isEmpty()) { + return utils.BadRequest("VisitsIsEmpty"); + } + + return new ResponseEntity<>(visits.get(), HttpStatus.OK); + } catch (Exception e) { + System.out.println("Exception: " + e.getMessage()); + return utils.BadRequest(e.getLocalizedMessage()); + } + } + @Operation(summary = "Open door with code") @ApiResponses(value = { @ApiResponse(responseCode = "200",