- Added Entrance DTO

- Added pagination to GET requests
- Added API docs with Swagger
- Impelemted security
This commit is contained in:
Индекс Зиро 2025-02-19 12:37:26 +03:00
parent 3edb2cf101
commit fb6c4e58d0
11 changed files with 145 additions and 34 deletions

View File

@ -30,6 +30,9 @@ public class SecurityConfig {
.requestMatchers("/v3/api-docs/**").permitAll()
.requestMatchers("/api/employee/login").authenticated()
.requestMatchers("/api/employee/profile").authenticated()
.requestMatchers("/api/employee/open").authenticated()
.requestMatchers("/api/entrance").authenticated()
.requestMatchers("/api/entrance/all").hasAuthority("ADMIN")
.requestMatchers("/api/employee/{login}/delete").hasAuthority("ADMIN")
.requestMatchers("/api/employee/{login}/{state}").hasAuthority("ADMIN")
.requestMatchers("/api/employee/all").hasAuthority("ADMIN")

View File

@ -1,22 +0,0 @@
package com.indexzero.finals.controller;
import com.indexzero.finals.entity.Entrance;
import com.indexzero.finals.repository.EntranceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/entrance")
public class CodeController {
@Autowired
EntranceRepository visitRepository;
@GetMapping
public List<Entrance> getVisits() {
return visitRepository.findAll();
}
}

View File

@ -3,14 +3,15 @@ package com.indexzero.finals.controller;
import com.indexzero.finals.dto.EmployeeDTO;
import com.indexzero.finals.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/employee")
@ -45,8 +46,9 @@ public class EmployeeController {
}
@GetMapping("/all")
public ResponseEntity<List<EmployeeDTO>> getAll() {
return employeeService.getAllEmployees();
public ResponseEntity<Page<EmployeeDTO>> getAll(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
return employeeService.getAllEmployees(pageable);
}
@GetMapping("/{login}")

View File

@ -0,0 +1,32 @@
package com.indexzero.finals.controller;
import com.indexzero.finals.dto.EntranceDTO;
import com.indexzero.finals.service.EntranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/entrance")
public class EntranceController {
@Autowired
EntranceService entranceService;
@GetMapping
public ResponseEntity<Page<EntranceDTO>> getEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
return entranceService.getEmployeeEntrances(pageable, SecurityContextHolder.getContext().getAuthentication());
}
@GetMapping("/all")
public ResponseEntity<Page<EntranceDTO>> getAllEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
return entranceService.getAllEntrances(pageable);
}
}

View File

@ -0,0 +1,14 @@
package com.indexzero.finals.dto;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class EntranceDTO {
private long id;
private String employeeLogin;
private LocalDateTime entryTime;
private String readerName;
private String type;
}

View File

@ -42,7 +42,7 @@ public class Employee implements UserDetails {
@ManyToMany(fetch = FetchType.EAGER)
Set<Authority> authorities;
@OneToMany(mappedBy = "id", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
List<Entrance> entrances;
@Override

View File

@ -2,18 +2,18 @@ package com.indexzero.finals.service;
import com.indexzero.finals.dto.EmployeeDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import java.util.List;
public interface EmployeeService {
ResponseEntity<Object> checkIfUserExists(String login);
ResponseEntity<EmployeeDTO> getUserInfo(Authentication auth);
ResponseEntity<Object> openTheDoor(Long code, Authentication auth);
ResponseEntity<HttpStatusCode> deleteEmployee(String login);
ResponseEntity<HttpStatusCode> changeState(String login, String state);
ResponseEntity<List<EmployeeDTO>> getAllEmployees();
ResponseEntity<Page<EmployeeDTO>> getAllEmployees(Pageable pageable);
ResponseEntity<EmployeeDTO> getEmployeeByLogin(String login);
}

View File

@ -0,0 +1,13 @@
package com.indexzero.finals.service;
import com.indexzero.finals.dto.EntranceDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
public interface EntranceService {
ResponseEntity<Page<EntranceDTO>> getEmployeeEntrances(Pageable pageable, Authentication auth);
ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable);
}

View File

@ -9,6 +9,8 @@ import com.indexzero.finals.repository.EntranceRepository;
import com.indexzero.finals.service.EmployeeService;
import com.indexzero.finals.util.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
@ -16,9 +18,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@ -122,8 +123,8 @@ public class EmployeeServiceImpl implements EmployeeService {
}
@Override
public ResponseEntity<List<EmployeeDTO>> getAllEmployees() {
return new ResponseEntity<>(employeeRepository.findAll().stream().map(EmployeeMapper::convertToDTO).collect(Collectors.toList()), HttpStatus.OK);
public ResponseEntity<Page<EmployeeDTO>> getAllEmployees(Pageable pageable) {
return new ResponseEntity<>(employeeRepository.findAll(pageable).map(EmployeeMapper::convertToDTO), HttpStatus.OK);
}
@Override

View File

@ -0,0 +1,48 @@
package com.indexzero.finals.service.impl;
import com.indexzero.finals.dto.EntranceDTO;
import com.indexzero.finals.entity.Employee;
import com.indexzero.finals.entity.Entrance;
import com.indexzero.finals.repository.EmployeeRepository;
import com.indexzero.finals.repository.EntranceRepository;
import com.indexzero.finals.service.EntranceService;
import com.indexzero.finals.util.EntranceMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class EntranceServiceImpl implements EntranceService {
private final EmployeeRepository employeeRepository;
private final EntranceRepository entranceRepository;
public EntranceServiceImpl(EmployeeRepository employeeRepository, EntranceRepository entranceRepository) {
this.employeeRepository = employeeRepository;
this.entranceRepository = entranceRepository;
}
@Override
public ResponseEntity<Page<EntranceDTO>> getEmployeeEntrances(Pageable pageable, Authentication auth) {
Employee employee = employeeRepository.findByLogin(auth.getName());
System.out.println(employee.getEntrances().stream().map(EntranceMapper::convertToDTO).collect(Collectors.toList()));
List<EntranceDTO> entrances = employee.getEntrances().stream().map(EntranceMapper::convertToDTO).collect(Collectors.toList());
Page<EntranceDTO> page = new PageImpl<>(entrances, pageable, entrances.size());
return new ResponseEntity<>(page, HttpStatus.OK);
}
@Override
public ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable) {
List<Entrance> entrances = entranceRepository.findAll();
List<EntranceDTO> entrancesdto = entrances.stream().map(EntranceMapper::convertToDTO).collect(Collectors.toList());
Page<EntranceDTO> page = new PageImpl<>(entrancesdto, pageable, entrances.size());
return new ResponseEntity<>(page, HttpStatus.OK);
}
}

View File

@ -0,0 +1,20 @@
package com.indexzero.finals.util;
import com.indexzero.finals.dto.EntranceDTO;
import com.indexzero.finals.entity.Entrance;
import lombok.experimental.UtilityClass;
@UtilityClass
public class EntranceMapper {
public static EntranceDTO convertToDTO(Entrance entrance) {
EntranceDTO entranceDTO = new EntranceDTO();
entranceDTO.setId(entrance.getId());
entranceDTO.setEmployeeLogin(entrance.getEmployee().getLogin());
entranceDTO.setEntryTime(entrance.getVisitTime());
entranceDTO.setReaderName(entrance.getReader().getName());
entranceDTO.setType(entrance.getType());
return entranceDTO;
}
}