diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d286b02..fc19531 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,18 @@
-
+
+
+
+
+
-
+
+
+
+
+
@@ -18,6 +26,7 @@
@@ -35,7 +44,7 @@
1740037639602
-
+
+
+ 1740038543344
+
+
+
+ 1740038543344
+
+
@@ -169,19 +186,15 @@
-
+
+
file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
- 36
-
-
-
- file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
- 17
+ 16
@@ -190,9 +203,19 @@
- file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java
- 70
-
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java
+ 79
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
+ 117
+
+
+
+ file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java
+ 106
+
diff --git a/src/main/java/com/example/nto/controller/AdminController.java b/src/main/java/com/example/nto/controller/AdminController.java
index c1cdbbf..d74b914 100644
--- a/src/main/java/com/example/nto/controller/AdminController.java
+++ b/src/main/java/com/example/nto/controller/AdminController.java
@@ -5,8 +5,6 @@ import com.example.nto.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
-import org.springframework.security.access.annotation.Secured;
-import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
diff --git a/src/main/java/com/example/nto/controller/AuthController.java b/src/main/java/com/example/nto/controller/AuthController.java
index 8f3b796..71d39e9 100644
--- a/src/main/java/com/example/nto/controller/AuthController.java
+++ b/src/main/java/com/example/nto/controller/AuthController.java
@@ -68,7 +68,7 @@ public class AuthController {
securityContext.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
- return ResponseEntity.status(HttpStatus.OK).build();
+ return ResponseEntity.status(HttpStatus.OK).build();
}
}
diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java
index 6a1322b..aa40fbc 100644
--- a/src/main/java/com/example/nto/controller/EmployeeController.java
+++ b/src/main/java/com/example/nto/controller/EmployeeController.java
@@ -1,27 +1,55 @@
package com.example.nto.controller;
import com.example.nto.entity.Employee;
+import com.example.nto.entity.Entry;
import com.example.nto.repository.CodeRepository;
+import com.example.nto.repository.EntryRepository;
import com.example.nto.service.EmployeeService;
-import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapProperties;
+import com.example.nto.service.EntryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
-import org.springframework.security.access.annotation.Secured;
-import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
-import javax.servlet.http.HttpServletRequest;
-import java.util.Optional;
+import java.util.List;
@RestController
public class EmployeeController {
private final EmployeeService employeeService;
private final CodeRepository codeRepository;
+ private final EntryService entryService;
+ private final EntryRepository entryRepository;
- public EmployeeController(EmployeeService employeeService, CodeRepository codeRepository) {
+ public EmployeeController(
+ EmployeeService employeeService,
+ CodeRepository codeRepository,
+ EntryService entryService,
+ EntryRepository entryRepository) {
this.employeeService = employeeService;
this.codeRepository = codeRepository;
+ this.entryService = entryService;
+ this.entryRepository = entryRepository;
+ }
+
+ public static class EmployeeInfoBody {
+ private long id;
+ private List entryList;
+
+ public List getEntryList() {
+ return entryList;
+ }
+
+ public void setEntryList(List entryList) {
+ this.entryList = entryList;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
}
/**
@@ -29,11 +57,8 @@ public class EmployeeController {
*
* @return статус код, смотри документацию к тз
*/
- @GetMapping("/api/auth")
- public ResponseEntity authAttempt(HttpServletRequest request) {
-
- String login = SecurityContextHolder.getContext().getAuthentication().getName();
-
+ @GetMapping("/api/{login}/auth")
+ public ResponseEntity authAttempt(@PathVariable String login) {
try {
if (employeeService.checkEmployeeExists(login)) {
return ResponseEntity.status(HttpStatus.ACCEPTED).build(); // Логин найден ДВЕСТИ
@@ -50,28 +75,26 @@ public class EmployeeController {
/**
* Получить информацию по емплоеееее
*
- * @return емплоеееееее иначе 401 бан бан бан бан, а если ошибка, то 400 БАААААН
+ * @return Return employee info.
+ * Удален вариант получить 401 статус,так как теперь до этого эндпоинта нельзя достучаться без авторизации
*/
@GetMapping("/api/info")
- public ResponseEntity getEmployeeInfo() {
+ public EmployeeInfoBody getEmployeeInfo() {
String login = SecurityContextHolder.getContext().getAuthentication().getName();
-
- try {
- Optional employee = employeeService.getEmployeeInfo(login);
- return employee.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
- } catch (
- Exception e) // Я понял статус "что-то пошло не так", как то, что произошла какая-то ошибка. Вообще по идее должен быть статус 500, но допустим
- {
- return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
- }
+ Employee employee = employeeService.getEmployeeInfo(login).get();
+ EmployeeInfoBody employeeInfoBody = new EmployeeInfoBody();
+ employeeInfoBody.setId(employee);
+ employeeInfoBody.setEntryList(entryService.getEntriesByEmployee(employee));
+ return employeeInfoBody;
}
/**
* Проверка на дверь открыть не открыть
+ *
* @param value код двери
* @return статус код отвта
*/
- @PatchMapping("api/{value}/open")
+ @PatchMapping("api/open")
public ResponseEntity openDoorAttempt(@RequestBody Long value) {
try {
String login = SecurityContextHolder.getContext().getAuthentication().getName();
@@ -85,6 +108,10 @@ public class EmployeeController {
}
employeeService.updateLastVisit(login);
+ Entry entry = new Entry();
+ Employee employee = employeeService.getEmployeeByLogin(login);
+ entry.setEmployee(employee);
+ entryRepository.save(entry);
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
} catch (Exception e) {
diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java
index 9b1158c..b3615af 100644
--- a/src/main/java/com/example/nto/entity/Employee.java
+++ b/src/main/java/com/example/nto/entity/Employee.java
@@ -4,13 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
-import org.springframework.security.core.GrantedAuthority;
-import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*;
import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
@Entity
@@ -34,8 +30,8 @@ public class Employee {
private LocalDateTime lastVisit;
-// @OneToMany(mappedBy = "employee")
-// private List adminsEmployees = new ArrayList<>();
+ @OneToMany(mappedBy = "employee")
+ private List entries;
public long getId() {
return id;
@@ -100,4 +96,12 @@ public class Employee {
public void setRole(String role) {
this.role = role;
}
+
+ public List getEntries() {
+ return entries;
+ }
+
+ public void setEntries(List entries) {
+ this.entries = entries;
+ }
}
diff --git a/src/main/java/com/example/nto/entity/Entry.java b/src/main/java/com/example/nto/entity/Entry.java
new file mode 100644
index 0000000..02d3a9e
--- /dev/null
+++ b/src/main/java/com/example/nto/entity/Entry.java
@@ -0,0 +1,31 @@
+package com.example.nto.entity;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name = "entry")
+public class Entry {
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private long id;
+
+ @ManyToOne
+ @JoinColumn(name = "employee")
+ private Employee employee;
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public Employee getEmployee() {
+ return employee;
+ }
+
+ public void setEmployee(Employee employee) {
+ this.employee = employee;
+ }
+}
diff --git a/src/main/java/com/example/nto/repository/EntryRepository.java b/src/main/java/com/example/nto/repository/EntryRepository.java
new file mode 100644
index 0000000..96a58fc
--- /dev/null
+++ b/src/main/java/com/example/nto/repository/EntryRepository.java
@@ -0,0 +1,12 @@
+package com.example.nto.repository;
+
+import com.example.nto.entity.Employee;
+import com.example.nto.entity.Entry;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+public interface EntryRepository extends JpaRepository {
+ List findByEmployee(Employee employee);
+}
diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java
index 03ace10..496bd01 100644
--- a/src/main/java/com/example/nto/service/EmployeeService.java
+++ b/src/main/java/com/example/nto/service/EmployeeService.java
@@ -14,4 +14,5 @@ public interface EmployeeService {
Employee updateEmployeeLogin(Long id, String newLogin);
Employee updateEmployeeAvatar(Long id, String newAvatar);
Employee updateEmployeePosition(Long id, String newPosition);
+ Employee getEmployeeByLogin(String login);
}
diff --git a/src/main/java/com/example/nto/service/EntryService.java b/src/main/java/com/example/nto/service/EntryService.java
new file mode 100644
index 0000000..71040b3
--- /dev/null
+++ b/src/main/java/com/example/nto/service/EntryService.java
@@ -0,0 +1,12 @@
+package com.example.nto.service;
+
+import com.example.nto.entity.Employee;
+import com.example.nto.entity.Entry;
+
+
+
+import java.util.List;
+
+public interface EntryService {
+ List getEntriesByEmployee(Employee employee);
+}
diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java
index 2156760..68976c2 100644
--- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java
+++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java
@@ -103,4 +103,9 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setPosition(newPosition);
employeeRepository.save(employee);
return employee; }
+
+ @Override
+ public Employee getEmployeeByLogin(String login) {
+ return employeeRepository.getByLogin(login).get();
+ }
}
diff --git a/src/main/java/com/example/nto/service/impl/EntryServiceImpl.java b/src/main/java/com/example/nto/service/impl/EntryServiceImpl.java
new file mode 100644
index 0000000..e83118c
--- /dev/null
+++ b/src/main/java/com/example/nto/service/impl/EntryServiceImpl.java
@@ -0,0 +1,21 @@
+package com.example.nto.service.impl;
+
+import com.example.nto.entity.Employee;
+import com.example.nto.entity.Entry;
+import com.example.nto.repository.EntryRepository;
+import com.example.nto.service.EntryService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class EntryServiceImpl implements EntryService {
+ @Autowired
+ private EntryRepository entryRepository;
+
+ @Override
+ public List getEntriesByEmployee(Employee employee) {
+ return entryRepository.findByEmployee(employee);
+ }
+}