Add table Visits
This commit is contained in:
parent
f14f91f725
commit
20db8878bc
@ -1,9 +1,22 @@
|
|||||||
package com.example.nto;
|
package com.example.nto;
|
||||||
|
|
||||||
|
import com.example.nto.repository.CodeRepository;
|
||||||
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
|
import com.example.nto.repository.VisitsRepository;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.springframework.boot.context.properties.bind.Bindable.listOf;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableJpaRepositories(basePackages = "com.example.nto.repository")
|
||||||
|
@ComponentScan(basePackages = "com.example.nto.*")
|
||||||
|
@EntityScan("com.example.nto.*")
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(App.class, args);
|
SpringApplication.run(App.class, args);
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
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.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/admin")
|
@RequestMapping("/api/admin")
|
||||||
@ -15,8 +19,41 @@ public class AdminController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
|
||||||
|
private Utils utils = new Utils();
|
||||||
|
|
||||||
@GetMapping("/employees")
|
@GetMapping("/employees")
|
||||||
public List<Employee> getEmployees() {
|
public List<Employee> getEmployees() {
|
||||||
return employeeService.getAllEmployees();
|
return employeeService.getAllEmployees();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{username}/info")
|
||||||
|
public ResponseEntity<?> getEmployeeInfo(@PathVariable("username") String username) {
|
||||||
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
|
if (employee.isEmpty()) {
|
||||||
|
return utils.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{username}/lastVisit")
|
||||||
|
public ResponseEntity<?> getEmployeeLastVisit(@PathVariable("username") String username) {
|
||||||
|
Optional<Employee> employee = employeeService.findByLogin(username);
|
||||||
|
if (employee.isEmpty()) {
|
||||||
|
return utils.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity<>(employee.get(), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{username}/visits")
|
||||||
|
public ResponseEntity<?> getEmployeeVisits(@PathVariable("username") String username) {
|
||||||
|
if (!employeeService.existsByLogin(username)) {
|
||||||
|
return utils.NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<List<Visits>> visits = Optional.ofNullable(employeeService.getEmployeeVisits(username));
|
||||||
|
|
||||||
|
return new ResponseEntity<>(visits.get(), HttpStatus.OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ 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;
|
||||||
|
import com.example.nto.service.CodeService;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
@ -27,6 +28,8 @@ import java.util.Optional;
|
|||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
@Autowired
|
||||||
|
private CodeService codeService;
|
||||||
|
|
||||||
private Utils utils = new Utils();
|
private Utils utils = new Utils();
|
||||||
|
|
||||||
@ -87,26 +90,13 @@ public class EmployeeController {
|
|||||||
return utils.NotFound();
|
return utils.NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Code> code = employeeService.findCodeById(employee.get().getId());
|
if (!codeService.exists(body.getValue())) {
|
||||||
if (code.isEmpty()) {
|
|
||||||
return utils.BadRequest();
|
return utils.BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
long codeFromDB = 0;
|
LocalDateTime time = LocalDateTime.now();
|
||||||
try {
|
employeeService.setLastVisitEmployee(employee.get().getId(), time);
|
||||||
codeFromDB = code.get().getValue();
|
employeeService.addVisit(employee.get().getLogin(), time);
|
||||||
} catch (Exception e) {
|
|
||||||
employeeService.setCodeEmployee(employee.get().getId(), body.getValue());
|
|
||||||
|
|
||||||
employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now());
|
|
||||||
return utils.Ok("Door opened success");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (codeFromDB != body.getValue()) {
|
|
||||||
return utils.BadRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
employeeService.setLastVisitEmployee(employee.get().getId(), LocalDateTime.now());
|
|
||||||
return utils.Ok("Door opened success");
|
return utils.Ok("Door opened success");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Exception: " + e.getMessage());
|
System.out.println("Exception: " + e.getMessage());
|
||||||
|
5
src/main/java/com/example/nto/entity/VisitType.java
Normal file
5
src/main/java/com/example/nto/entity/VisitType.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
public enum VisitType {
|
||||||
|
SCANNER, NFC
|
||||||
|
}
|
33
src/main/java/com/example/nto/entity/Visits.java
Normal file
33
src/main/java/com/example/nto/entity/Visits.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Table(name = "visits")
|
||||||
|
public class Visits {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "id", unique = true, nullable = false)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "username", nullable = false)
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Column(name = "time", nullable = false)
|
||||||
|
private LocalDateTime time;
|
||||||
|
|
||||||
|
@Column(name = "type", nullable = false)
|
||||||
|
private VisitType type;
|
||||||
|
|
||||||
|
@Column(name = "reader_id", nullable = false)
|
||||||
|
private String readerId;
|
||||||
|
}
|
@ -2,6 +2,9 @@ package com.example.nto.repository;
|
|||||||
|
|
||||||
import com.example.nto.entity.Code;
|
import com.example.nto.entity.Code;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface CodeRepository extends JpaRepository<Code, Long> {
|
public interface CodeRepository extends JpaRepository<Code, Long> {
|
||||||
|
boolean existsByValue(long value);
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,11 @@ package com.example.nto.repository;
|
|||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
Optional<Employee> findByLogin(String login);
|
Optional<Employee> findByLogin(String login);
|
||||||
|
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Visits;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface VisitsRepository extends JpaRepository<Visits, Long> {
|
||||||
|
List<Visits> findAllByUsername(String username);
|
||||||
|
}
|
9
src/main/java/com/example/nto/service/CodeService.java
Normal file
9
src/main/java/com/example/nto/service/CodeService.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public interface CodeService {
|
||||||
|
|
||||||
|
boolean exists(long code);
|
||||||
|
}
|
@ -2,16 +2,23 @@ package com.example.nto.service;
|
|||||||
|
|
||||||
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.Visits;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Component
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
boolean existsByLogin(String login);
|
boolean existsByLogin(String login);
|
||||||
|
|
||||||
|
List<Visits> getEmployeeVisits(String username);
|
||||||
|
|
||||||
void setLastVisitEmployee(long Id, LocalDateTime lastVisit);
|
void setLastVisitEmployee(long Id, LocalDateTime lastVisit);
|
||||||
|
|
||||||
|
void addVisit(String username, LocalDateTime time);
|
||||||
|
|
||||||
void setCodeEmployee(long Id, long code);
|
void setCodeEmployee(long Id, long code);
|
||||||
|
|
||||||
Optional<Employee> findByLogin(String login);
|
Optional<Employee> findByLogin(String login);
|
||||||
|
7
src/main/java/com/example/nto/service/VisitsService.java
Normal file
7
src/main/java/com/example/nto/service/VisitsService.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public interface VisitsService {
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.repository.CodeRepository;
|
||||||
|
import com.example.nto.service.CodeService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CodeServiceImpl implements CodeService {
|
||||||
|
@Autowired
|
||||||
|
private CodeRepository codeRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean exists(long code) {
|
||||||
|
return codeRepository.existsByValue(code);
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,10 @@ 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.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.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -17,6 +20,8 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
private com.example.nto.repository.EmployeeRepository EmployeeRepository;
|
private com.example.nto.repository.EmployeeRepository EmployeeRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private CodeRepository codeRepository;
|
private CodeRepository codeRepository;
|
||||||
|
@Autowired
|
||||||
|
private VisitsRepository visitRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Employee> findByLogin(String login) {
|
public Optional<Employee> findByLogin(String login) {
|
||||||
@ -35,11 +40,21 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
EmployeeRepository.save(employee);
|
EmployeeRepository.save(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addVisit(String username, LocalDateTime time) {
|
||||||
|
visitRepository.save(new Visits());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean existsByLogin(String login) {
|
public boolean existsByLogin(String login) {
|
||||||
return EmployeeRepository.existsByLogin(login);
|
return EmployeeRepository.existsByLogin(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Visits> getEmployeeVisits(String username) {
|
||||||
|
return visitRepository.findAllByUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCodeEmployee(long Id, long code) {
|
public void setCodeEmployee(long Id, long code) {
|
||||||
Code newCode = new Code(Id, code);
|
Code newCode = new Code(Id, code);
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.repository.CodeRepository;
|
||||||
|
import com.example.nto.service.CodeService;
|
||||||
|
import com.example.nto.service.VisitsService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class VisitsServiceImpl implements VisitsService {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user