From f14f91f7250100f718717e6d9284391e590b3675 Mon Sep 17 00:00:00 2001 From: Denis Oleynik Date: Wed, 19 Feb 2025 16:02:50 +0300 Subject: [PATCH] [controller] Add controllers and change EmployeeController - Create AdminController with path /api/admin with endpoint admin endpoint `/employees` - Create GlobalController with endpoint `/api/login` - Remove some endpoints and refactor remaining endpoints --- .../nto/controller/AdminController.java | 22 ++++++++ .../nto/controller/EmployeeController.java | 50 ++++++++++--------- .../nto/controller/GlobalController.java | 33 ++++++++++++ 3 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/example/nto/controller/AdminController.java create mode 100644 src/main/java/com/example/nto/controller/GlobalController.java diff --git a/src/main/java/com/example/nto/controller/AdminController.java b/src/main/java/com/example/nto/controller/AdminController.java new file mode 100644 index 0000000..45a6dca --- /dev/null +++ b/src/main/java/com/example/nto/controller/AdminController.java @@ -0,0 +1,22 @@ +package com.example.nto.controller; + +import com.example.nto.entity.Employee; +import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api/admin") +public class AdminController { + @Autowired + private EmployeeService employeeService; + + @GetMapping("/employees") + public List getEmployees() { + return employeeService.getAllEmployees(); + } +} diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index c2ee208..42872a3 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,5 +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; @@ -13,6 +14,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; 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.*; @@ -21,10 +23,13 @@ import java.time.LocalDateTime; import java.util.Optional; @RestController +@RequestMapping("/api/employee/") public class EmployeeController { @Autowired private EmployeeService employeeService; + private Utils utils = new Utils(); + @Operation(summary = "Get employee info with login") @ApiResponses(value = { @ApiResponse(responseCode = "200", @@ -40,22 +45,20 @@ public class EmployeeController { content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseData.class))}) }) - @GetMapping("/api/{login}/info") - public ResponseEntity info(@PathVariable("login") String login) { + @GetMapping("/info") + public ResponseEntity info(Authentication authentication) { try { - if (login == null || login.isEmpty() || login.isBlank() || !employeeService.existsByLogin(login)) { - return NotFound(); - } + String username = authentication.getName(); - Optional employee = employeeService.findByLogin(login); + Optional employee = employeeService.findByLogin(username); if (employee.isEmpty()) { - return NotFound(); + return utils.NotFound(); } return new ResponseEntity<>(employee.get(), HttpStatus.OK); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); - return BadRequest(); + return utils.BadRequest(); } } @@ -74,41 +77,40 @@ public class EmployeeController { content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseData.class))}) }) - @PatchMapping("/api/{login}/open") - public ResponseEntity open(@PathVariable("login") String login, @Valid @RequestBody CodeBody body) { + @PatchMapping("/open") + public ResponseEntity open(Authentication authentication, @Valid @RequestBody CodeBody body) { try { - if (login == null || login.isEmpty() || login.isBlank() || !employeeService.existsByLogin(login)) { - return NotFound(); + String username = authentication.getName(); + + Optional employee = employeeService.findByLogin(username); + if (employee.isEmpty()) { + return utils.NotFound(); } - Optional employee = employeeService.findByLogin(login); - if (employee.isEmpty()) { - return NotFound(); - } Optional code = employeeService.findCodeById(employee.get().getId()); if (code.isEmpty()) { - return BadRequest(); + return utils.BadRequest(); } - long dbValue = 0; + long codeFromDB = 0; try { - dbValue = code.get().getValue(); + codeFromDB = code.get().getValue(); } catch (Exception e) { employeeService.setCodeEmployee(employee.get().getId(), body.getValue()); employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now()); - return Ok("Door opened success"); + return utils.Ok("Door opened success"); } - if (dbValue != body.getValue()) { - return BadRequest(); + if (codeFromDB != body.getValue()) { + return utils.BadRequest(); } employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now()); - return Ok("Door opened success"); + return utils.Ok("Door opened success"); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); - return BadRequest(); + return utils.BadRequest(); } } } diff --git a/src/main/java/com/example/nto/controller/GlobalController.java b/src/main/java/com/example/nto/controller/GlobalController.java new file mode 100644 index 0000000..4fc58ae --- /dev/null +++ b/src/main/java/com/example/nto/controller/GlobalController.java @@ -0,0 +1,33 @@ +package com.example.nto.controller; + +import com.example.nto.Utils; +import com.example.nto.entity.ResponseData; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +public class GlobalController { + Utils utils = new Utils(); + + @PostMapping("/api/login") + @Operation(summary = "Auth employee with login") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", + description = "Auth employee with login success", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = ResponseData.class))}), + @ApiResponse(responseCode = "401", + description = "Login not found or invalid"), + @ApiResponse(responseCode = "400", + description = "Something went wrong") + + }) + public ResponseEntity login() { + return utils.Ok("Auth success!"); + } +}