Block, unblock and enterEmployee Usecases
This commit is contained in:
parent
aa3927a439
commit
8cdac4cb12
@ -2,33 +2,23 @@ package com.example.nto.controller;
|
|||||||
|
|
||||||
import com.example.nto.dto.EmployeeDto;
|
import com.example.nto.dto.EmployeeDto;
|
||||||
import com.example.nto.dto.EnterDto;
|
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.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.EmployeeService;
|
||||||
import com.example.nto.service.EnterService;
|
import com.example.nto.service.EnterService;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import com.example.nto.dto.CodeDto;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
private final CodeRepository codeRepository;
|
|
||||||
private final EnterService enterService;
|
private final EnterService enterService;
|
||||||
|
|
||||||
@GetMapping("/login/{login}")
|
@GetMapping("/login/{login}")
|
||||||
@ -56,29 +46,24 @@ public class EmployeeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{login}/open")
|
@PatchMapping("/{login}/open")
|
||||||
public ResponseEntity<String> openEmployee(@PathVariable String login, @RequestBody Code code) {
|
public ResponseEntity<String> openEmployee(@PathVariable String login, @RequestBody CodeDto value) {
|
||||||
try {
|
Long code = value.getValue();
|
||||||
EmployeeDto employeeDto = employeeService.getByLogin(login);
|
return ResponseEntity.ok(employeeService.enterEmployee(login, code));
|
||||||
|
|
||||||
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("что-то пошло не так");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{login}/enters")
|
@GetMapping("/{login}/entrances")
|
||||||
public ResponseEntity<List<EnterDto>> infoEnters(@PathVariable String login) {
|
public ResponseEntity<List<EnterDto>> infoEnters(@PathVariable String login) {
|
||||||
List<EnterDto> enters = enterService.getByLogin(login);
|
List<EnterDto> enters = enterService.getByLogin(login);
|
||||||
return ResponseEntity.ok(enters);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ import com.example.nto.entity.Code;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface CodeRepository extends JpaRepository<Code, Long> {
|
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")
|
Optional<Code> findByValue(long value);
|
||||||
boolean checkCode(long value);
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import com.example.nto.entity.Enter;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface EnterRepository extends JpaRepository<Enter, Long> {
|
public interface EnterRepository extends JpaRepository<Enter, Long> {
|
||||||
List<Enter> findByEmployeeLogin(String login);
|
List<Enter> findByEmployeeLogin(String login);
|
||||||
|
@ -2,8 +2,12 @@ package com.example.nto.service;
|
|||||||
|
|
||||||
|
|
||||||
import com.example.nto.dto.EmployeeDto;
|
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 {
|
public interface EmployeeService {
|
||||||
EmployeeDto getByLogin(String login);
|
EmployeeDto getByLogin(String login);
|
||||||
|
String blockEmployee(String login);
|
||||||
|
String unblockEmployee(String login);
|
||||||
|
String enterEmployee(String login, Long value);
|
||||||
}
|
}
|
@ -1,8 +1,6 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import com.example.nto.dto.EnterDto;
|
import com.example.nto.dto.EnterDto;
|
||||||
import com.example.nto.entity.Enter;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -1,32 +1,30 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.dto.EmployeeDto;
|
import com.example.nto.dto.EmployeeDto;
|
||||||
import com.example.nto.dto.RegisterDto;
|
import com.example.nto.entity.Code;
|
||||||
import com.example.nto.entity.Authority;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.Enter;
|
||||||
import com.example.nto.exception.*;
|
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.service.EmployeeService;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
|
|
||||||
import com.example.nto.utils.EmployeeMapper;
|
import com.example.nto.utils.EmployeeMapper;
|
||||||
import lombok.RequiredArgsConstructor;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
private final AuthorityRepository authorityRepository;
|
private final EnterRepository enterRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final CodeRepository codeRepository;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,4 +35,53 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
}
|
}
|
||||||
return EmployeeMapper.convertToDto(employee.get());
|
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("логина не существует или неверный");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,7 +9,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
Loading…
x
Reference in New Issue
Block a user