From 7d59c875ddba8a92692787e9537e5b894bdc6970 Mon Sep 17 00:00:00 2001 From: Denis Oleynik Date: Wed, 19 Feb 2025 15:40:38 +0300 Subject: [PATCH] [Utils] move result-models methods to class `Utils` --- src/main/java/com/example/nto/Utils.java | 20 +++++++++ .../nto/controller/EmployeeController.java | 43 ------------------- 2 files changed, 20 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/example/nto/Utils.java diff --git a/src/main/java/com/example/nto/Utils.java b/src/main/java/com/example/nto/Utils.java new file mode 100644 index 0000000..1fe2552 --- /dev/null +++ b/src/main/java/com/example/nto/Utils.java @@ -0,0 +1,20 @@ +package com.example.nto; + +import com.example.nto.entity.ResponseData; +import org.springframework.http.HttpStatus; +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 BadRequest() { + return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), "Something went wrong"), HttpStatus.BAD_REQUEST); + } + + public ResponseEntity Ok(String message) { + return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), 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 dd23971..c2ee208 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -25,49 +25,6 @@ public class EmployeeController { @Autowired private EmployeeService employeeService; - private ResponseEntity NotFound() { - return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), "Login not found or invalid"), HttpStatus.UNAUTHORIZED); - } - - private ResponseEntity BadRequest() { - return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), "Something went wrong"), HttpStatus.BAD_REQUEST); - } - - private ResponseEntity Ok(String message) { - return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), HttpStatus.OK); - } - - - @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", - 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("/api/{login}/auth") - public ResponseEntity auth(@PathVariable("login") String login) { - try { - if (login == null || login.isEmpty() || !employeeService.existsByLogin(login)) { - return NotFound(); - } - - return Ok("Auth success"); - } catch (Exception e) { - System.out.println("Exception: " + e.getMessage()); - return BadRequest(); - } - } - - @Operation(summary = "Get employee info with login") @ApiResponses(value = { @ApiResponse(responseCode = "200",