no problem

This commit is contained in:
truettwo 2025-02-19 12:17:15 +03:00
parent 23f38f31d2
commit 7c01a29e3f
5 changed files with 38 additions and 51 deletions

View File

@ -3,23 +3,28 @@ package com.example.nto.controller;
import com.example.nto.entity.Employee;
import com.example.nto.entity.User;
import com.example.nto.service.EmployeeService;
import com.example.nto.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder; // Импортируйте PasswordEncoder
import org.springframework.web.bind.annotation.*;
import java.util.List;
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;
private final UserService userService;
private final PasswordEncoder passwordEncoder; // Добавленное поле
public EmployeeController(EmployeeService employeeService) {
public EmployeeController(EmployeeService employeeService, UserService userService, PasswordEncoder passwordEncoder) {
this.employeeService = employeeService;
this.userService = userService;
this.passwordEncoder = passwordEncoder; // Инициализация поля
}
@PreAuthorize("hasRole('ADMIN')")
@ -29,20 +34,20 @@ public class EmployeeController {
return ResponseEntity.ok(employees);
}
@PostMapping("/register")
public ResponseEntity<?> registerUser(@RequestBody User user) {
userService.saveUser(user);
return ResponseEntity.status(HttpStatus.CREATED).body("User registered successfully");
}
@GetMapping("/{login}/auth")//auth
@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")
@ -54,28 +59,20 @@ public class EmployeeController {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
}
@PatchMapping("/{login}/open")//open
@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");
}
@ -83,11 +80,10 @@ public class EmployeeController {
@GetMapping("/auth")
public ResponseEntity<?> authenticate(@RequestParam String username, @RequestParam String password) {
User user = userService.findByUsername(username);
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
Optional<User> optionalUser = userService.findByUsername(username); // Исправление
if (optionalUser.isPresent() && passwordEncoder.matches(password, optionalUser.get().getPassword())) { // Исправление
return ResponseEntity.ok("Valid login");
}
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
}
}
// made by truettwo and maks ))
}

View File

@ -13,9 +13,9 @@ public class Code {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private long value;
}
// made by truettwo
private long value; // Убедитесь, что это поле существует
// Геттеры и сеттеры для value
}

View File

@ -2,8 +2,6 @@ package com.example.nto.entity;
import javax.persistence.*;
import lombok.*;
import javax.persistence.Entity;
import java.time.LocalDateTime;
@Data
@ -17,14 +15,13 @@ public class Employee {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String login;
private String login; // Убедитесь, что это поле существует
private String name;
private String photo;
private String position;
@Column(name = "last_visit")
private LocalDateTime lastVisit;
}
// made by truettwo
// Геттеры и сеттеры для login, name, и других полей
}

View File

@ -2,10 +2,11 @@ package com.example.nto.service;
import com.example.nto.entity.Employee;
import java.util.List;
import java.util.Optional;
public interface EmployeeService {
List<Employee> findAll(); // Методы, необходимые в контроллере
Optional<Employee> findByLogin(String login);
boolean validateCode(String login, long code);
}
}

View File

@ -1,32 +1,28 @@
package com.example.nto.service.impl;
import com.example.nto.entity.Code; //надо удалить
import com.example.nto.entity.Employee;
import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.EmployeeService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
private final CodeRepository codeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository, CodeRepository codeRepository) {
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
this.codeRepository = codeRepository;
}
@Override
public List<Employee> findAll() {
return employeeRepository.findAll(); // Реализуем метод findAll
}
@Override
public Optional<Employee> findByLogin(String login) {
return employeeRepository.findAll()
.stream()
.filter(employee -> employee.getLogin().equals(login))
@ -35,10 +31,7 @@ public class EmployeeServiceImpl implements EmployeeService {
@Override
public boolean validateCode(String login, long code) {
return codeRepository.findAll()
.stream()
.anyMatch(c -> c.getValue() == code);
// Реализуйте вашу логику для валидации кода
return false; // Замените на действительную логику
}
}
// made by truettwo
}