refacor: Rewrite relationships

pull/1/head
Lunix-420 2024-11-03 15:36:39 +01:00
parent ff97011ba3
commit 2bdafcc8d0
5 changed files with 109 additions and 53 deletions

Binary file not shown.

View File

@ -2,9 +2,8 @@ package com.maradona.backend.dto;
import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill;
import java.util.List;
import org.springframework.data.util.Pair;
import java.util.List;
public class SkillDto {
private PrimarySkill primarySkill;

View File

@ -3,6 +3,8 @@ package com.maradona.backend.entities;
import jakarta.persistence.*;
import lombok.Data;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
@Entity
@Data
@ -10,7 +12,7 @@ public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long eid;
private Integer employeeNr;
@ -30,59 +32,95 @@ public class Employee {
private LocalTime dStart;
private LocalTime dEnd;
public void setEmployeeNr(Integer employeeNr) {
this.employeeNr = employeeNr;
@ManyToMany
@JoinTable(name = "employee_secondary_skill", joinColumns = @JoinColumn(name = "employee_id"), inverseJoinColumns = @JoinColumn(name = "secondary_skill_id"))
private List<SecondarySkill> secondarySkills;
@ElementCollection
@CollectionTable(name = "employee_secondary_skill_level", joinColumns = @JoinColumn(name = "employee_id"))
@MapKeyJoinColumn(name = "secondary_skill_id")
@Column(name = "level")
private Map<SecondarySkill, Integer> secondarySkillLevels;
// Getters and setters
public Long getEid() {
return eid;
}
public void setEid(Long eid) {
this.eid = eid;
}
public Integer getEmployeeNr() {
return employeeNr;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
public void setEmployeeNr(Integer employeeNr) {
this.employeeNr = employeeNr;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setFormOfAddress(FormOfAddress formOfAddress) {
this.formOfAddress = formOfAddress;
public void setLastName(String lastName) {
this.lastName = lastName;
}
public FormOfAddress getFormOfAddress() {
return formOfAddress;
}
public void setMail(String mail) {
this.mail = mail;
public void setFormOfAddress(FormOfAddress formOfAddress) {
this.formOfAddress = formOfAddress;
}
public String getMail() {
return mail;
}
public void setDStart(LocalTime dStart) {
this.dStart = dStart;
public void setMail(String mail) {
this.mail = mail;
}
public LocalTime getDStart() {
return dStart;
}
public void setDEnd(LocalTime dEnd) {
this.dEnd = dEnd;
public void setDStart(LocalTime dStart) {
this.dStart = dStart;
}
public LocalTime getDEnd() {
return dEnd;
}
}
public void setDEnd(LocalTime dEnd) {
this.dEnd = dEnd;
}
public List<SecondarySkill> getSecondarySkills() {
return secondarySkills;
}
public void setSecondarySkills(List<SecondarySkill> secondarySkills) {
this.secondarySkills = secondarySkills;
}
public Map<SecondarySkill, Integer> getSecondarySkillLevels() {
return secondarySkillLevels;
}
public void setSecondarySkillLevels(Map<SecondarySkill, Integer> secondarySkillLevels) {
this.secondarySkillLevels = secondarySkillLevels;
}
}

View File

@ -1,11 +1,12 @@
package com.maradona.backend.entities;
import jakarta.persistence.*;
import lombok.Data;
import java.util.List;
@Entity
@Data
public class PrimarySkill {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long psid;
@ -16,27 +17,46 @@ public class PrimarySkill {
@OneToMany(mappedBy = "primarySkill", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SecondarySkill> secondarySkills;
public void setPsid(Long psid) {
this.psid = psid;
}
// Getters and setters
public Long getPsid() {
return psid;
}
public void setDescription(String description) {
this.description = description;
public void setPsid(Long psid) {
this.psid = psid;
}
public String getDescription() {
return description;
}
public void setSecondarySkills(List<SecondarySkill> secondarySkills) {
this.secondarySkills = secondarySkills;
public void setDescription(String description) {
this.description = description;
}
public List<SecondarySkill> getSecondarySkills() {
return secondarySkills;
}
public void setSecondarySkills(List<SecondarySkill> secondarySkills) {
this.secondarySkills = secondarySkills;
}
public void addSecondarySkill(SecondarySkill secondarySkill) {
secondarySkills.add(secondarySkill);
secondarySkill.setPrimarySkill(this);
}
public void removeSecondarySkill(SecondarySkill secondarySkill) {
secondarySkills.remove(secondarySkill);
secondarySkill.setPrimarySkill(null);
}
public void removeAllSecondarySkills() {
for (SecondarySkill secondarySkill : secondarySkills) {
secondarySkill.setPrimarySkill(null);
}
secondarySkills.clear();
}
}

View File

@ -1,8 +1,10 @@
package com.maradona.backend.services;
import com.maradona.backend.dto.SkillDto;
import com.maradona.backend.entities.Employee;
import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.repositories.EmployeeRepository;
import com.maradona.backend.repositories.PrimarySkillRepository;
import com.maradona.backend.repositories.SecondarySkillRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,6 +13,7 @@ import org.springframework.data.util.Pair;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class SkillService {
@ -21,6 +24,9 @@ public class SkillService {
@Autowired
private SecondarySkillRepository secondarySkillRepository;
@Autowired
private EmployeeRepository employeeRepository;
public Iterable<SkillDto> getAllSkills() {
Iterable<PrimarySkill> primarySkills = primarySkillRepository.findAll();
Iterable<SecondarySkill> secondarySkills = secondarySkillRepository.findAll();
@ -29,10 +35,8 @@ public class SkillService {
for (PrimarySkill primarySkill : primarySkills) {
List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
for (SecondarySkill secondarySkill : secondarySkills) {
var currentSecondarySkillID = secondarySkill.getPrimarySkill().getPsid();
var currentPrimarySkillID = primarySkill.getPsid();
if (currentSecondarySkillID.equals(currentPrimarySkillID)) {
secondarySkillList.add(Pair.of(secondarySkill, 3));
if (secondarySkill.getPrimarySkill().getPsid().equals(primarySkill.getPsid())) {
secondarySkillList.add(Pair.of(secondarySkill, 3)); // Placeholder level
}
}
skills.add(new SkillDto(primarySkill, secondarySkillList));
@ -40,28 +44,23 @@ public class SkillService {
return skills;
}
// public Iterable<SkillDto> getUserSkills() {
// // Assuming you have a way to get the user's skills and levels
// // For now, we will filter the skills for the user with ID 1
// Iterable<PrimarySkill> primarySkills = primarySkillRepository.findAll();
// Iterable<SecondarySkill> secondarySkills =
// secondarySkillRepository.findAll();
// List<SkillDto> skills = new ArrayList<>();
public Iterable<SkillDto> getUserSkills(Long userId) {
Employee employee = employeeRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("Employee not found"));
Map<SecondarySkill, Integer> secondarySkillLevels = employee.getSecondarySkillLevels();
List<SkillDto> skills = new ArrayList<>();
// for (PrimarySkill primarySkill : primarySkills) {
// List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
// for (SecondarySkill secondarySkill : secondarySkills) {
// var currentSecondarySkillID = secondarySkill.getPrimarySkill().getPsid();
// var currentPrimarySkillID = primarySkill.getPsid();
// if (currentSecondarySkillID.equals(currentPrimarySkillID) &&
// secondarySkill.getUserId().equals(userId)) {
// secondarySkillList.add(Pair.of(secondarySkill, secondarySkill.getLevel()));
// }
// }
// if (!secondarySkillList.isEmpty()) {
// skills.add(new SkillDto(primarySkill, secondarySkillList));
// }
// }
// return skills;
// }
for (PrimarySkill primarySkill : primarySkillRepository.findAll()) {
List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
for (Map.Entry<SecondarySkill, Integer> entry : secondarySkillLevels.entrySet()) {
if (entry.getKey().getPrimarySkill().getPsid().equals(primarySkill.getPsid())) {
secondarySkillList.add(Pair.of(entry.getKey(), entry.getValue()));
}
}
if (!secondarySkillList.isEmpty()) {
skills.add(new SkillDto(primarySkill, secondarySkillList));
}
}
return skills;
}
}