Добавление фиксирования входа пользователя

This commit is contained in:
Shilyaev_Dmitry 2025-02-19 17:33:36 +03:00
parent 7dc4d1e77e
commit 659306712d
18 changed files with 203 additions and 25 deletions

View File

@ -1,14 +1,19 @@
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.Employee;
import com.example.nto.entity.EnterType;
import com.example.nto.service.impl.EmployeeServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
@ -17,17 +22,17 @@ public class EmployeeController {
private final EmployeeServiceImpl employeeService;
@GetMapping("/login")
public ResponseEntity<Employee> login(Authentication authentication){
public ResponseEntity<EmployeeDTO> login(Authentication authentication){
return ResponseEntity.ok(employeeService.getEmployeeByUsername(authentication.getName()));
}
@GetMapping("/info/{login}")
public Employee info(@PathVariable("login") String username){
return employeeService.getInfo(username);
public ResponseEntity<EmployeeDTO> info(@PathVariable("login") String username){
return ResponseEntity.ok(employeeService.getInfo(username));
}
@PatchMapping("/open/{login}")
public String open(@PathVariable("login") String login, @RequestBody Code value){
return employeeService.patchOpen(login, value.getValue());
@PostMapping("/add/{login}")
public ResponseEntity<EnterDTO> add(@PathVariable("login") String login, @RequestBody EnterDTO enterDTO){
return ResponseEntity.ok(employeeService.addEnter(login, enterDTO));
}
}

View File

@ -2,10 +2,14 @@ package com.example.nto.dto;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class EmployeeDTO {
private long id;
private String username;
private String name;
private String password;
private String lastVisit;
private String photoUrl;
private String position;
private LocalDateTime lastVisit;
}

View 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;
}

View File

@ -8,7 +8,7 @@ import javax.persistence.*;
@Entity
@Builder
@AllArgsConstructor
@Getter
@NoArgsConstructor
public class Code {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -39,7 +39,7 @@ public class Employee implements UserDetails {
@ManyToOne
@JoinColumn(name="enter_type_id")
private EnterType typeId;
private EnterType type;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Authority> authorities;

View 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;
}

View File

@ -11,6 +11,6 @@ public class EnterType {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "enter_type")
@Column(name = "types")
private String type;
}

View File

@ -0,0 +1,7 @@
package com.example.nto.exception;
public class IncorrectTypeException extends RuntimeException {
public IncorrectTypeException(String message) {
super(message);
}
}

View File

@ -3,6 +3,7 @@ package com.example.nto.exception.handler;
import com.example.nto.exception.EmployeeNotFoundException;
import com.example.nto.exception.IncorrectCodeException;
import com.example.nto.exception.IncorrectPasswordException;
import com.example.nto.exception.IncorrectTypeException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ -17,11 +18,11 @@ public class GlobalExceptionHandler {
@ExceptionHandler
public ResponseEntity<String> handleIncorrectCodeException(IncorrectCodeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.valueOf(430));
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
}
@ExceptionHandler(IncorrectPasswordException.class)
public ResponseEntity<String> handlerIncorrectPasswordException(IncorrectPasswordException e) {
@ExceptionHandler(IncorrectTypeException.class)
public ResponseEntity<String> handlerIncorrectTypeException(IncorrectTypeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
}
}

View File

@ -4,8 +4,10 @@ import com.example.nto.entity.Code;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface CodeRepository extends JpaRepository<Code, Long> {
Code findByValue(long value);
Optional<Code> findByValue(long value);
}

View File

@ -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> {
}

View File

@ -2,6 +2,11 @@ package com.example.nto.repository;
import com.example.nto.entity.EnterType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface EnterTypeRepository extends JpaRepository<EnterType, Long> {
Optional<EnterType> findByType(String type);
}

View File

@ -1,9 +1,16 @@
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.EnterType;
import java.time.LocalDateTime;
import java.util.List;
public interface EmployeeService {
Employee getEmployeeByUsername(String username);
Employee getInfo(String username);
String patchOpen(String username, long value);
EmployeeDTO getEmployeeByUsername(String username);
EmployeeDTO getInfo(String username);
EnterDTO addEnter(String username, EnterDTO enterDTO);
List<EnterDTO> getAllEmployeeEnters();
}

View File

@ -1,16 +1,29 @@
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.Employee;
import com.example.nto.entity.Enter;
import com.example.nto.entity.EnterType;
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.EmployeeRepository;
import com.example.nto.repository.EnterRepository;
import com.example.nto.repository.EnterTypeRepository;
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 org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import static org.springframework.util.Assert.hasText;
@ -21,30 +34,56 @@ public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
private final CodeRepository codeRepository;
private final EnterTypeRepository enterTypeRepository;
private final EnterRepository enterRepository;
@Override
public Employee getEmployeeByUsername(String username) {
public EmployeeDTO getEmployeeByUsername(String username) {
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
if(optionalEmployee.isEmpty())
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
return optionalEmployee.get();
return EmployeeMapper.convertToDTO(optionalEmployee.get());
}
@Override
public Employee getInfo(String username) {
public EmployeeDTO getInfo(String username) {
Optional<Employee> optionalEmployee = employeeRepository.findByUsername(username);
if(optionalEmployee.isEmpty()) {
throw new EmployeeNotFoundException("Employee with username: " + username + " not found");
}
return optionalEmployee.get();
return EmployeeMapper.convertToDTO(optionalEmployee.get());
}
@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();
}
}

View File

@ -8,9 +8,12 @@ import lombok.experimental.UtilityClass;
public class EmployeeMapper {
public EmployeeDTO convertToDTO(Employee employee){
EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setId(employee.getId());
employeeDTO.setUsername(employee.getUsername());
employeeDTO.setPassword(employee.getPassword());
employeeDTO.setName(employee.getName());
employeeDTO.setPosition(employee.getPosition());
employeeDTO.setPhotoUrl(employee.getPhotoUrl());
employeeDTO.setLastVisit(employee.getLastVisit());
return employeeDTO;
}

View 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;
}
}

View File

@ -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>

View File

@ -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/data/2025-02-18-0004-employee-authority-data.xml"/>
<include file="db.changelog/1.0/1.0/2025-02-19-0006-enters.xml"/>