add entry db (history)
This commit is contained in:
parent
4529f95264
commit
a8480602c9
@ -3,6 +3,7 @@ package com.example.nto.controller;
|
|||||||
import com.example.nto.model.dto.EmployeeDTO;
|
import com.example.nto.model.dto.EmployeeDTO;
|
||||||
import com.example.nto.model.entity.Code;
|
import com.example.nto.model.entity.Code;
|
||||||
import com.example.nto.model.entity.Employee;
|
import com.example.nto.model.entity.Employee;
|
||||||
|
import com.example.nto.model.entity.Entry;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@ -13,6 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
@ -26,6 +28,13 @@ public class EmployeeController {
|
|||||||
return new ResponseEntity<>(null, HttpStatus.OK);
|
return new ResponseEntity<>(null, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/entries")
|
||||||
|
public List<Entry> getAllEntriesByLogin(@RequestParam String login) {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String recipientLogin = authentication.getName();
|
||||||
|
return employeeService.getAllEntriesByLogin(login, recipientLogin);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
public EmployeeDTO info(@RequestParam final String login) {
|
public EmployeeDTO info(@RequestParam final String login) {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
@ -34,8 +43,8 @@ public class EmployeeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/open")
|
@PatchMapping("/open")
|
||||||
public void open(@RequestParam final String login, @RequestBody final Code code) {
|
public void open(@RequestBody final Entry entry) {
|
||||||
employeeService.updateVisit(login, code.getValue());
|
employeeService.updateVisit(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||||
|
6
src/main/java/com/example/nto/model/EntryType.java
Normal file
6
src/main/java/com/example/nto/model/EntryType.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package com.example.nto.model;
|
||||||
|
|
||||||
|
public enum EntryType {
|
||||||
|
PHONE,
|
||||||
|
CARD
|
||||||
|
}
|
31
src/main/java/com/example/nto/model/entity/Entry.java
Normal file
31
src/main/java/com/example/nto/model/entity/Entry.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.nto.model.entity;
|
||||||
|
|
||||||
|
import com.example.nto.model.EntryType;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity(name = "entry")
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Entry {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private long value;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private EntryType type;
|
||||||
|
|
||||||
|
private LocalDateTime dateTime;
|
||||||
|
|
||||||
|
private String login;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.model.entity.Employee;
|
||||||
|
import com.example.nto.model.entity.Entry;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface EntryRepository extends JpaRepository<Entry, Long> {
|
||||||
|
|
||||||
|
List<Entry> findAllEntryByLogin(final String login);
|
||||||
|
|
||||||
|
}
|
@ -2,17 +2,25 @@ package com.example.nto.service;
|
|||||||
|
|
||||||
import com.example.nto.model.dto.EmployeeDTO;
|
import com.example.nto.model.dto.EmployeeDTO;
|
||||||
import com.example.nto.model.entity.Employee;
|
import com.example.nto.model.entity.Employee;
|
||||||
|
import com.example.nto.model.entity.Entry;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
|
|
||||||
EmployeeDTO getEmployeeDTO(String login, final String recipientLogin);
|
EmployeeDTO getEmployeeDTO(String login, String recipientLogin);
|
||||||
|
|
||||||
void updateVisit(String login, long value);
|
void updateVisit(Entry entry);
|
||||||
|
|
||||||
void addEmployee(Employee employee);
|
void addEmployee(Employee employee);
|
||||||
|
|
||||||
void banEmployee(String login);
|
void banEmployee(String login);
|
||||||
|
|
||||||
void unbanEmployee(String login);
|
void unbanEmployee(String login);
|
||||||
|
|
||||||
|
void addEntry(Entry entry);
|
||||||
|
|
||||||
|
List<Entry> getAllEntriesByLogin(String login, final String recipientLogin);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,10 @@ import com.example.nto.mapper.EmployeeDTOMapper;
|
|||||||
import com.example.nto.model.EmployeeRoleType;
|
import com.example.nto.model.EmployeeRoleType;
|
||||||
import com.example.nto.model.dto.EmployeeDTO;
|
import com.example.nto.model.dto.EmployeeDTO;
|
||||||
import com.example.nto.model.entity.Employee;
|
import com.example.nto.model.entity.Employee;
|
||||||
|
import com.example.nto.model.entity.Entry;
|
||||||
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.EntryRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import com.example.nto.service.exception.CodeNotFoundException;
|
import com.example.nto.service.exception.CodeNotFoundException;
|
||||||
import com.example.nto.service.exception.EmployeeBannedException;
|
import com.example.nto.service.exception.EmployeeBannedException;
|
||||||
@ -15,7 +17,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -30,6 +32,8 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
|
|
||||||
private final EmployeeDTOMapper employeeDTOMapper;
|
private final EmployeeDTOMapper employeeDTOMapper;
|
||||||
|
|
||||||
|
private final EntryRepository entryRepository;
|
||||||
|
|
||||||
private void employeeExists(final String login) {
|
private void employeeExists(final String login) {
|
||||||
if (!employeeRepository.existsByLogin(login)) {
|
if (!employeeRepository.existsByLogin(login)) {
|
||||||
throw new EmployeeNotFoundException();
|
throw new EmployeeNotFoundException();
|
||||||
@ -39,14 +43,13 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
@Override
|
@Override
|
||||||
public EmployeeDTO getEmployeeDTO(final String login, final String recipientLogin) {
|
public EmployeeDTO getEmployeeDTO(final String login, final String recipientLogin) {
|
||||||
employeeExists(login);
|
employeeExists(login);
|
||||||
var e = employeeRepository.findEmployeeByLogin(login);
|
if (!Objects.equals(login, recipientLogin)) {
|
||||||
if (!Objects.equals(e.getLogin(), recipientLogin)) {
|
|
||||||
var recipient = getEmployee(recipientLogin);
|
var recipient = getEmployee(recipientLogin);
|
||||||
if (recipient.getRole() != EmployeeRoleType.ADMIN) {
|
if (recipient.getRole() != EmployeeRoleType.ADMIN) {
|
||||||
throw new NotAnAdminException();
|
throw new NotAnAdminException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return employeeDTOMapper.map(e);
|
return employeeDTOMapper.map(employeeRepository.findEmployeeByLogin(login));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Employee getEmployee(final String login) {
|
private Employee getEmployee(final String login) {
|
||||||
@ -55,17 +58,18 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateVisit(final String login, final long value) {
|
public void updateVisit(Entry entry) {
|
||||||
employeeExists(login);
|
employeeExists(entry.getLogin());
|
||||||
if (!codeRepository.existsByValue(value)) {
|
if (!codeRepository.existsByValue(entry.getValue())) {
|
||||||
throw new CodeNotFoundException();
|
throw new CodeNotFoundException();
|
||||||
}
|
}
|
||||||
final Employee employee = getEmployee(login);
|
final Employee employee = getEmployee(entry.getLogin());
|
||||||
if (employee.getIsBanned()) {
|
if (employee.getIsBanned()) {
|
||||||
throw new EmployeeBannedException();
|
throw new EmployeeBannedException();
|
||||||
}
|
}
|
||||||
employee.setLastVisit(LocalDateTime.now());
|
employee.setLastVisit(entry.getDateTime());
|
||||||
employeeRepository.save(employee);
|
employeeRepository.save(employee);
|
||||||
|
addEntry(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -89,4 +93,21 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
employee.setIsBanned(false);
|
employee.setIsBanned(false);
|
||||||
employeeRepository.save(employee);
|
employeeRepository.save(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addEntry(Entry entry) {
|
||||||
|
entryRepository.save(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Entry> getAllEntriesByLogin(String login, String recipientLogin) {
|
||||||
|
employeeExists(login);
|
||||||
|
if (!Objects.equals(login, recipientLogin)) {
|
||||||
|
var recipient = getEmployee(recipientLogin);
|
||||||
|
if (recipient.getRole() != EmployeeRoleType.ADMIN) {
|
||||||
|
throw new NotAnAdminException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return entryRepository.findAllEntryByLogin(login);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,7 @@ VALUES
|
|||||||
(1122334455667788990),
|
(1122334455667788990),
|
||||||
(998877665544332211),
|
(998877665544332211),
|
||||||
(5566778899001122334);
|
(5566778899001122334);
|
||||||
|
|
||||||
|
INSERT INTO entry (value, login, type, date_time)
|
||||||
|
VALUES
|
||||||
|
(1234567890123456789, 'pivanov', 'PHONE', '2024-02-13T08:35');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user