Files from NTO-2 stage (Olympath command)
This commit is contained in:
parent
61b130fdfc
commit
60fce4cd3d
2
pom.xml
2
pom.xml
@ -24,6 +24,8 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.30</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -1,9 +1,11 @@
|
||||
package com.example.nto;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,157 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.entity.Code;
|
||||
import com.example.nto.entity.CodeBody;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.ResponseData;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class EmployeeController {
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
|
||||
private ResponseEntity<ResponseData> NotFound() {
|
||||
return new ResponseEntity<>(new ResponseData(HttpStatus.UNAUTHORIZED.value(), "Login not found or invalid"), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
private ResponseEntity<ResponseData> BadRequest() {
|
||||
return new ResponseEntity<>(new ResponseData(HttpStatus.BAD_REQUEST.value(), "Something went wrong"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
private ResponseEntity<ResponseData> Ok(String message) {
|
||||
return new ResponseEntity<>(new ResponseData(HttpStatus.OK.value(), message), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Auth employee with login")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
description = "Auth employee with login success",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))}),
|
||||
@ApiResponse(responseCode = "401",
|
||||
description = "Login not found or invalid",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))}),
|
||||
@ApiResponse(responseCode = "400",
|
||||
description = "Something went wrong",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))})
|
||||
})
|
||||
@GetMapping("/api/{login}/auth")
|
||||
public ResponseEntity<?> auth(@PathVariable("login") String login) {
|
||||
try {
|
||||
if (login == null || login.isEmpty() || !employeeService.existsByLogin(login)) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok("Auth success");
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e.getMessage());
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "Get employee info with login")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
description = "OK",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = Employee.class))}),
|
||||
@ApiResponse(responseCode = "401",
|
||||
description = "Login not found or invalid",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))}),
|
||||
@ApiResponse(responseCode = "400",
|
||||
description = "Something went wrong",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))})
|
||||
})
|
||||
@GetMapping("/api/{login}/info")
|
||||
public ResponseEntity<?> info(@PathVariable("login") String login) {
|
||||
try {
|
||||
if (login == null || login.isEmpty() || login.isBlank() || !employeeService.existsByLogin(login)) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||
if (employee.isEmpty()) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e.getMessage());
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "Open door with code")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
description = "Door opened success",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))}),
|
||||
@ApiResponse(responseCode = "401",
|
||||
description = "Login not found or invalid",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))}),
|
||||
@ApiResponse(responseCode = "400",
|
||||
description = "Something went wrong",
|
||||
content = {@Content(mediaType = "application/json",
|
||||
schema = @Schema(implementation = ResponseData.class))})
|
||||
})
|
||||
@PatchMapping("/api/{login}/open")
|
||||
public ResponseEntity<?> open(@PathVariable("login") String login, @Valid @RequestBody CodeBody body) {
|
||||
try {
|
||||
if (login == null || login.isEmpty() || login.isBlank() || !employeeService.existsByLogin(login)) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Optional<Employee> employee = employeeService.findByLogin(login);
|
||||
if (employee.isEmpty()) {
|
||||
return NotFound();
|
||||
}
|
||||
Optional<Code> code = employeeService.findCodeById(employee.get().getId());
|
||||
if (code.isEmpty()) {
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
long dbValue = 0;
|
||||
try {
|
||||
dbValue = code.get().getValue();
|
||||
} catch (Exception e) {
|
||||
employeeService.setCodeEmployee(employee.get().getId(), body.getValue());
|
||||
|
||||
employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now());
|
||||
return Ok("Door opened success");
|
||||
}
|
||||
|
||||
if (dbValue != body.getValue()) {
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now());
|
||||
return Ok("Door opened success");
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e.getMessage());
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "code")
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Code {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", unique = true, nullable = false)
|
||||
private long id;
|
||||
|
||||
@Column(name = "value", nullable = false)
|
||||
private long value;
|
||||
}
|
||||
|
13
src/main/java/com/example/nto/entity/CodeBody.java
Normal file
13
src/main/java/com/example/nto/entity/CodeBody.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CodeBody {
|
||||
private long value;
|
||||
}
|
@ -1,21 +1,36 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Setter
|
||||
@Getter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", unique = true, nullable = false)
|
||||
private long id;
|
||||
|
||||
@Column(name = "login", unique = true, nullable = false)
|
||||
private String login;
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(name = "photo", nullable = false)
|
||||
private String photo;
|
||||
|
||||
@Column(name = "position", nullable = false)
|
||||
private String position;
|
||||
|
||||
@Column(name = "last_visit", nullable = false)
|
||||
private LocalDateTime lastVisit;
|
||||
}
|
||||
|
14
src/main/java/com/example/nto/entity/ResponseData.java
Normal file
14
src/main/java/com/example/nto/entity/ResponseData.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ResponseData {
|
||||
private int status;
|
||||
private String message;
|
||||
}
|
@ -3,5 +3,10 @@ package com.example.nto.repository;
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
Optional<Employee> findByLogin(String login);
|
||||
|
||||
boolean existsByLogin(String login);
|
||||
}
|
||||
|
@ -1,4 +1,19 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.entity.Code;
|
||||
import com.example.nto.entity.Employee;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeService {
|
||||
boolean existsByLogin(String login);
|
||||
|
||||
void setLastVisitEmployee(long Id, LocalDateTime lastVisit);
|
||||
|
||||
Code setCodeEmployee(long Id, long code);
|
||||
|
||||
Optional<Employee> findByLogin(String login);
|
||||
|
||||
Optional<Code> findCodeById(Long id);
|
||||
}
|
||||
|
@ -1,6 +1,48 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.entity.Code;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.repository.CodeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
}
|
||||
@Autowired
|
||||
private com.example.nto.repository.EmployeeRepository EmployeeRepository;
|
||||
@Autowired
|
||||
private CodeRepository codeRepository;
|
||||
|
||||
@Override
|
||||
public Optional<Employee> findByLogin(String login) {
|
||||
return EmployeeRepository.findByLogin(login);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Code> findCodeById(Long id) {
|
||||
return Optional.of(codeRepository.getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastVisitEmployee(long Id, LocalDateTime lastVisit) {
|
||||
Employee employee = EmployeeRepository.getById(Id);
|
||||
employee.setLastVisit(lastVisit);
|
||||
EmployeeRepository.save(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsByLogin(String login) {
|
||||
return EmployeeRepository.existsByLogin(login);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Code setCodeEmployee(long Id, long code) {
|
||||
Code newCode = new Code(Id, code);
|
||||
codeRepository.save(newCode);
|
||||
return newCode;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user