User register
This commit is contained in:
parent
e7733384ef
commit
a2626980d4
10
pom.xml
10
pom.xml
@ -29,10 +29,7 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
@ -52,6 +49,11 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,23 +1,26 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
|
||||
import com.example.nto.dto.EmployeeDTO;
|
||||
import com.example.nto.dto.RegisterDTO;
|
||||
import com.example.nto.entity.Code;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.service.impl.EmployeeServiceImpl;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/")
|
||||
@RequestMapping("/api")
|
||||
@AllArgsConstructor
|
||||
public class EmployeeController {
|
||||
private EmployeeServiceImpl service;
|
||||
|
||||
|
||||
@GetMapping("{login}/auth")
|
||||
@GetMapping("/{login}/auth")
|
||||
private ResponseEntity<String> getAuth(@PathVariable("login") String login){
|
||||
|
||||
if(login==null||login.trim().isEmpty()){
|
||||
@ -37,7 +40,7 @@ public class EmployeeController {
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("{login}/info")
|
||||
@GetMapping("/{login}/info")
|
||||
private ResponseEntity<Object> getInfo(@PathVariable("login") String login){
|
||||
if(login==null||login.trim().isEmpty()){
|
||||
return ResponseEntity.status(400).body("что-то пошло не так");
|
||||
@ -55,7 +58,7 @@ public class EmployeeController {
|
||||
|
||||
}
|
||||
|
||||
@PatchMapping("{login}/open")
|
||||
@PatchMapping("/{login}/open")
|
||||
private ResponseEntity<Object> updateLocalTime(@PathVariable("login") String login, @RequestBody Code code){
|
||||
if(login==null||login.trim().isEmpty()){
|
||||
return ResponseEntity.status(400).body("что-то пошло не так");
|
||||
@ -66,7 +69,7 @@ public class EmployeeController {
|
||||
|
||||
Employee employee = service.findEmployeeByLogin(login);
|
||||
if(employee!=null){
|
||||
employee.setLastVisit(LocalDateTime.now());
|
||||
employee.setLastVisit(LocalDateTime.now().toString());
|
||||
service.updateLocalTime(employee);
|
||||
|
||||
return ResponseEntity.status(200).body("дверь открылась");
|
||||
@ -76,5 +79,9 @@ public class EmployeeController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/registration")
|
||||
public ResponseEntity<EmployeeDTO> registerEmployee(@RequestBody RegisterDTO dto) {
|
||||
return ResponseEntity.status(HttpStatus.OK).body(service.createEmployee(dto));
|
||||
}
|
||||
|
||||
}
|
||||
|
20
src/main/java/com/example/nto/dto/EmployeeDTO.java
Normal file
20
src/main/java/com/example/nto/dto/EmployeeDTO.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class EmployeeDTO {
|
||||
|
||||
private long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private String name;
|
||||
|
||||
private String photo;
|
||||
|
||||
private String position;
|
||||
|
||||
private String lastVisit;
|
||||
|
||||
}
|
18
src/main/java/com/example/nto/dto/RegisterDTO.java
Normal file
18
src/main/java/com/example/nto/dto/RegisterDTO.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RegisterDTO {
|
||||
|
||||
private String name;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String photo;
|
||||
|
||||
private String position;
|
||||
|
||||
}
|
@ -16,11 +16,15 @@ import java.time.LocalDateTime;
|
||||
@Table(name="employee")
|
||||
public class Employee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "login")
|
||||
private String login;
|
||||
|
||||
@Column(name = "password")
|
||||
private String password;
|
||||
|
||||
@Column(name="name")
|
||||
private String name;
|
||||
|
||||
@ -31,5 +35,5 @@ public class Employee {
|
||||
private String position;
|
||||
|
||||
@Column(name="lastVisit")
|
||||
private LocalDateTime lastVisit;
|
||||
private String lastVisit;
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.example.nto.exceptions;
|
||||
|
||||
public class EmployeeAlreadyExistException extends RuntimeException {
|
||||
public EmployeeAlreadyExistException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.nto.exceptions;
|
||||
|
||||
public class EmployeeNotFoundException extends RuntimeException {
|
||||
public EmployeeNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.example.nto.exceptions.handler;
|
||||
|
||||
import com.example.nto.exceptions.EmployeeAlreadyExistException;
|
||||
import com.example.nto.exceptions.EmployeeNotFoundException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(EmployeeAlreadyExistException.class)
|
||||
public ResponseEntity<String> employeeAlreadyExistExceptionHandler(EmployeeAlreadyExistException e){
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(EmployeeNotFoundException.class)
|
||||
public ResponseEntity<String> employeeNotFoundExceptionHandler(EmployeeNotFoundException e){
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,6 +3,8 @@ package com.example.nto.repository;
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
public Employee findEmployeeByLogin(String login);
|
||||
public Optional<Employee> findEmployeeByLogin(String login);
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.dto.EmployeeDTO;
|
||||
import com.example.nto.dto.RegisterDTO;
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -7,4 +9,5 @@ import org.springframework.stereotype.Service;
|
||||
public interface EmployeeService {
|
||||
public void updateLocalTime(Employee employee);
|
||||
public Employee findEmployeeByLogin(String login);
|
||||
EmployeeDTO createEmployee(RegisterDTO dto);
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.dto.EmployeeDTO;
|
||||
import com.example.nto.dto.RegisterDTO;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.exceptions.EmployeeAlreadyExistException;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import com.example.nto.util.EmployeeMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@ -14,13 +19,35 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
private EmployeeRepository repository;
|
||||
@Override
|
||||
public void updateLocalTime(Employee employee) {
|
||||
employee.setLastVisit(LocalDateTime.now());
|
||||
employee.setLastVisit(LocalDateTime.now().toString());
|
||||
repository.save(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee findEmployeeByLogin(String login) {
|
||||
|
||||
return repository.findEmployeeByLogin(login);
|
||||
return repository.findEmployeeByLogin(login).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmployeeDTO createEmployee(RegisterDTO dto) {
|
||||
|
||||
Optional<Employee> optionalEmployee = repository.findEmployeeByLogin(dto.getUsername());
|
||||
if(optionalEmployee.isPresent()){
|
||||
throw new EmployeeAlreadyExistException("Employee with username " + dto.getUsername() +" already exist");
|
||||
}
|
||||
|
||||
Employee employee = new Employee();
|
||||
employee.setName(dto.getName());
|
||||
employee.setLogin(dto.getUsername());
|
||||
employee.setPassword(dto.getPassword());
|
||||
employee.setPhoto(dto.getPhoto());
|
||||
employee.setPosition(dto.getPosition());
|
||||
employee.setLastVisit(LocalDateTime.now().toString());
|
||||
|
||||
return EmployeeMapper.convertToDTO(repository.save(employee));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
24
src/main/java/com/example/nto/util/EmployeeMapper.java
Normal file
24
src/main/java/com/example/nto/util/EmployeeMapper.java
Normal file
@ -0,0 +1,24 @@
|
||||
package com.example.nto.util;
|
||||
|
||||
import com.example.nto.dto.EmployeeDTO;
|
||||
import com.example.nto.entity.Employee;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
|
||||
@UtilityClass
|
||||
public class EmployeeMapper {
|
||||
|
||||
public EmployeeDTO convertToDTO(Employee employee){
|
||||
EmployeeDTO dto = new EmployeeDTO();
|
||||
dto.setId(employee.getId());
|
||||
dto.setUsername(employee.getLogin());
|
||||
dto.setName(employee.getName());
|
||||
dto.setPhoto(employee.getPhoto());
|
||||
dto.setPosition(employee.getPosition());
|
||||
dto.setLastVisit(employee.getLastVisit());
|
||||
|
||||
return dto;
|
||||
}
|
||||
}
|
@ -1,20 +1,24 @@
|
||||
spring:
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
url: jdbc:postgresql://localhost:5432/testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
#enabled: false
|
||||
enabled: true
|
||||
username: "postgres"
|
||||
password: "MobileDev"
|
||||
|
||||
driver-class-name:
|
||||
org.postgresql.Driver
|
||||
|
||||
jpa:
|
||||
#generate-ddl: false
|
||||
generate-ddl: true
|
||||
|
||||
database-platform:
|
||||
org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
hibernate:
|
||||
#ddl-auto: none
|
||||
ddl-auto: create-drop
|
||||
ddl-auto: create
|
||||
|
||||
# Показываем запросы
|
||||
show-sql: true
|
||||
|
@ -1,9 +1,9 @@
|
||||
INSERT INTO employee (id, login, name, photo, position, last_visit)
|
||||
INSERT INTO employee (id, login, password, name, photo, position, last_visit)
|
||||
VALUES
|
||||
(1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
||||
(2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
||||
(3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
|
||||
(4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36');
|
||||
(1, 'pivanov', 'employee', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
||||
(2, 'ipetrov', 'employee', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
||||
(3, 'asemenov', 'employee', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
|
||||
(4, 'afedorov', 'employee', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36');
|
||||
|
||||
INSERT INTO code (value)
|
||||
VALUES
|
||||
|
Loading…
x
Reference in New Issue
Block a user