Add method Forbidden
to Utils; change logic of endpoint /employee/open
This commit is contained in:
parent
e6ca7e5322
commit
9b2c4501e1
@ -6,15 +6,19 @@ import org.springframework.http.ResponseEntity;
|
|||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public ResponseEntity<ResponseData> NotFound() {
|
public ResponseEntity<ResponseData> NotFound(String message) {
|
||||||
return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), "Login not found or invalid"), HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), message), HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<ResponseData> BadRequest() {
|
public ResponseEntity<ResponseData> BadRequest(String message) {
|
||||||
return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), "Something went wrong"), HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), message), HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<ResponseData> Ok(String message) {
|
public ResponseEntity<ResponseData> Ok(String message) {
|
||||||
return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), HttpStatus.OK);
|
return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<ResponseData> Forbidden(String message) {
|
||||||
|
return new ResponseEntity<>(new ResponseData(HttpStatus.FORBIDDEN.value(), message), HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.example.nto.controller;
|
|||||||
|
|
||||||
import com.example.nto.Utils;
|
import com.example.nto.Utils;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.ResponseData;
|
|
||||||
import com.example.nto.entity.Visits;
|
import com.example.nto.entity.Visits;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -19,7 +18,7 @@ public class AdminController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
|
||||||
private Utils utils = new Utils();
|
private final Utils utils = new Utils();
|
||||||
|
|
||||||
@GetMapping("/employees")
|
@GetMapping("/employees")
|
||||||
public List<Employee> getEmployees() {
|
public List<Employee> getEmployees() {
|
||||||
@ -30,7 +29,7 @@ public class AdminController {
|
|||||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable("username") String username) {
|
public ResponseEntity<?> getEmployeeInfo(@PathVariable("username") String username) {
|
||||||
Optional<Employee> employee = employeeService.findByLogin(username);
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return utils.NotFound();
|
return utils.NotFound("EmployeeNotFound");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||||
@ -40,7 +39,7 @@ public class AdminController {
|
|||||||
public ResponseEntity<?> getEmployeeLastVisit(@PathVariable("username") String username) {
|
public ResponseEntity<?> getEmployeeLastVisit(@PathVariable("username") String username) {
|
||||||
Optional<Employee> employee = employeeService.findByLogin(username);
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return utils.NotFound();
|
return utils.NotFound("EmployeeNotFound");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||||
@ -49,7 +48,7 @@ public class AdminController {
|
|||||||
@GetMapping("/{username}/visits")
|
@GetMapping("/{username}/visits")
|
||||||
public ResponseEntity<?> getEmployeeVisits(@PathVariable("username") String username) {
|
public ResponseEntity<?> getEmployeeVisits(@PathVariable("username") String username) {
|
||||||
if (!employeeService.existsByLogin(username)) {
|
if (!employeeService.existsByLogin(username)) {
|
||||||
return utils.NotFound();
|
return utils.NotFound("EmployeeNotFound");
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<List<Visits>> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username));
|
Optional<List<Visits>> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username));
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
import com.example.nto.Utils;
|
import com.example.nto.Utils;
|
||||||
import com.example.nto.entity.Code;
|
|
||||||
import com.example.nto.entity.CodeBody;
|
import com.example.nto.entity.CodeBody;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.ResponseData;
|
import com.example.nto.entity.ResponseData;
|
||||||
@ -16,12 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/employee/")
|
@RequestMapping("/api/employee/")
|
||||||
@ -31,7 +31,7 @@ public class EmployeeController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CodeService codeService;
|
private CodeService codeService;
|
||||||
|
|
||||||
private Utils utils = new Utils();
|
private final Utils utils = new Utils();
|
||||||
|
|
||||||
@Operation(summary = "Get employee info with login")
|
@Operation(summary = "Get employee info with login")
|
||||||
@ApiResponses(value = {
|
@ApiResponses(value = {
|
||||||
@ -55,13 +55,13 @@ public class EmployeeController {
|
|||||||
|
|
||||||
Optional<Employee> employee = employeeService.findByLogin(username);
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return utils.NotFound();
|
return utils.NotFound("EmployeeNotFound");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Exception: " + e.getMessage());
|
System.out.println("Exception: " + e.getMessage());
|
||||||
return utils.BadRequest();
|
return utils.BadRequest(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,6 +77,10 @@ public class EmployeeController {
|
|||||||
schema = @Schema(implementation = ResponseData.class))}),
|
schema = @Schema(implementation = ResponseData.class))}),
|
||||||
@ApiResponse(responseCode = "400",
|
@ApiResponse(responseCode = "400",
|
||||||
description = "Something went wrong",
|
description = "Something went wrong",
|
||||||
|
content = {@Content(mediaType = "application/json",
|
||||||
|
schema = @Schema(implementation = ResponseData.class))}),
|
||||||
|
@ApiResponse(responseCode = "403",
|
||||||
|
description = "Employee is blocked",
|
||||||
content = {@Content(mediaType = "application/json",
|
content = {@Content(mediaType = "application/json",
|
||||||
schema = @Schema(implementation = ResponseData.class))})
|
schema = @Schema(implementation = ResponseData.class))})
|
||||||
})
|
})
|
||||||
@ -87,20 +91,23 @@ public class EmployeeController {
|
|||||||
|
|
||||||
Optional<Employee> employee = employeeService.findByLogin(username);
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
if (employee.isEmpty()) {
|
if (employee.isEmpty()) {
|
||||||
return utils.NotFound();
|
return utils.NotFound("EmployeeNotFound");
|
||||||
|
}
|
||||||
|
if (employee.get().getBlocked()) {
|
||||||
|
return utils.Forbidden("YouAreBlocked");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!codeService.exists(body.getValue())) {
|
if (!codeService.exists(body.getValue())) {
|
||||||
return utils.BadRequest();
|
return utils.BadRequest("CodeIsInvalid");
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalDateTime time = LocalDateTime.now();
|
LocalDateTime time = LocalDateTime.now();
|
||||||
employeeService.setLastVisitEmployee(employee.get().getId(), time);
|
employeeService.setLastVisitEmployee(employee.get().getId(), time);
|
||||||
employeeService.addVisit(employee.get().getLogin(), time);
|
employeeService.addVisit(employee.get().getLogin(), time, "" + body.getValue());
|
||||||
return utils.Ok("Door opened success");
|
return utils.Ok("OpenedSuccess");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Exception: " + e.getMessage());
|
System.out.println("Exception: " + e.getMessage());
|
||||||
return utils.BadRequest();
|
return utils.BadRequest(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public interface EmployeeService {
|
|||||||
|
|
||||||
void setLastVisitEmployee(long Id, LocalDateTime lastVisit);
|
void setLastVisitEmployee(long Id, LocalDateTime lastVisit);
|
||||||
|
|
||||||
void addVisit(String username, LocalDateTime time);
|
void addVisit(String username, LocalDateTime time, String readerId);
|
||||||
|
|
||||||
void setCodeEmployee(long Id, long code);
|
void setCodeEmployee(long Id, long code);
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@ package com.example.nto.service.impl;
|
|||||||
|
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.VisitType;
|
||||||
import com.example.nto.entity.Visits;
|
import com.example.nto.entity.Visits;
|
||||||
import com.example.nto.repository.CodeRepository;
|
import com.example.nto.repository.CodeRepository;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
|
||||||
import com.example.nto.repository.VisitsRepository;
|
import com.example.nto.repository.VisitsRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -41,8 +41,9 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addVisit(String username, LocalDateTime time) {
|
public void addVisit(String username, LocalDateTime time, String readerId) {
|
||||||
visitRepository.save(new Visits());
|
Visits visit = new Visits(0, username, time, VisitType.SCANNER, readerId);
|
||||||
|
visitRepository.save(visit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user