[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
This commit is contained in:
parent
604166fbf8
commit
f14f91f725
@ -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<Employee> getEmployees() {
|
||||
return employeeService.getAllEmployees();
|
||||
}
|
||||
}
|
@ -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> employee = employeeService.findByLogin(login);
|
||||
Optional<Employee> 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> employee = employeeService.findByLogin(username);
|
||||
if (employee.isEmpty()) {
|
||||
return utils.NotFound();
|
||||
}
|
||||
|
||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||
if (employee.isEmpty()) {
|
||||
return NotFound();
|
||||
}
|
||||
Optional<Code> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<ResponseData> login() {
|
||||
return utils.Ok("Auth success!");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user