- Added last entrance request

- Added EntranceController documentation annotations for swagger
- Changed QR blocking functionality
This commit is contained in:
Индекс Зиро 2025-02-19 14:21:12 +03:00
parent 336bb589e7
commit 06aae07afd
8 changed files with 41 additions and 9 deletions

View File

@ -2,6 +2,9 @@ package com.indexzero.finals.controller;
import com.indexzero.finals.dto.EntranceDTO; import com.indexzero.finals.dto.EntranceDTO;
import com.indexzero.finals.service.EntranceService; import com.indexzero.finals.service.EntranceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -18,12 +21,32 @@ public class EntranceController {
@GetMapping @GetMapping
@Operation(description = "Get all entrances of a user with pagination. Username is taken from Authentication", summary = "Get all entrances of a user")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request Successful."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
})
public ResponseEntity<Page<EntranceDTO>> getEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { public ResponseEntity<Page<EntranceDTO>> getEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size); Pageable pageable = PageRequest.of(page, size);
return entranceService.getEmployeeEntrances(pageable, SecurityContextHolder.getContext().getAuthentication()); return entranceService.getEmployeeEntrances(pageable, SecurityContextHolder.getContext().getAuthentication());
} }
@GetMapping("/last")
@Operation(description = "Get user's last entrance. Username is taken from Authentication", summary = "Get user's last entrance")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request Successful."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
})
public ResponseEntity<EntranceDTO> getLastEntrance() {
return entranceService.getLastEntrance(SecurityContextHolder.getContext().getAuthentication());
}
@GetMapping("/all") @GetMapping("/all")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request Successful."),
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "403", description = "Forbidden"),
})
@Operation(description = "Get all entrances of all users (ADMIN only)", summary = "Get all entrances")
public ResponseEntity<Page<EntranceDTO>> getAllEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { public ResponseEntity<Page<EntranceDTO>> getAllEntrances(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size); Pageable pageable = PageRequest.of(page, size);
return entranceService.getAllEntrances(pageable); return entranceService.getAllEntrances(pageable);

View File

@ -1,5 +1,6 @@
package com.indexzero.finals.dto; package com.indexzero.finals.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
@Data @Data
@ -10,4 +11,6 @@ public class EmployeeDTO {
private String authority; private String authority;
private String position; private String position;
private String photoUrl; private String photoUrl;
@JsonProperty("qrEnabled")
private boolean isQREnabled;
} }

View File

@ -37,7 +37,7 @@ public class Employee implements UserDetails {
private String photoUrl; private String photoUrl;
@Column(name = "is_enabled") @Column(name = "is_enabled")
Boolean isEmpEnabled; Boolean isQREnabled;
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
Set<Authority> authorities; Set<Authority> authorities;
@ -50,8 +50,4 @@ public class Employee implements UserDetails {
return this.login; return this.login;
} }
@Override
public boolean isEnabled() {
return isEmpEnabled;
}
} }

View File

@ -8,6 +8,8 @@ import org.springframework.security.core.Authentication;
public interface EntranceService { public interface EntranceService {
ResponseEntity<Page<EntranceDTO>> getEmployeeEntrances(Pageable pageable, Authentication auth); ResponseEntity<Page<EntranceDTO>> getEmployeeEntrances(Pageable pageable, Authentication auth);
ResponseEntity<EntranceDTO> getLastEntrance(Authentication auth);
ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable); ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable);
} }

View File

@ -50,7 +50,6 @@ public class EmployeeServiceImpl implements EmployeeService {
entranceRepository.save(entrance); entranceRepository.save(entrance);
// employeeRepository.save(employee);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@ -90,12 +89,12 @@ public class EmployeeServiceImpl implements EmployeeService {
} }
else { else {
if(state.equals("active")) { if(state.equals("active")) {
e.setIsEmpEnabled(true); e.setIsQREnabled(true);
employeeRepository.save(e); employeeRepository.save(e);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
else if(state.equals("blocked")) { else if(state.equals("blocked")) {
e.setIsEmpEnabled(false); e.setIsQREnabled(false);
employeeRepository.save(e); employeeRepository.save(e);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }

View File

@ -37,6 +37,14 @@ public class EntranceServiceImpl implements EntranceService {
return new ResponseEntity<>(page, HttpStatus.OK); return new ResponseEntity<>(page, HttpStatus.OK);
} }
@Override
public ResponseEntity<EntranceDTO> getLastEntrance(Authentication auth) {
Employee employee = employeeRepository.findByLogin(auth.getName());
List<EntranceDTO> entrances = employee.getEntrances().stream().map(EntranceMapper::convertToDTO).collect(Collectors.toList());
System.out.println(entrances.getLast());
return new ResponseEntity<>(entrances.getLast(), HttpStatus.OK);
}
@Override @Override
public ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable) { public ResponseEntity<Page<EntranceDTO>> getAllEntrances(Pageable pageable) {
List<Entrance> entrances = entranceRepository.findAll(); List<Entrance> entrances = entranceRepository.findAll();

View File

@ -14,6 +14,7 @@ public class EmployeeMapper {
employeeDTO.setName(user.getName()); employeeDTO.setName(user.getName());
employeeDTO.setPosition(user.getPosition()); employeeDTO.setPosition(user.getPosition());
employeeDTO.setPhotoUrl(user.getPhotoUrl()); employeeDTO.setPhotoUrl(user.getPhotoUrl());
employeeDTO.setQREnabled(user.getIsQREnabled());
return employeeDTO; return employeeDTO;
} }
} }