Снова доблавен VisitController для получения посещений конкретного пользователя + исправлен баг с тем, что время создания Visit не сохранялось
This commit is contained in:
parent
ae3b001d8f
commit
e7aea29ec8
@ -31,21 +31,13 @@ public class EmployeeController {
|
|||||||
|
|
||||||
@GetMapping("/{username}/info")
|
@GetMapping("/{username}/info")
|
||||||
public ResponseEntity<Employee> info(@PathVariable String username) {
|
public ResponseEntity<Employee> info(@PathVariable String username) {
|
||||||
return employeeService.info(username);
|
return ResponseEntity.ok(employeeService.info(username));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{username}/auth")
|
@GetMapping("/{username}/auth")
|
||||||
public ResponseEntity<Visit> auth(@PathVariable String username) {
|
public ResponseEntity<Visit> auth(@PathVariable String username) {
|
||||||
return new ResponseEntity<>(null, employeeService.auth(username));
|
employeeService.auth(username);
|
||||||
}
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
|
|
||||||
@GetMapping("/{username}/visits")
|
|
||||||
public ResponseEntity<List<Visit>> visits(
|
|
||||||
@PathVariable String username,
|
|
||||||
@RequestParam Integer offset,
|
|
||||||
@RequestParam Integer limit
|
|
||||||
) {
|
|
||||||
return ResponseEntity.ok(employeeService.getVisits(username, offset, limit));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{username}/open")
|
@PatchMapping("/{username}/open")
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.onomatopoeiaback.controller;
|
||||||
|
|
||||||
|
import com.example.onomatopoeiaback.domain.visit.Visit;
|
||||||
|
import com.example.onomatopoeiaback.domain.visit.VisitDTO;
|
||||||
|
import com.example.onomatopoeiaback.service.VisitService;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/visit")
|
||||||
|
public class VisitController {
|
||||||
|
private final VisitService visitService;
|
||||||
|
|
||||||
|
public VisitController(VisitService visitService) {
|
||||||
|
this.visitService = visitService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{username}/visits")
|
||||||
|
public ResponseEntity<List<Visit>> visits(
|
||||||
|
@PathVariable String username,
|
||||||
|
@RequestParam Integer offset,
|
||||||
|
@RequestParam Integer limit
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(visitService.getVisits(username, offset, limit));
|
||||||
|
}
|
||||||
|
}
|
@ -2,19 +2,12 @@ package com.example.onomatopoeiaback.service;
|
|||||||
|
|
||||||
import com.example.onomatopoeiaback.domain.employee.Employee;
|
import com.example.onomatopoeiaback.domain.employee.Employee;
|
||||||
import com.example.onomatopoeiaback.domain.employee.EmployeeDTO;
|
import com.example.onomatopoeiaback.domain.employee.EmployeeDTO;
|
||||||
import com.example.onomatopoeiaback.domain.visit.Visit;
|
import com.example.onomatopoeiaback.exceptions.UnauthorizedException;
|
||||||
import com.example.onomatopoeiaback.repository.EmployeeRepository;
|
import com.example.onomatopoeiaback.repository.EmployeeRepository;
|
||||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
import com.example.onomatopoeiaback.repository.VisitRepository;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class EmployeeService {
|
public class EmployeeService {
|
||||||
|
|
||||||
@ -38,33 +31,21 @@ public class EmployeeService {
|
|||||||
return employee;
|
return employee;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpStatus auth(String login) {
|
public void auth(String login) {
|
||||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||||
|
|
||||||
if (employee == null) return HttpStatus.UNAUTHORIZED;
|
if (employee == null) {
|
||||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
throw new UnauthorizedException();
|
||||||
employee.setLastVisit(localDateTime);
|
}
|
||||||
employeeRepository.save(employee);
|
|
||||||
|
|
||||||
return HttpStatus.OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<Employee> info(String login) {
|
public Employee info(String login) {
|
||||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||||
HttpStatus response_status = HttpStatus.OK;
|
|
||||||
|
|
||||||
if (employee == null) response_status = HttpStatus.UNAUTHORIZED;
|
if (employee == null) {
|
||||||
else {
|
throw new UnauthorizedException();
|
||||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
|
||||||
employee.setLastVisit(localDateTime);
|
|
||||||
employeeRepository.save(employee);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseEntity<>(employee, response_status);
|
return employee;
|
||||||
}
|
|
||||||
|
|
||||||
public List<Visit> getVisits(String login, Integer offset, Integer limit) {
|
|
||||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,8 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -32,6 +34,8 @@ public class VisitService {
|
|||||||
|
|
||||||
public void register(String login, VisitDTO visitDTO) {
|
public void register(String login, VisitDTO visitDTO) {
|
||||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||||
|
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
||||||
|
|
||||||
Optional<QrCode> qrCodeOptional = qrCodeRepository.findById(visitDTO.getQrCodeId());
|
Optional<QrCode> qrCodeOptional = qrCodeRepository.findById(visitDTO.getQrCodeId());
|
||||||
if (qrCodeOptional.isEmpty()) {
|
if (qrCodeOptional.isEmpty()) {
|
||||||
throw new BadRequestException();
|
throw new BadRequestException();
|
||||||
@ -45,10 +49,15 @@ public class VisitService {
|
|||||||
Visit visit = new Visit();
|
Visit visit = new Visit();
|
||||||
visit.setQrCode(qrCode);
|
visit.setQrCode(qrCode);
|
||||||
visit.setVisitType(visitDTO.getVisitType());
|
visit.setVisitType(visitDTO.getVisitType());
|
||||||
|
visit.setVisitTime(localDateTime);
|
||||||
visit.setEmployee(employee);
|
visit.setEmployee(employee);
|
||||||
visitRepository.saveAndFlush(visit);
|
visitRepository.saveAndFlush(visit);
|
||||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
|
||||||
employee.setLastVisit(localDateTime);
|
employee.setLastVisit(localDateTime);
|
||||||
employeeRepository.saveAndFlush(employee);
|
employeeRepository.saveAndFlush(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Visit> getVisits(String login, Integer offset, Integer limit) {
|
||||||
|
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user