addded all routes from the second task
This commit is contained in:
parent
8519c5c73f
commit
7853f77e14
@ -0,0 +1,45 @@
|
||||
package com.example.onomatopoeiaback.controller;
|
||||
|
||||
|
||||
import com.example.onomatopoeiaback.domain.employee.Employee;
|
||||
import com.example.onomatopoeiaback.domain.visit.Visit;
|
||||
import com.example.onomatopoeiaback.domain.visit.VisitDTO;
|
||||
import com.example.onomatopoeiaback.service.EmployeeService;
|
||||
import com.example.onomatopoeiaback.service.VisitService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class EmployeeController {
|
||||
private final VisitService visitService;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService, VisitService visitService) {
|
||||
this.employeeService = employeeService;
|
||||
this.visitService = visitService;
|
||||
}
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/info")
|
||||
public ResponseEntity<Employee> info(@PathVariable String username) {
|
||||
return employeeService.info(username);
|
||||
}
|
||||
|
||||
@GetMapping("/{username}/auth")
|
||||
public ResponseEntity<Visit> auth(@PathVariable String username) {
|
||||
return new ResponseEntity<>(null, employeeService.auth(username));
|
||||
}
|
||||
|
||||
@PatchMapping("/{username}/open")
|
||||
public ResponseEntity<Visit> open(@PathVariable String username, VisitDTO visitDTO) {
|
||||
visitService.open(username, visitDTO.getValue());
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.onomatopoeiaback.exceptions;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "Что-то пошло не так")
|
||||
public class BadRequestException extends RuntimeException {
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.onomatopoeiaback.exceptions;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.UNAUTHORIZED, reason = "Пользователь не авторизован")
|
||||
public class UnauthorizedException extends RuntimeException {
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.onomatopoeiaback.repository;
|
||||
|
||||
import com.example.onomatopoeiaback.domain.employee.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
Employee getEmployeeById(Long id);
|
||||
|
||||
Employee getEmployeesByLogin(String login);
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.onomatopoeiaback.repository;
|
||||
|
||||
import com.example.onomatopoeiaback.domain.visit.Visit;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface VisitRepository extends JpaRepository<Visit, Long> {
|
||||
Visit getVisitByValue(long value);
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.example.onomatopoeiaback.service;
|
||||
|
||||
import com.example.onomatopoeiaback.domain.employee.Employee;
|
||||
import com.example.onomatopoeiaback.repository.EmployeeRepository;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public EmployeeService(EmployeeRepository employeeRepository) {
|
||||
this.employeeRepository = employeeRepository;
|
||||
}
|
||||
|
||||
public HttpStatus auth(String login) {
|
||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||
|
||||
if (employee == null) return HttpStatus.UNAUTHORIZED;
|
||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
||||
employee.setLastVisit(localDateTime);
|
||||
employeeRepository.save(employee);
|
||||
|
||||
return HttpStatus.OK;
|
||||
}
|
||||
|
||||
public ResponseEntity<Employee> info(String login) {
|
||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||
HttpStatus response_status = HttpStatus.OK;
|
||||
|
||||
if (employee == null) response_status = HttpStatus.UNAUTHORIZED;
|
||||
else {
|
||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
||||
employee.setLastVisit(localDateTime);
|
||||
employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(employee, response_status);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.example.onomatopoeiaback.service;
|
||||
|
||||
|
||||
import com.example.onomatopoeiaback.domain.employee.Employee;
|
||||
import com.example.onomatopoeiaback.domain.visit.Visit;
|
||||
import com.example.onomatopoeiaback.exceptions.BadRequestException;
|
||||
import com.example.onomatopoeiaback.exceptions.UnauthorizedException;
|
||||
import com.example.onomatopoeiaback.repository.EmployeeRepository;
|
||||
import com.example.onomatopoeiaback.repository.VisitRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
@Service
|
||||
public class VisitService {
|
||||
private final VisitRepository visitRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public VisitService(VisitRepository visitRepository, EmployeeRepository employeeRepository) {
|
||||
this.visitRepository = visitRepository;
|
||||
this.employeeRepository = employeeRepository;
|
||||
}
|
||||
|
||||
public void open(String login, Long value) {
|
||||
Visit visit = visitRepository.getVisitByValue(value);
|
||||
Employee employee = employeeRepository.getEmployeesByLogin(login);
|
||||
|
||||
if (employee == null) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
else if (visit == null) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
LocalDateTime localDateTime = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
|
||||
employee.setLastVisit(localDateTime);
|
||||
employeeRepository.saveAndFlush(employee);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user