package com.example.nto.controller; 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; @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 = employeeService.findByLogin(username); if (employee.isEmpty()) { throw new EmployeeNotFoundException(); } return new ResponseEntity<>(employee.get(), HttpStatus.OK); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), e.getLocalizedMessage()), HttpStatus.BAD_REQUEST); } } @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()) { throw new EmployeeNotFoundException(); } Optional> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username)); if (visits.isEmpty()) { throw new VisitsEmptyException(); } return new ResponseEntity<>(visits.get(), HttpStatus.OK); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), e.getLocalizedMessage()), HttpStatus.BAD_REQUEST); } } @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 = employeeService.findByLogin(username); if (employee.isEmpty()) { throw new EmployeeNotFoundException(); } if (employee.get().getBlocked()) { throw new YouAreBlockedException(); } if (!codeService.exists(body.getValue())) { throw new CodeNotFoundException(); } LocalDateTime time = LocalDateTime.now(); employeeService.setLastVisitEmployee(employee.get().getId(), time); employeeService.addVisit(employee.get().getLogin(), time, "" + body.getValue()); return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), "DoorOpenedSuccess"), HttpStatus.OK); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), e.getLocalizedMessage()), HttpStatus.BAD_REQUEST); } } }