Добавление фиксирования входа пользователя
This commit is contained in:
parent
7dc4d1e77e
commit
659306712d
@ -1,14 +1,19 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.example.nto.dto.EmployeeDTO;
|
||||||
|
import com.example.nto.dto.EnterDTO;
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.EnterType;
|
||||||
import com.example.nto.service.impl.EmployeeServiceImpl;
|
import com.example.nto.service.impl.EmployeeServiceImpl;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
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.time.LocalDateTime;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
@ -17,17 +22,17 @@ public class EmployeeController {
|
|||||||
private final EmployeeServiceImpl employeeService;
|
private final EmployeeServiceImpl employeeService;
|
||||||
|
|
||||||
@GetMapping("/login")
|
@GetMapping("/login")
|
||||||
public ResponseEntity<Employee> login(Authentication authentication){
|
public ResponseEntity<EmployeeDTO> login(Authentication authentication){
|
||||||
return ResponseEntity.ok(employeeService.getEmployeeByUsername(authentication.getName()));
|
return ResponseEntity.ok(employeeService.getEmployeeByUsername(authentication.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/info/{login}")
|
@GetMapping("/info/{login}")
|
||||||
public Employee info(@PathVariable("login") String username){
|
public ResponseEntity<EmployeeDTO> info(@PathVariable("login") String username){
|
||||||
return employeeService.getInfo(username);
|
return ResponseEntity.ok(employeeService.getInfo(username));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/open/{login}")
|
@PostMapping("/add/{login}")
|
||||||
public String open(@PathVariable("login") String login, @RequestBody Code value){
|
public ResponseEntity<EnterDTO> add(@PathVariable("login") String login, @RequestBody EnterDTO enterDTO){
|
||||||
return employeeService.patchOpen(login, value.getValue());
|
return ResponseEntity.ok(employeeService.addEnter(login, enterDTO));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,14 @@ package com.example.nto.dto;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class EmployeeDTO {
|
public class EmployeeDTO {
|
||||||
|
private long id;
|
||||||
private String username;
|
private String username;
|
||||||
private String name;
|
private String name;
|
||||||
private String password;
|
private String photoUrl;
|
||||||
private String lastVisit;
|
private String position;
|
||||||
|
private LocalDateTime lastVisit;
|
||||||
}
|
}
|
||||||
|
12
src/main/java/com/example/nto/dto/EnterDTO.java
Normal file
12
src/main/java/com/example/nto/dto/EnterDTO.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.example.nto.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class EnterDTO {
|
||||||
|
private long value;
|
||||||
|
private String type;
|
||||||
|
private LocalDateTime time;
|
||||||
|
}
|
@ -8,7 +8,7 @@ import javax.persistence.*;
|
|||||||
@Entity
|
@Entity
|
||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@NoArgsConstructor
|
||||||
public class Code {
|
public class Code {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@ -39,7 +39,7 @@ public class Employee implements UserDetails {
|
|||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name="enter_type_id")
|
@JoinColumn(name="enter_type_id")
|
||||||
private EnterType typeId;
|
private EnterType type;
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
private Set<Authority> authorities;
|
private Set<Authority> authorities;
|
||||||
|
29
src/main/java/com/example/nto/entity/Enter.java
Normal file
29
src/main/java/com/example/nto/entity/Enter.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
@Table(name="enters")
|
||||||
|
public class Enter {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name="employee_username")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Column(name="value")
|
||||||
|
private long value;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name="enter_type_id")
|
||||||
|
private EnterType type;
|
||||||
|
|
||||||
|
@Column(name="time")
|
||||||
|
private LocalDateTime time;
|
||||||
|
|
||||||
|
}
|
@ -11,6 +11,6 @@ public class EnterType {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@Column(name = "id")
|
@Column(name = "id")
|
||||||
private long id;
|
private long id;
|
||||||
@Column(name = "enter_type")
|
@Column(name = "types")
|
||||||
private String type;
|
private String type;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class IncorrectTypeException extends RuntimeException {
|
||||||
|
public IncorrectTypeException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.example.nto.exception.handler;
|
|||||||
import com.example.nto.exception.EmployeeNotFoundException;
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
import com.example.nto.exception.IncorrectCodeException;
|
import com.example.nto.exception.IncorrectCodeException;
|
||||||
import com.example.nto.exception.IncorrectPasswordException;
|
import com.example.nto.exception.IncorrectPasswordException;
|
||||||
|
import com.example.nto.exception.IncorrectTypeException;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
@ -17,11 +18,11 @@ public class GlobalExceptionHandler {
|
|||||||
|
|
||||||
@ExceptionHandler
|
@ExceptionHandler
|
||||||
public ResponseEntity<String> handleIncorrectCodeException(IncorrectCodeException e) {
|
public ResponseEntity<String> handleIncorrectCodeException(IncorrectCodeException e) {
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.valueOf(430));
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(IncorrectPasswordException.class)
|
@ExceptionHandler(IncorrectTypeException.class)
|
||||||
public ResponseEntity<String> handlerIncorrectPasswordException(IncorrectPasswordException e) {
|
public ResponseEntity<String> handlerIncorrectTypeException(IncorrectTypeException e) {
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@ import com.example.nto.entity.Code;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface CodeRepository extends JpaRepository<Code, Long> {
|
public interface CodeRepository extends JpaRepository<Code, Long> {
|
||||||
Code findByValue(long value);
|
Optional<Code> findByValue(long value);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Enter;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface EnterRepository extends JpaRepository<Enter, Long> {
|
||||||
|
}
|
@ -2,6 +2,11 @@ package com.example.nto.repository;
|
|||||||
|
|
||||||
import com.example.nto.entity.EnterType;
|
import com.example.nto.entity.EnterType;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface EnterTypeRepository extends JpaRepository<EnterType, Long> {
|
public interface EnterTypeRepository extends JpaRepository<EnterType, Long> {
|
||||||
|
Optional<EnterType> findByType(String type);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.dto.EmployeeDTO;
|
||||||
|
import com.example.nto.dto.EnterDTO;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.EnterType;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
Employee getEmployeeByUsername(String username);
|
EmployeeDTO getEmployeeByUsername(String username);
|
||||||
Employee getInfo(String username);
|
EmployeeDTO getInfo(String username);
|
||||||
String patchOpen(String username, long value);
|
EnterDTO addEnter(String username, EnterDTO enterDTO);
|
||||||
|
List<EnterDTO> getAllEmployeeEnters();
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,29 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.dto.EmployeeDTO;
|
||||||
|
import com.example.nto.dto.EnterDTO;
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.Enter;
|
||||||
|
import com.example.nto.entity.EnterType;
|
||||||
import com.example.nto.exception.EmployeeNotFoundException;
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
|
import com.example.nto.exception.IncorrectCodeException;
|
||||||
|
import com.example.nto.exception.IncorrectTypeException;
|
||||||
import com.example.nto.repository.CodeRepository;
|
import com.example.nto.repository.CodeRepository;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
|
import com.example.nto.repository.EnterRepository;
|
||||||
|
import com.example.nto.repository.EnterTypeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import com.example.nto.util.EmployeeMapper;
|
||||||
|
import com.example.nto.util.EnterMapper;
|
||||||
|
import liquibase.pro.packaged.E;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.springframework.util.Assert.hasText;
|
import static org.springframework.util.Assert.hasText;
|
||||||
@ -21,30 +34,56 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
|
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
private final CodeRepository codeRepository;
|
private final CodeRepository codeRepository;
|
||||||
|
private final EnterTypeRepository enterTypeRepository;
|
||||||
|
private final EnterRepository enterRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Employee getEmployeeByUsername(String username) {
|
public EmployeeDTO getEmployeeByUsername(String username) {
|
||||||
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
|
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
|
||||||
|
|
||||||
if(optionalEmployee.isEmpty())
|
if(optionalEmployee.isEmpty())
|
||||||
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
|
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
|
||||||
|
|
||||||
return optionalEmployee.get();
|
return EmployeeMapper.convertToDTO(optionalEmployee.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Employee getInfo(String username) {
|
public EmployeeDTO getInfo(String username) {
|
||||||
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
|
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
|
||||||
if(optionalEmployee.isEmpty()) {
|
if(optionalEmployee.isEmpty()) {
|
||||||
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
|
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return optionalEmployee.get();
|
return EmployeeMapper.convertToDTO(optionalEmployee.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String patchOpen(String username, long value) {
|
public EnterDTO addEnter(String username, EnterDTO enterDTO) {
|
||||||
|
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
|
||||||
|
if(optionalEmployee.isEmpty())
|
||||||
|
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
|
||||||
|
Optional<Code> optionalCode = codeRepository.findByValue(enterDTO.getValue());
|
||||||
|
if(optionalCode.isEmpty())
|
||||||
|
throw new IncorrectCodeException("Incorrect code");
|
||||||
|
|
||||||
return ("Дверь открыта");
|
Optional<EnterType> optionalEnterType = enterTypeRepository.findByType(enterDTO.getType());
|
||||||
|
if(optionalEnterType.isEmpty())
|
||||||
|
throw new IncorrectTypeException("Incorrect type");
|
||||||
|
|
||||||
|
Enter enter = new Enter();
|
||||||
|
enter.setType(optionalEnterType.get());
|
||||||
|
enter.setTime(enterDTO.getTime());
|
||||||
|
enter.setValue(enterDTO.getValue());
|
||||||
|
enter.setUsername(username);
|
||||||
|
|
||||||
|
|
||||||
|
return EnterMapper.convertToDTO(enterRepository.save(enter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<EnterDTO> getAllEmployeeEnters() {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ import lombok.experimental.UtilityClass;
|
|||||||
public class EmployeeMapper {
|
public class EmployeeMapper {
|
||||||
public EmployeeDTO convertToDTO(Employee employee){
|
public EmployeeDTO convertToDTO(Employee employee){
|
||||||
EmployeeDTO employeeDTO = new EmployeeDTO();
|
EmployeeDTO employeeDTO = new EmployeeDTO();
|
||||||
|
employeeDTO.setId(employee.getId());
|
||||||
employeeDTO.setUsername(employee.getUsername());
|
employeeDTO.setUsername(employee.getUsername());
|
||||||
employeeDTO.setPassword(employee.getPassword());
|
|
||||||
employeeDTO.setName(employee.getName());
|
employeeDTO.setName(employee.getName());
|
||||||
|
employeeDTO.setPosition(employee.getPosition());
|
||||||
|
employeeDTO.setPhotoUrl(employee.getPhotoUrl());
|
||||||
|
employeeDTO.setLastVisit(employee.getLastVisit());
|
||||||
|
|
||||||
return employeeDTO;
|
return employeeDTO;
|
||||||
}
|
}
|
||||||
|
17
src/main/java/com/example/nto/util/EnterMapper.java
Normal file
17
src/main/java/com/example/nto/util/EnterMapper.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.example.nto.util;
|
||||||
|
|
||||||
|
import com.example.nto.dto.EnterDTO;
|
||||||
|
import com.example.nto.entity.Enter;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class EnterMapper {
|
||||||
|
public EnterDTO convertToDTO(Enter enter){
|
||||||
|
EnterDTO enterDTO = new EnterDTO();
|
||||||
|
enterDTO.setTime(enter.getTime());
|
||||||
|
enterDTO.setValue(enter.getValue());
|
||||||
|
enterDTO.setType(enter.getType().getType());
|
||||||
|
|
||||||
|
return enterDTO;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
|
||||||
|
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd
|
||||||
|
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog-ext.xsd">
|
||||||
|
|
||||||
|
<changeSet id="2025-02-19-0005-enters" author="dshilyaev">
|
||||||
|
<preConditions onFail="MARK_RAN">
|
||||||
|
<not>
|
||||||
|
<tableExists tableName="enters"/>
|
||||||
|
</not>
|
||||||
|
</preConditions>
|
||||||
|
|
||||||
|
<createTable tableName="enters">
|
||||||
|
<column name="id" type="BIGINT" autoIncrement="true" >
|
||||||
|
<constraints primaryKey="true" nullable="false"/>
|
||||||
|
</column>
|
||||||
|
<column name="employee_username" type="VARCHAR(100)">
|
||||||
|
<constraints nullable="false"/>
|
||||||
|
</column>
|
||||||
|
<column name="value" type="BIGINT">
|
||||||
|
<constraints nullable="false"/>
|
||||||
|
</column>
|
||||||
|
<column name="enter_type_id" type="BIGINT">
|
||||||
|
<constraints foreignKeyName="fk_enters_enter_type" referencedTableName="enter_type" referencedColumnNames="id"/>
|
||||||
|
</column>
|
||||||
|
<column name="time" type="DATETIME()">
|
||||||
|
<constraints nullable="false"/>
|
||||||
|
</column>
|
||||||
|
|
||||||
|
</createTable>
|
||||||
|
|
||||||
|
</changeSet>
|
||||||
|
|
||||||
|
|
||||||
|
</databaseChangeLog>
|
@ -19,6 +19,8 @@
|
|||||||
<include file="db.changelog/1.0/1.0/2025-02-18-0004-employee-authority.xml"/>
|
<include file="db.changelog/1.0/1.0/2025-02-18-0004-employee-authority.xml"/>
|
||||||
<include file="db.changelog/1.0/data/2025-02-18-0004-employee-authority-data.xml"/>
|
<include file="db.changelog/1.0/data/2025-02-18-0004-employee-authority-data.xml"/>
|
||||||
|
|
||||||
|
<include file="db.changelog/1.0/1.0/2025-02-19-0006-enters.xml"/>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user