153 lines
6.4 KiB
Java
153 lines
6.4 KiB
Java
package com.example.nto.controller;
|
|
|
|
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;
|
|
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.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.core.Authentication;
|
|
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;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/employee/")
|
|
public class EmployeeController {
|
|
@Autowired
|
|
private EmployeeService employeeService;
|
|
@Autowired
|
|
private CodeService codeService;
|
|
|
|
private final Utils utils = new Utils();
|
|
|
|
@Operation(summary = "Get employee info")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200",
|
|
description = "OK",
|
|
content = {@Content(mediaType = "application/json",
|
|
schema = @Schema(implementation = Employee.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("/info")
|
|
public ResponseEntity<?> info(Authentication authentication) {
|
|
try {
|
|
String username = authentication.getName();
|
|
|
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
|
if (employee.isEmpty()) {
|
|
return utils.NotFound("EmployeeNotFound");
|
|
}
|
|
|
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
|
} catch (Exception e) {
|
|
System.out.println("Exception: " + e.getMessage());
|
|
return utils.BadRequest(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
|
|
|
|
@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> employee = employeeService.findByLogin(username);
|
|
if (employee.isEmpty()) {
|
|
return utils.NotFound("EmployeeNotFound");
|
|
}
|
|
Optional<List<Visits>> 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",
|
|
description = "Door opened 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))}),
|
|
@ApiResponse(responseCode = "403",
|
|
description = "Employee is blocked",
|
|
content = {@Content(mediaType = "application/json",
|
|
schema = @Schema(implementation = ResponseData.class))})
|
|
})
|
|
@PatchMapping("/open")
|
|
public ResponseEntity<?> open(Authentication authentication, @Valid @RequestBody CodeBody body) {
|
|
try {
|
|
String username = authentication.getName();
|
|
|
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
|
if (employee.isEmpty()) {
|
|
return utils.NotFound("EmployeeNotFound");
|
|
}
|
|
if (employee.get().getBlocked()) {
|
|
return utils.Forbidden("YouAreBlocked");
|
|
}
|
|
|
|
if (!codeService.exists(body.getValue())) {
|
|
return utils.BadRequest("CodeIsInvalid");
|
|
}
|
|
|
|
LocalDateTime time = LocalDateTime.now();
|
|
employeeService.setLastVisitEmployee(employee.get().getId(), time);
|
|
employeeService.addVisit(employee.get().getLogin(), time, "" + body.getValue());
|
|
return utils.Ok("OpenedSuccess");
|
|
} catch (Exception e) {
|
|
System.out.println("Exception: " + e.getMessage());
|
|
return utils.BadRequest(e.getLocalizedMessage());
|
|
}
|
|
}
|
|
}
|