add Entiries table and bugfix

This commit is contained in:
Justiks 2025-02-20 12:27:04 +03:00
parent 049447a31a
commit f147f9acf9
11 changed files with 180 additions and 46 deletions

51
.idea/workspace.xml generated
View File

@ -4,10 +4,18 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <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$/.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/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> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -18,6 +26,7 @@
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" /> <option value="Class" />
<option value="Interface" />
</list> </list>
</option> </option>
</component> </component>
@ -35,7 +44,7 @@
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "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", "Maven.NTO-2024 [org.apache.maven.plugins:maven-clean-plugin:3.1.0:clean].executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
@ -157,7 +166,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1740037639602</updated> <updated>1740037639602</updated>
</task> </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 /> <servers />
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
@ -169,19 +186,15 @@
<MESSAGE value="промежуточные итоги :skull:" /> <MESSAGE value="промежуточные итоги :skull:" />
<MESSAGE value="AUTHORIZATION WORK!" /> <MESSAGE value="AUTHORIZATION WORK!" />
<MESSAGE value="create new endpoints (employee edit)" /> <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>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
<breakpoints> <breakpoints>
<line-breakpoint enabled="true" type="java-line"> <line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url> <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
<line>36</line> <line>16</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>
<option name="timeStamp" value="7" /> <option name="timeStamp" value="7" />
</line-breakpoint> </line-breakpoint>
<line-breakpoint enabled="true" type="java-line"> <line-breakpoint enabled="true" type="java-line">
@ -190,9 +203,19 @@
<option name="timeStamp" value="25" /> <option name="timeStamp" value="25" />
</line-breakpoint> </line-breakpoint>
<line-breakpoint enabled="true" type="java-line"> <line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java</url> <url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java</url>
<line>70</line> <line>79</line>
<option name="timeStamp" value="26" /> <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> </line-breakpoint>
</breakpoints> </breakpoints>
</breakpoint-manager> </breakpoint-manager>

View File

@ -5,8 +5,6 @@ import com.example.nto.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired; 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.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;

View File

@ -1,27 +1,55 @@
package com.example.nto.controller; package com.example.nto.controller;
import com.example.nto.entity.Employee; import com.example.nto.entity.Employee;
import com.example.nto.entity.Entry;
import com.example.nto.repository.CodeRepository; import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EntryRepository;
import com.example.nto.service.EmployeeService; 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.HttpStatus;
import org.springframework.http.ResponseEntity; 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.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import java.util.List;
import java.util.Optional;
@RestController @RestController
public class EmployeeController { public class EmployeeController {
private final EmployeeService employeeService; private final EmployeeService employeeService;
private final CodeRepository codeRepository; 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.employeeService = employeeService;
this.codeRepository = codeRepository; 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 статус код, смотри документацию к тз * @return статус код, смотри документацию к тз
*/ */
@GetMapping("/api/auth") @GetMapping("/api/{login}/auth")
public ResponseEntity<String> authAttempt(HttpServletRequest request) { public ResponseEntity<String> authAttempt(@PathVariable String login) {
String login = SecurityContextHolder.getContext().getAuthentication().getName();
try { try {
if (employeeService.checkEmployeeExists(login)) { if (employeeService.checkEmployeeExists(login)) {
return ResponseEntity.status(HttpStatus.ACCEPTED).build(); // Логин найден ДВЕСТИ return ResponseEntity.status(HttpStatus.ACCEPTED).build(); // Логин найден ДВЕСТИ
@ -50,28 +75,26 @@ public class EmployeeController {
/** /**
* Получить информацию по емплоеееее * Получить информацию по емплоеееее
* *
* @return емплоеееееее иначе 401 бан бан бан бан, а если ошибка, то 400 БАААААН * @return Return employee info.
* Удален вариант получить 401 статус,так как теперь до этого эндпоинта нельзя достучаться без авторизации
*/ */
@GetMapping("/api/info") @GetMapping("/api/info")
public ResponseEntity<Employee> getEmployeeInfo() { public EmployeeInfoBody getEmployeeInfo() {
String login = SecurityContextHolder.getContext().getAuthentication().getName(); String login = SecurityContextHolder.getContext().getAuthentication().getName();
Employee employee = employeeService.getEmployeeInfo(login).get();
try { EmployeeInfoBody employeeInfoBody = new EmployeeInfoBody();
Optional<Employee> employee = employeeService.getEmployeeInfo(login); employeeInfoBody.setId(employee);
return employee.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()); employeeInfoBody.setEntryList(entryService.getEntriesByEmployee(employee));
} catch ( return employeeInfoBody;
Exception e) // Я понял статус "что-то пошло не так", как то, что произошла какая-то ошибка. Вообще по идее должен быть статус 500, но допустим
{
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
} }
/** /**
* Проверка на дверь открыть не открыть * Проверка на дверь открыть не открыть
*
* @param value код двери * @param value код двери
* @return статус код отвта * @return статус код отвта
*/ */
@PatchMapping("api/{value}/open") @PatchMapping("api/open")
public ResponseEntity<String> openDoorAttempt(@RequestBody Long value) { public ResponseEntity<String> openDoorAttempt(@RequestBody Long value) {
try { try {
String login = SecurityContextHolder.getContext().getAuthentication().getName(); String login = SecurityContextHolder.getContext().getAuthentication().getName();
@ -85,6 +108,10 @@ public class EmployeeController {
} }
employeeService.updateLastVisit(login); employeeService.updateLastVisit(login);
Entry entry = new Entry();
Employee employee = employeeService.getEmployeeByLogin(login);
entry.setEmployee(employee);
entryRepository.save(entry);
return ResponseEntity.status(HttpStatus.ACCEPTED).build(); return ResponseEntity.status(HttpStatus.ACCEPTED).build();
} catch (Exception e) { } catch (Exception e) {

View File

@ -4,13 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*; import javax.persistence.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
@Entity @Entity
@ -34,8 +30,8 @@ public class Employee {
private LocalDateTime lastVisit; private LocalDateTime lastVisit;
// @OneToMany(mappedBy = "employee") @OneToMany(mappedBy = "employee")
// private List<AdminsEmployee> adminsEmployees = new ArrayList<>(); private List<Entry> entries;
public long getId() { public long getId() {
return id; return id;
@ -100,4 +96,12 @@ public class Employee {
public void setRole(String role) { public void setRole(String role) {
this.role = role; this.role = role;
} }
public List<Entry> getEntries() {
return entries;
}
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
} }

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -14,4 +14,5 @@ public interface EmployeeService {
Employee updateEmployeeLogin(Long id, String newLogin); Employee updateEmployeeLogin(Long id, String newLogin);
Employee updateEmployeeAvatar(Long id, String newAvatar); Employee updateEmployeeAvatar(Long id, String newAvatar);
Employee updateEmployeePosition(Long id, String newPosition); Employee updateEmployeePosition(Long id, String newPosition);
Employee getEmployeeByLogin(String login);
} }

View File

@ -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);
}

View File

@ -103,4 +103,9 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setPosition(newPosition); employee.setPosition(newPosition);
employeeRepository.save(employee); employeeRepository.save(employee);
return employee; } return employee; }
@Override
public Employee getEmployeeByLogin(String login) {
return employeeRepository.getByLogin(login).get();
}
} }

View File

@ -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);
}
}