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" />
</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>

View File

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

View File

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

View File

@ -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) {

View File

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

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 updateEmployeeAvatar(Long id, String newAvatar);
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);
employeeRepository.save(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);
}
}