TheDevs-Back/src/main/java/com/example/nto/controller/EmployeeController.java
2025-02-19 12:41:51 +03:00

92 lines
2.7 KiB
Java

package com.example.nto.controller;
import com.example.nto.entity.Employee;
import com.example.nto.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Optional;
//я поменял на BAD_REQUEST 06.12.24 23:00
@RestController
@RequestMapping("/api")//база
public class EmployeeController {
private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("/{login}/auth")//auth
public ResponseEntity<?> authenticate(@PathVariable String login) {
Optional<Employee> employee = employeeService.findByLogin(login);
if (employee.isPresent()) {
return ResponseEntity.ok("Valid login");
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
}
}
@GetMapping("/{login}/info")//info
public ResponseEntity<?> getInfo(@PathVariable String login) {
Optional<Employee> employee = employeeService.findByLogin(login);
if (employee.isPresent()) {
return ResponseEntity.ok(employee.get());
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
}
}
@PatchMapping("/{login}/open")//open
public ResponseEntity<?> openDoor(@PathVariable String login, @RequestBody Map<String, Long> payload) {
Long code = payload.get("value");
if (code == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload");
}
Optional<Employee> employee = employeeService.findByLogin(login);
if (employee.isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login");
}
if (employeeService.validateCode(login, code)) {
return ResponseEntity.ok("Door opened");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
}
}
@PostMapping("/auth") // auth
public ResponseEntity<?> authenticate(@RequestBody Map<String, String> payload) {
String login = payload.get("login");
String password = payload.get("password");
Optional<Employee> employee = employeeService.findByLogin(login);
if (employee.isPresent() && employee.get().getPassword().equals(password)) {
return ResponseEntity.ok("Valid login");
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login or password");
}
}
}
// made by truettwo and maks ))