From f147f9acf9b6e65e9664dfd796dd96ead29620c3 Mon Sep 17 00:00:00 2001
From: Justiks <dubkovmacsim@yandex.ru>
Date: Thu, 20 Feb 2025 12:27:04 +0300
Subject: [PATCH] add Entiries table and bugfix

---
 .idea/workspace.xml                           | 51 +++++++++----
 .../nto/controller/AdminController.java       |  2 -
 .../nto/controller/AuthController.java        |  2 +-
 .../nto/controller/EmployeeController.java    | 73 +++++++++++++------
 .../java/com/example/nto/entity/Employee.java | 16 ++--
 .../java/com/example/nto/entity/Entry.java    | 31 ++++++++
 .../nto/repository/EntryRepository.java       | 12 +++
 .../example/nto/service/EmployeeService.java  |  1 +
 .../com/example/nto/service/EntryService.java | 12 +++
 .../nto/service/impl/EmployeeServiceImpl.java |  5 ++
 .../nto/service/impl/EntryServiceImpl.java    | 21 ++++++
 11 files changed, 180 insertions(+), 46 deletions(-)
 create mode 100644 src/main/java/com/example/nto/entity/Entry.java
 create mode 100644 src/main/java/com/example/nto/repository/EntryRepository.java
 create mode 100644 src/main/java/com/example/nto/service/EntryService.java
 create mode 100644 src/main/java/com/example/nto/service/impl/EntryServiceImpl.java

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 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="create new endpoints (employee edit)">
+    <list default="true" id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="fix secure">
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Entry.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/EntryRepository.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EntryService.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EntryServiceImpl.java" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/websecurity/WebSecurityConfig.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Employee.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Employee.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -18,6 +26,7 @@
     <option name="RECENT_TEMPLATES">
       <list>
         <option value="Class" />
+        <option value="Interface" />
       </list>
     </option>
   </component>
@@ -35,7 +44,7 @@
   </component>
   <component name="PropertiesComponent"><![CDATA[{
   "keyToString": {
-    "Application.App.executor": "Run",
+    "Application.App.executor": "Debug",
     "Maven.NTO-2024 [org.apache.maven.plugins:maven-clean-plugin:3.1.0:clean].executor": "Run",
     "RunOnceActivity.ShowReadmeOnStart": "true",
     "RunOnceActivity.git.unshallow": "true",
@@ -157,7 +166,15 @@
       <option name="project" value="LOCAL" />
       <updated>1740037639602</updated>
     </task>
-    <option name="localTasksCounter" value="9" />
+    <task id="LOCAL-00009" summary="fix secure">
+      <option name="closed" value="true" />
+      <created>1740038543344</created>
+      <option name="number" value="00009" />
+      <option name="presentableId" value="LOCAL-00009" />
+      <option name="project" value="LOCAL" />
+      <updated>1740038543344</updated>
+    </task>
+    <option name="localTasksCounter" value="10" />
     <servers />
   </component>
   <component name="VcsManagerConfiguration">
@@ -169,19 +186,15 @@
     <MESSAGE value="промежуточные итоги :skull:" />
     <MESSAGE value="AUTHORIZATION WORK!" />
     <MESSAGE value="create new endpoints (employee edit)" />
-    <option name="LAST_COMMIT_MESSAGE" value="create new endpoints (employee edit)" />
+    <MESSAGE value="fix secure" />
+    <option name="LAST_COMMIT_MESSAGE" value="fix secure" />
   </component>
   <component name="XDebuggerManager">
     <breakpoint-manager>
       <breakpoints>
         <line-breakpoint enabled="true" type="java-line">
           <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
-          <line>36</line>
-          <option name="timeStamp" value="6" />
-        </line-breakpoint>
-        <line-breakpoint enabled="true" type="java-line">
-          <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
-          <line>17</line>
+          <line>16</line>
           <option name="timeStamp" value="7" />
         </line-breakpoint>
         <line-breakpoint enabled="true" type="java-line">
@@ -190,9 +203,19 @@
           <option name="timeStamp" value="25" />
         </line-breakpoint>
         <line-breakpoint enabled="true" type="java-line">
-          <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java</url>
-          <line>70</line>
-          <option name="timeStamp" value="26" />
+          <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java</url>
+          <line>79</line>
+          <option name="timeStamp" value="42" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" type="java-line">
+          <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
+          <line>117</line>
+          <option name="timeStamp" value="43" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" type="java-line">
+          <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
+          <line>106</line>
+          <option name="timeStamp" value="44" />
         </line-breakpoint>
       </breakpoints>
     </breakpoint-manager>
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<Entry> entryList;
+
+        public List<Entry> getEntryList() {
+            return entryList;
+        }
+
+        public void setEntryList(List<Entry> 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<String> authAttempt(HttpServletRequest request) {
-
-        String login = SecurityContextHolder.getContext().getAuthentication().getName();
-
+    @GetMapping("/api/{login}/auth")
+    public ResponseEntity<String> 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<Employee> getEmployeeInfo() {
+    public EmployeeInfoBody getEmployeeInfo() {
         String login = SecurityContextHolder.getContext().getAuthentication().getName();
-
-        try {
-            Optional<Employee> 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<String> 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<AdminsEmployee> adminsEmployees = new ArrayList<>();
+    @OneToMany(mappedBy = "employee")
+    private List<Entry> entries;
 
     public long getId() {
         return id;
@@ -100,4 +96,12 @@ public class Employee {
     public void setRole(String role) {
         this.role = role;
     }
+
+    public List<Entry> getEntries() {
+        return entries;
+    }
+
+    public void setEntries(List<Entry> 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<Entry, Long> {
+    List<Entry> 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<Entry> 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<Entry> getEntriesByEmployee(Employee employee) {
+        return entryRepository.findByEmployee(employee);
+    }
+}