fix: Fix fetching skills

pull/1/head
Lunix-420 2024-11-03 18:47:43 +01:00
parent 84208bafd3
commit 67552e4f1e
7 changed files with 34 additions and 33 deletions

Binary file not shown.

View File

@ -81,7 +81,12 @@ public class SkillsController {
@GetMapping("/secondary-skills")
@ResponseBody
public Iterable<SecondarySkill> getSecondarySkills(@RequestParam Long primarySkillId) {
return secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId);
var list = secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId);
System.out.println("Secondary skills for primary skill " + primarySkillId + ":");
for (SecondarySkill skill : list) {
System.out.println(skill.getDescription());
}
return list;
}
@PostMapping("/save")

View File

@ -1,6 +1,7 @@
package com.maradona.backend.entities;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;
@Entity
@ -13,10 +14,12 @@ public class EmployeeSecondarySkill {
@ManyToOne
@JoinColumn(name = "employee_id", nullable = false)
@JsonBackReference
private Employee employee;
@ManyToOne
@JoinColumn(name = "secondary_skill_id", nullable = false)
@JsonBackReference
private SecondarySkill secondarySkill;
@Column(nullable = false)

View File

@ -1,6 +1,7 @@
package com.maradona.backend.entities;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Data;
import java.util.List;
@ -15,48 +16,30 @@ public class PrimarySkill {
private String description;
@OneToMany(mappedBy = "primarySkill", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<SecondarySkill> secondarySkills;
// Getters and setters
public Long getPsid() {
return psid;
}
public void setPsid(Long psid) {
this.psid = psid;
}
public String getDescription() {
return description;
public Long getPsid() {
return psid;
}
public void setDescription(String description) {
this.description = description;
}
public List<SecondarySkill> getSecondarySkills() {
return secondarySkills;
public String getDescription() {
return description;
}
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();
public List<SecondarySkill> getSecondarySkills() {
return secondarySkills;
}
}

View File

@ -1,6 +1,8 @@
package com.maradona.backend.entities;
import jakarta.persistence.*;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Data;
import java.util.List;
@ -16,9 +18,11 @@ public class SecondarySkill {
@ManyToOne
@JoinColumn(name = "psid", nullable = false)
@JsonBackReference
private PrimarySkill primarySkill;
@OneToMany(mappedBy = "secondarySkill", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<EmployeeSecondarySkill> employeeSecondarySkills;
public void setSsid(Long ssid) {

View File

@ -35,6 +35,7 @@ public class SecondarySkillService {
for (SecondarySkill skill : skills) {
if (skill.getPrimarySkill().getPsid().equals(primarySkillId)) {
result.add(skill);
System.out.println(skill.getDescription());
}
}
return result;

View File

@ -2,6 +2,8 @@
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Skill</title>
<!-- never remove this metadata -->
<div th:replace="~{_metadata :: metadata}"></div>
<link rel="stylesheet" href="/skills.css" />
</head>
<body>
@ -33,11 +35,9 @@
class="form-control"
th:field="*{secondarySkillId}"
>
<option
th:each="secondarySkill : ${secondarySkills}"
th:value="${secondarySkill.ssid}"
th:text="${secondarySkill.description}"
></option>
<option value="" disabled selected>
Select a secondary skill
</option>
</select>
</div>
<div class="form-group">
@ -68,16 +68,21 @@
fetch("/skills/secondary-skills?primarySkillId=" + primarySkillId)
.then((response) => response.json())
.then((data) => {
console.log("Fetch response data:", data);
var secondarySkillSelect =
document.getElementById("secondarySkill");
secondarySkillSelect.innerHTML = "";
secondarySkillSelect.innerHTML =
'<option value="" disabled selected>Select a secondary skill</option>';
data.forEach(function (secondarySkill) {
var option = document.createElement("option");
option.value = secondarySkill.ssid;
option.text = secondarySkill.description;
secondarySkillSelect.add(option);
});
});
})
.catch((error) =>
console.error("Error fetching secondary skills:", error)
);
});
</script>
</body>