feat: Relation between projects and employees
parent
4520f418f7
commit
4bdbc34452
|
@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.maradona.backend.entities.Employee;
|
||||||
import com.maradona.backend.entities.Project;
|
import com.maradona.backend.entities.Project;
|
||||||
import com.maradona.backend.services.actions.ProjectActions;
|
import com.maradona.backend.services.actions.ProjectActions;
|
||||||
import com.maradona.backend.services.details.ProjectDetails;
|
import com.maradona.backend.services.details.ProjectDetails;
|
||||||
|
@ -24,10 +25,12 @@ import com.maradona.backend.services.details.ProjectDetails;
|
||||||
* List of endpoints:
|
* List of endpoints:
|
||||||
* - GET /api/project
|
* - GET /api/project
|
||||||
* - GET /api/project/all
|
* - GET /api/project/all
|
||||||
* - GET /api/project/from-user
|
* - GET /api/project/participant/all
|
||||||
* - POST /api/project
|
* - POST /api/project
|
||||||
* - PUT /api/project
|
* - PUT /api/project
|
||||||
|
* - PUT /api/project/participant
|
||||||
* - DELETE /api/project
|
* - DELETE /api/project
|
||||||
|
* - DELETE /api/project/participant
|
||||||
*
|
*
|
||||||
* @see com.maradona.backend.entities.Project
|
* @see com.maradona.backend.entities.Project
|
||||||
* @see com.maradona.backend.services.actions.ProjectActions
|
* @see com.maradona.backend.services.actions.ProjectActions
|
||||||
|
@ -77,6 +80,18 @@ public class ProjectController {
|
||||||
return ResponseEntity.ok(projectDetails.getProjectsByEid(eid));
|
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<Iterable<Employee>> getParticipants(@RequestParam Long pid) {
|
||||||
|
return ResponseEntity.ok(projectDetails.getProjectEmployees(pid));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new project to the database.
|
* Adds a new project to the database.
|
||||||
*
|
*
|
||||||
|
@ -103,6 +118,19 @@ public class ProjectController {
|
||||||
return ResponseEntity.ok(updatedProject);
|
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<Void> addParticipant(@RequestParam Long pid, @RequestParam Long eid) {
|
||||||
|
projectActions.addEmployeeToProject(pid, eid);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a project from the database.
|
* Deletes a project from the database.
|
||||||
*
|
*
|
||||||
|
@ -115,4 +143,18 @@ public class ProjectController {
|
||||||
projectActions.deleteProject(pid);
|
projectActions.deleteProject(pid);
|
||||||
return ResponseEntity.noContent().build();
|
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<Void> removeParticipant(@RequestParam Long pid, @RequestParam Long eid) {
|
||||||
|
projectActions.removeEmployeeFromProject(pid, eid);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -24,7 +24,7 @@ public class Employee {
|
||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "fid")
|
@JoinColumn(name = "form_of_address_fid")
|
||||||
private FormOfAddress formOfAddress;
|
private FormOfAddress formOfAddress;
|
||||||
|
|
||||||
private LocalTime dStart;
|
private LocalTime dStart;
|
||||||
|
@ -34,6 +34,10 @@ public class Employee {
|
||||||
@JsonManagedReference
|
@JsonManagedReference
|
||||||
private List<EmployeeSecondarySkill> secondarySkills;
|
private List<EmployeeSecondarySkill> secondarySkills;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
@JoinTable(name = "employee_project", joinColumns = @JoinColumn(name = "employee_eid"), inverseJoinColumns = @JoinColumn(name = "project_pid"))
|
||||||
|
private List<Project> projects;
|
||||||
|
|
||||||
public void setEid(Long eid) {
|
public void setEid(Long eid) {
|
||||||
this.eid = eid;
|
this.eid = eid;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.maradona.backend.entities;
|
package com.maradona.backend.entities;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ -14,6 +16,9 @@ public class FormOfAddress {
|
||||||
@Column(nullable = false, length = 50)
|
@Column(nullable = false, length = 50)
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "formOfAddress")
|
||||||
|
private List<Employee> employees;
|
||||||
|
|
||||||
public void setFid(Long fid) {
|
public void setFid(Long fid) {
|
||||||
this.fid = fid;
|
this.fid = fid;
|
||||||
}
|
}
|
||||||
|
@ -29,4 +34,12 @@ public class FormOfAddress {
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEmployees(List<Employee> employees) {
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Employee> getEmployees() {
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.maradona.backend.entities;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Data
|
@Data
|
||||||
|
@ -15,6 +16,9 @@ public class Project {
|
||||||
@Column(nullable = false, length = 255)
|
@Column(nullable = false, length = 255)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy = "projects")
|
||||||
|
private List<Employee> employees;
|
||||||
|
|
||||||
private LocalDate startDate;
|
private LocalDate startDate;
|
||||||
private LocalDate endDate;
|
private LocalDate endDate;
|
||||||
|
|
||||||
|
@ -39,6 +43,14 @@ public class Project {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEmployees(List<Employee> employees) {
|
||||||
|
this.employees = employees;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<Employee> getEmployees() {
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
public void setStartDate(LocalDate startDate) {
|
public void setStartDate(LocalDate startDate) {
|
||||||
this.startDate = startDate;
|
this.startDate = startDate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
package com.maradona.backend.services.actions;
|
package com.maradona.backend.services.actions;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.maradona.backend.entities.Employee;
|
||||||
import com.maradona.backend.entities.Project;
|
import com.maradona.backend.entities.Project;
|
||||||
import com.maradona.backend.repositories.ProjectRepository;
|
import com.maradona.backend.repositories.ProjectRepository;
|
||||||
|
import com.maradona.backend.services.details.EmployeeDetails;
|
||||||
|
import com.maradona.backend.services.details.ProjectDetails;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProjectActions {
|
public class ProjectActions {
|
||||||
|
@ -11,6 +17,12 @@ public class ProjectActions {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProjectRepository projectRepository;
|
private ProjectRepository projectRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProjectDetails projectDetails;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmployeeDetails employeeDetails;
|
||||||
|
|
||||||
public Project saveProject(Project project) {
|
public Project saveProject(Project project) {
|
||||||
return projectRepository.save(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<Employee>();
|
||||||
|
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<Employee>();
|
||||||
|
for (Employee currentEmployee : oldEmployees) {
|
||||||
|
if (!currentEmployee.getEid().equals(eid)) {
|
||||||
|
employeeList.add(currentEmployee);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
project.setEmployees(employeeList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,12 @@ package com.maradona.backend.services.details;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.maradona.backend.entities.Employee;
|
||||||
import com.maradona.backend.entities.Project;
|
import com.maradona.backend.entities.Project;
|
||||||
import com.maradona.backend.repositories.ProjectRepository;
|
import com.maradona.backend.repositories.ProjectRepository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -27,8 +30,23 @@ public class ProjectDetails {
|
||||||
return projectRepository.findAll();
|
return projectRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<Employee> getProjectEmployees(Long pid) {
|
||||||
|
var project = getProjectByPid(pid).orElse(null);
|
||||||
|
var employees = project.getEmployees();
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
|
||||||
public Iterable<Project> getProjectsByEid(Long eid) {
|
public Iterable<Project> getProjectsByEid(Long eid) {
|
||||||
// TODO: Actually filter by user
|
var projects = projectRepository.findAll();
|
||||||
return projectRepository.findAll();
|
var employeeProjects = new ArrayList<Project>();
|
||||||
|
for (Project project : projects) {
|
||||||
|
var employees = project.getEmployees();
|
||||||
|
for (Employee employee : employees) {
|
||||||
|
if (employee.getEid().equals(eid)) {
|
||||||
|
employeeProjects.add(project);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return employeeProjects;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue