Block, unblock and enterEmployee Usecases

This commit is contained in:
Niktia 2025-02-19 16:12:25 +03:00
parent aa3927a439
commit 8cdac4cb12
7 changed files with 80 additions and 47 deletions

View File

@ -2,33 +2,23 @@ package com.example.nto.controller;
import com.example.nto.dto.EmployeeDto;
import com.example.nto.dto.EnterDto;
import com.example.nto.dto.RegisterDto;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
import com.example.nto.entity.Enter;
import com.example.nto.exception.NoRequestBodyException;
import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.EmployeeService;
import com.example.nto.service.EnterService;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import com.example.nto.dto.CodeDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class EmployeeController {
private final EmployeeService employeeService;
private final CodeRepository codeRepository;
private final EnterService enterService;
@GetMapping("/login/{login}")
@ -56,29 +46,24 @@ public class EmployeeController {
}
@PatchMapping("/{login}/open")
public ResponseEntity<String> openEmployee(@PathVariable String login, @RequestBody Code code) {
try {
EmployeeDto employeeDto = employeeService.getByLogin(login);
if (employeeDto != null) {
boolean codeExists = codeRepository.checkCode(code.getValue());
if (codeExists) {
return ResponseEntity.ok("дверь открылась");
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("что-то пошло не так");
}
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("логина не существует или неверный");
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("что-то пошло не так");
}
public ResponseEntity<String> openEmployee(@PathVariable String login, @RequestBody CodeDto value) {
Long code = value.getValue();
return ResponseEntity.ok(employeeService.enterEmployee(login, code));
}
@GetMapping("/{login}/enters")
@GetMapping("/{login}/entrances")
public ResponseEntity<List<EnterDto>> infoEnters(@PathVariable String login) {
List<EnterDto> enters = enterService.getByLogin(login);
return ResponseEntity.ok(enters);
}
@PatchMapping("/{login}/block")
public ResponseEntity<String> blockEmployee(@PathVariable String login) {
return ResponseEntity.ok(employeeService.blockEmployee(login));
}
@PatchMapping("/{login}/unblock")
public ResponseEntity<String> unblockEmployee(@PathVariable String login) {
return ResponseEntity.ok(employeeService.unblockEmployee(login));
}
}

View File

@ -4,7 +4,8 @@ import com.example.nto.entity.Code;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.Optional;
public interface CodeRepository extends JpaRepository<Code, Long> {
@Query("SELECT CASE WHEN COUNT(c) > 0 THEN TRUE ELSE FALSE END FROM Code c WHERE c.value = :value")
boolean checkCode(long value);
Optional<Code> findByValue(long value);
}

View File

@ -4,7 +4,6 @@ import com.example.nto.entity.Enter;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
public interface EnterRepository extends JpaRepository<Enter, Long> {
List<Enter> findByEmployeeLogin(String login);

View File

@ -2,8 +2,12 @@ package com.example.nto.service;
import com.example.nto.dto.EmployeeDto;
import com.example.nto.dto.RegisterDto;
import com.example.nto.entity.Code;
import com.example.nto.entity.Enter;
public interface EmployeeService {
EmployeeDto getByLogin(String login);
String blockEmployee(String login);
String unblockEmployee(String login);
String enterEmployee(String login, Long value);
}

View File

@ -1,8 +1,6 @@
package com.example.nto.service;
import com.example.nto.dto.EnterDto;
import com.example.nto.entity.Enter;
import org.springframework.stereotype.Service;
import java.util.List;

View File

@ -1,32 +1,30 @@
package com.example.nto.service.impl;
import com.example.nto.dto.EmployeeDto;
import com.example.nto.dto.RegisterDto;
import com.example.nto.entity.Authority;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
import com.example.nto.entity.Enter;
import com.example.nto.exception.*;
import com.example.nto.repository.AuthorityRepository;
import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EnterRepository;
import com.example.nto.service.EmployeeService;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.utils.EmployeeMapper;
import lombok.RequiredArgsConstructor;
import org.h2.jdbc.JdbcSQLDataException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
private final AuthorityRepository authorityRepository;
private final PasswordEncoder passwordEncoder;
private final EnterRepository enterRepository;
private final CodeRepository codeRepository;
@Override
@ -37,4 +35,53 @@ public class EmployeeServiceImpl implements EmployeeService {
}
return EmployeeMapper.convertToDto(employee.get());
}
@Override
public String blockEmployee(String login) {
Optional<Employee> employeeOp = employeeRepository.findByLogin(login);
if (employeeOp.isPresent()) {
Employee employee = employeeOp.get();
employee.setAccess(false);
return "blocked";
}
throw new UserNotFoundException("user with login " + login + " not found");
}
@Override
public String unblockEmployee(String login) {
Optional<Employee> employeeOp = employeeRepository.findByLogin(login);
if (employeeOp.isPresent()) {
Employee employee = employeeOp.get();
employee.setAccess(true);
return "unblocked";
}
throw new UserNotFoundException("user with login " + login + " not found");
}
@Override
public String enterEmployee(String login, Long value) {
Employee employee = EmployeeMapper.convertFromDto(getByLogin(login));
if (employee != null) {
Optional<Code> codeOp = codeRepository.findByValue(value);
if (codeOp.isPresent()) {
Code code = codeOp.get();
if (employee.isAccess()) {
Enter enter = new Enter();
enter.setEmployee(employee);
enter.setCode(code);
enter.setEntered_at(Timestamp.valueOf(LocalDateTime.now()));
enter.setEnterType("Phone");
enterRepository.save(enter);
return "дверь открылась";
} else {
return "Доступ закрыт";
}
} else {
throw new CodeNotFoundException("Неверный код");
}
} else {
throw new UserNotFoundException("логина не существует или неверный");
}
}
}

View File

@ -9,7 +9,6 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service