no problem
This commit is contained in:
parent
23f38f31d2
commit
7c01a29e3f
@ -3,23 +3,28 @@ package com.example.nto.controller;
|
|||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.User;
|
import com.example.nto.entity.User;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import com.example.nto.service.UserService;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder; // Импортируйте PasswordEncoder
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
//я поменял на BAD_REQUEST 06.12.24 23:00
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
private final EmployeeService employeeService;
|
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.employeeService = employeeService;
|
||||||
|
this.userService = userService;
|
||||||
|
this.passwordEncoder = passwordEncoder; // Инициализация поля
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
@ -29,20 +34,20 @@ public class EmployeeController {
|
|||||||
return ResponseEntity.ok(employees);
|
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) {
|
public ResponseEntity<?> authenticate(@PathVariable String login) {
|
||||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
if (employee.isPresent()) {
|
if (employee.isPresent()) {
|
||||||
return ResponseEntity.ok("Valid login");
|
return ResponseEntity.ok("Valid login");
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{login}/info")
|
@GetMapping("/{login}/info")
|
||||||
@ -54,28 +59,20 @@ public class EmployeeController {
|
|||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
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) {
|
public ResponseEntity<?> openDoor(@PathVariable String login, @RequestBody Map<String, Long> payload) {
|
||||||
Long code = payload.get("value");
|
Long code = payload.get("value");
|
||||||
if (code == null) {
|
if (code == null) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (employeeService.validateCode(login, code)) {
|
if (employeeService.validateCode(login, code)) {
|
||||||
return ResponseEntity.ok("Door opened");
|
return ResponseEntity.ok("Door opened");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
|
||||||
}
|
}
|
||||||
@ -83,11 +80,10 @@ public class EmployeeController {
|
|||||||
|
|
||||||
@GetMapping("/auth")
|
@GetMapping("/auth")
|
||||||
public ResponseEntity<?> authenticate(@RequestParam String username, @RequestParam String password) {
|
public ResponseEntity<?> authenticate(@RequestParam String username, @RequestParam String password) {
|
||||||
User user = userService.findByUsername(username);
|
Optional<User> optionalUser = userService.findByUsername(username); // Исправление
|
||||||
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
|
if (optionalUser.isPresent() && passwordEncoder.matches(password, optionalUser.get().getPassword())) { // Исправление
|
||||||
return ResponseEntity.ok("Valid login");
|
return ResponseEntity.ok("Valid login");
|
||||||
}
|
}
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// made by truettwo and maks ))
|
|
@ -13,9 +13,9 @@ public class Code {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
private long value;
|
private long value; // Убедитесь, что это поле существует
|
||||||
}
|
|
||||||
// made by truettwo
|
// Геттеры и сеттеры для value
|
||||||
|
}
|
@ -2,8 +2,6 @@ package com.example.nto.entity;
|
|||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -17,14 +15,13 @@ public class Employee {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
private String login;
|
private String login; // Убедитесь, что это поле существует
|
||||||
private String name;
|
private String name;
|
||||||
private String photo;
|
private String photo;
|
||||||
private String position;
|
private String position;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Column(name = "last_visit")
|
@Column(name = "last_visit")
|
||||||
private LocalDateTime lastVisit;
|
private LocalDateTime lastVisit;
|
||||||
}
|
|
||||||
// made by truettwo
|
// Геттеры и сеттеры для login, name, и других полей
|
||||||
|
}
|
@ -2,10 +2,11 @@ package com.example.nto.service;
|
|||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
|
List<Employee> findAll(); // Методы, необходимые в контроллере
|
||||||
Optional<Employee> findByLogin(String login);
|
Optional<Employee> findByLogin(String login);
|
||||||
|
|
||||||
boolean validateCode(String login, long code);
|
boolean validateCode(String login, long code);
|
||||||
}
|
}
|
@ -1,32 +1,28 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.entity.Code; //надо удалить
|
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.repository.CodeRepository;
|
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
private final CodeRepository codeRepository;
|
|
||||||
|
|
||||||
|
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
|
||||||
|
|
||||||
|
|
||||||
public EmployeeServiceImpl(EmployeeRepository employeeRepository, CodeRepository codeRepository) {
|
|
||||||
this.employeeRepository = employeeRepository;
|
this.employeeRepository = employeeRepository;
|
||||||
this.codeRepository = codeRepository;
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Employee> findAll() {
|
||||||
|
return employeeRepository.findAll(); // Реализуем метод findAll
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Employee> findByLogin(String login) {
|
public Optional<Employee> findByLogin(String login) {
|
||||||
|
|
||||||
return employeeRepository.findAll()
|
return employeeRepository.findAll()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(employee -> employee.getLogin().equals(login))
|
.filter(employee -> employee.getLogin().equals(login))
|
||||||
@ -35,10 +31,7 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validateCode(String login, long code) {
|
public boolean validateCode(String login, long code) {
|
||||||
|
// Реализуйте вашу логику для валидации кода
|
||||||
return codeRepository.findAll()
|
return false; // Замените на действительную логику
|
||||||
.stream()
|
|
||||||
.anyMatch(c -> c.getValue() == code);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// made by truettwo
|
|
Loading…
x
Reference in New Issue
Block a user