From 4bdbc3445232c39823a3161fe54efe5d4f847888 Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Mon, 11 Nov 2024 18:28:04 +0100 Subject: [PATCH] feat: Relation between projects and employees --- .../controllers/api/ProjectController.java | 44 ++++++++++++++++++- .../maradona/backend/entities/Employee.java | 6 ++- .../backend/entities/FormOfAddress.java | 13 ++++++ .../maradona/backend/entities/Project.java | 12 +++++ .../services/actions/ProjectActions.java | 36 +++++++++++++++ .../services/details/ProjectDetails.java | 22 +++++++++- 6 files changed, 129 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/maradona/backend/controllers/api/ProjectController.java b/src/main/java/com/maradona/backend/controllers/api/ProjectController.java index 61f38f5..8aa501b 100644 --- a/src/main/java/com/maradona/backend/controllers/api/ProjectController.java +++ b/src/main/java/com/maradona/backend/controllers/api/ProjectController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Project; import com.maradona.backend.services.actions.ProjectActions; import com.maradona.backend.services.details.ProjectDetails; @@ -24,10 +25,12 @@ import com.maradona.backend.services.details.ProjectDetails; * List of endpoints: * - GET /api/project * - GET /api/project/all - * - GET /api/project/from-user + * - GET /api/project/participant/all * - POST /api/project * - PUT /api/project + * - PUT /api/project/participant * - DELETE /api/project + * - DELETE /api/project/participant * * @see com.maradona.backend.entities.Project * @see com.maradona.backend.services.actions.ProjectActions @@ -77,6 +80,18 @@ public class ProjectController { return ResponseEntity.ok(projectDetails.getProjectsByEid(eid)); } + /** + * Returns a list of employees that participate in a given project. + * + * @param pid ID of the project + * @return list of employees + * @see com.maradona.backend.entities.Employee + */ + @GetMapping("/participant/all") + public ResponseEntity> getParticipants(@RequestParam Long pid) { + return ResponseEntity.ok(projectDetails.getProjectEmployees(pid)); + } + /** * Adds a new project to the database. * @@ -103,6 +118,19 @@ public class ProjectController { return ResponseEntity.ok(updatedProject); } + /** + * Adds an employee to a project. The employee is identified by their ID. + * + * @param pid The ID of the project + * @param eid The ID of the employee + * @return + */ + @PutMapping("/participant") + public ResponseEntity addParticipant(@RequestParam Long pid, @RequestParam Long eid) { + projectActions.addEmployeeToProject(pid, eid); + return ResponseEntity.noContent().build(); + } + /** * Deletes a project from the database. * @@ -115,4 +143,18 @@ public class ProjectController { projectActions.deleteProject(pid); return ResponseEntity.noContent().build(); } + + /** + * Removes an employee from a project. The employee is identified by their ID. + * + * @param pid The ID of the project + * @param eid The ID of the employee + * @return HTTP status indicating the outcome of the operation + * @see com.maradona.backend.entities.Project + */ + @DeleteMapping("/participant") + public ResponseEntity removeParticipant(@RequestParam Long pid, @RequestParam Long eid) { + projectActions.removeEmployeeFromProject(pid, eid); + return ResponseEntity.noContent().build(); + } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/entities/Employee.java b/src/main/java/com/maradona/backend/entities/Employee.java index 5cf719e..f596cf5 100644 --- a/src/main/java/com/maradona/backend/entities/Employee.java +++ b/src/main/java/com/maradona/backend/entities/Employee.java @@ -24,7 +24,7 @@ public class Employee { private String lastName; @ManyToOne - @JoinColumn(name = "fid") + @JoinColumn(name = "form_of_address_fid") private FormOfAddress formOfAddress; private LocalTime dStart; @@ -34,6 +34,10 @@ public class Employee { @JsonManagedReference private List secondarySkills; + @ManyToMany + @JoinTable(name = "employee_project", joinColumns = @JoinColumn(name = "employee_eid"), inverseJoinColumns = @JoinColumn(name = "project_pid")) + private List projects; + public void setEid(Long eid) { this.eid = eid; } diff --git a/src/main/java/com/maradona/backend/entities/FormOfAddress.java b/src/main/java/com/maradona/backend/entities/FormOfAddress.java index 3bf7cac..25df59c 100644 --- a/src/main/java/com/maradona/backend/entities/FormOfAddress.java +++ b/src/main/java/com/maradona/backend/entities/FormOfAddress.java @@ -1,5 +1,7 @@ package com.maradona.backend.entities; +import java.util.List; + import jakarta.persistence.*; import lombok.Data; @@ -14,6 +16,9 @@ public class FormOfAddress { @Column(nullable = false, length = 50) private String description; + @OneToMany(mappedBy = "formOfAddress") + private List employees; + public void setFid(Long fid) { this.fid = fid; } @@ -29,4 +34,12 @@ public class FormOfAddress { public String getDescription() { return description; } + + public void setEmployees(List employees) { + this.employees = employees; + } + + public List getEmployees() { + return employees; + } } diff --git a/src/main/java/com/maradona/backend/entities/Project.java b/src/main/java/com/maradona/backend/entities/Project.java index c7f92cb..7f8f937 100644 --- a/src/main/java/com/maradona/backend/entities/Project.java +++ b/src/main/java/com/maradona/backend/entities/Project.java @@ -3,6 +3,7 @@ package com.maradona.backend.entities; import jakarta.persistence.*; import lombok.Data; import java.time.LocalDate; +import java.util.List; @Entity @Data @@ -15,6 +16,9 @@ public class Project { @Column(nullable = false, length = 255) private String name; + @ManyToMany(mappedBy = "projects") + private List employees; + private LocalDate startDate; private LocalDate endDate; @@ -39,6 +43,14 @@ public class Project { return name; } + public void setEmployees(List employees) { + this.employees = employees; + } + + public Iterable getEmployees() { + return employees; + } + public void setStartDate(LocalDate startDate) { this.startDate = startDate; } diff --git a/src/main/java/com/maradona/backend/services/actions/ProjectActions.java b/src/main/java/com/maradona/backend/services/actions/ProjectActions.java index c1d974b..d44b57d 100644 --- a/src/main/java/com/maradona/backend/services/actions/ProjectActions.java +++ b/src/main/java/com/maradona/backend/services/actions/ProjectActions.java @@ -1,9 +1,15 @@ package com.maradona.backend.services.actions; +import java.util.ArrayList; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; + +import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Project; import com.maradona.backend.repositories.ProjectRepository; +import com.maradona.backend.services.details.EmployeeDetails; +import com.maradona.backend.services.details.ProjectDetails; @Service public class ProjectActions { @@ -11,6 +17,12 @@ public class ProjectActions { @Autowired private ProjectRepository projectRepository; + @Autowired + private ProjectDetails projectDetails; + + @Autowired + private EmployeeDetails employeeDetails; + public Project saveProject(Project project) { return projectRepository.save(project); } @@ -23,4 +35,28 @@ public class ProjectActions { } } } + + public void addEmployeeToProject(Long pid, Long eid) { + var project = projectDetails.getProjectByPid(pid).orElse(null); + var newEmployee = employeeDetails.getEmployeeByEid(eid).orElse(null); + var oldEmployees = project.getEmployees(); + var employeeList = new ArrayList(); + for (Employee currentEmployee : oldEmployees) { + employeeList.add(currentEmployee); + } + employeeList.add(newEmployee); + project.setEmployees(employeeList); + } + + public void removeEmployeeFromProject(Long pid, Long eid) { + var project = projectDetails.getProjectByPid(pid).orElse(null); + var oldEmployees = project.getEmployees(); + var employeeList = new ArrayList(); + for (Employee currentEmployee : oldEmployees) { + if (!currentEmployee.getEid().equals(eid)) { + employeeList.add(currentEmployee); + } + } + project.setEmployees(employeeList); + } } diff --git a/src/main/java/com/maradona/backend/services/details/ProjectDetails.java b/src/main/java/com/maradona/backend/services/details/ProjectDetails.java index 0af40a5..ea54b9b 100644 --- a/src/main/java/com/maradona/backend/services/details/ProjectDetails.java +++ b/src/main/java/com/maradona/backend/services/details/ProjectDetails.java @@ -2,9 +2,12 @@ package com.maradona.backend.services.details; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; + +import com.maradona.backend.entities.Employee; import com.maradona.backend.entities.Project; import com.maradona.backend.repositories.ProjectRepository; +import java.util.ArrayList; import java.util.Optional; @Service @@ -27,8 +30,23 @@ public class ProjectDetails { return projectRepository.findAll(); } + public Iterable getProjectEmployees(Long pid) { + var project = getProjectByPid(pid).orElse(null); + var employees = project.getEmployees(); + return employees; + } + public Iterable getProjectsByEid(Long eid) { - // TODO: Actually filter by user - return projectRepository.findAll(); + var projects = projectRepository.findAll(); + var employeeProjects = new ArrayList(); + for (Project project : projects) { + var employees = project.getEmployees(); + for (Employee employee : employees) { + if (employee.getEid().equals(eid)) { + employeeProjects.add(project); + } + } + } + return employeeProjects; } }