From 0706bd294ad5ad580354653667271578735594b2 Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Fri, 8 Nov 2024 14:58:29 +0100 Subject: [PATCH 1/8] fix: Circular inclusion --- src/main/java/com/maradona/backend/entities/Employee.java | 3 ++- .../com/maradona/backend/entities/EmployeeSecondarySkill.java | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/maradona/backend/entities/Employee.java b/src/main/java/com/maradona/backend/entities/Employee.java index 60eeb84..0c211a1 100644 --- a/src/main/java/com/maradona/backend/entities/Employee.java +++ b/src/main/java/com/maradona/backend/entities/Employee.java @@ -2,6 +2,7 @@ package com.maradona.backend.entities; import jakarta.persistence.*; import lombok.Data; +import com.fasterxml.jackson.annotation.JsonManagedReference; import java.time.LocalTime; import java.util.List; import java.util.Map; @@ -10,7 +11,6 @@ import java.util.stream.Collectors; @Entity @Data public class Employee { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -31,6 +31,7 @@ public class Employee { private LocalTime dEnd; @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true) + @JsonManagedReference private List secondarySkills; public void setEmployeeNr(Integer employeeNr) { diff --git a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java index 8d7985f..5c17779 100644 --- a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java +++ b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java @@ -1,13 +1,12 @@ package com.maradona.backend.entities; import jakarta.persistence.*; -import com.fasterxml.jackson.annotation.JsonBackReference; import lombok.Data; +import com.fasterxml.jackson.annotation.JsonBackReference; @Entity @Data public class EmployeeSecondarySkill { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -19,7 +18,6 @@ public class EmployeeSecondarySkill { @ManyToOne @JoinColumn(name = "secondary_skill_id", nullable = false) - @JsonBackReference private SecondarySkill secondarySkill; @Column(nullable = false) From 3b538f8e5696fe0b8a01c8a9209016540515955b Mon Sep 17 00:00:00 2001 From: mohammad Date: Fri, 8 Nov 2024 16:57:33 +0100 Subject: [PATCH 2/8] test: "Adding tests for EmployeeController and ProjectController" --- .../api/EmployeeControllerTest.java | 57 +++++ .../api/ProjectControllerTest.java | 199 ++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java create mode 100644 src/test/java/com/maradona/backend/controllers/api/ProjectControllerTest.java diff --git a/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java new file mode 100644 index 0000000..15b3048 --- /dev/null +++ b/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java @@ -0,0 +1,57 @@ +package com.maradona.backend.controllers.api; + +import com.maradona.backend.controllers.api.EmployeeController; +import com.maradona.backend.entities.Employee; +import com.maradona.backend.services.EmployeeService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalTime; +import java.util.Optional; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@WebMvcTest(EmployeeController.class) +public class EmployeeControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private EmployeeService employeeService; + + @Test + public void testGetEmployeeById() throws Exception { + // Arrange: Mock an employee + Employee employee = new Employee(); + employee.setId(1L); + employee.setEmployeeNr(123); + employee.setFirstName("John"); + employee.setLastName("Doe"); + employee.setDStart(LocalTime.of(9, 0)); + employee.setDEnd(LocalTime.of(17, 0)); + + // Assuming FormOfAddress and EmployeeSecondarySkill are also set up if needed for your test. + + when(employeeService.getEmployeeById(1L)).thenReturn(Optional.of(employee)); + + // Act & Assert: Send GET request and expect a 200 OK status and JSON response + mockMvc.perform(get("/api/employee") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.employeeNr").value(123)) + .andExpect(jsonPath("$.firstName").value("John")) + .andExpect(jsonPath("$.lastName").value("Doe")) + .andExpect(jsonPath("$.dStart").value("09:00:00")) + .andExpect(jsonPath("$.dEnd").value("17:00:00")); + } +} diff --git a/src/test/java/com/maradona/backend/controllers/api/ProjectControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/ProjectControllerTest.java new file mode 100644 index 0000000..8cc4f99 --- /dev/null +++ b/src/test/java/com/maradona/backend/controllers/api/ProjectControllerTest.java @@ -0,0 +1,199 @@ +package com.maradona.backend.controllers.api; + +import com.maradona.backend.controllers.api.ProjectController; +import com.maradona.backend.entities.Project; +import com.maradona.backend.services.ProjectService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@WebMvcTest(ProjectController.class) +public class ProjectControllerTest { + // MockMvc to simulate HTTP requests to the controller + @Autowired + private MockMvc mockMvc; + + // Mocked ProjectService to simulate service calls + @MockBean + private ProjectService projectService; + + @Test + public void testGetProjectById() throws Exception { + //Arrange: Mock an project + Project project = new Project(); + project.setId(1L); + project.setName("Skillmanagementsystem erstellen"); + project.setStartDate(LocalDate.of(2024, 11,8)); + project.setEndDate(LocalDate.of(2024, 11,20)); + project.setWorkload(12); + project.setDescription("Skillmanagementsystem erstellen für die Firma"); + + //Define the behavior of the mocked ProjectService: return the project when ID 1 is requested + when(projectService.getProjectById(1L)).thenReturn(Optional.of(project)); + + //Act & Assert: Send GET request an expect a 200 OK status and JSON response + //GET /api/project/ + mockMvc.perform(get("/api/project") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.name").value("Skillmanagementsystem erstellen")) + .andExpect(jsonPath("$.startDate").value("2024-11-08")) + .andExpect(jsonPath("$.endDate").value("2024-11-20")) + .andExpect(jsonPath("$.workload").value(12)) + .andExpect(jsonPath("$.description").value("Skillmanagementsystem erstellen für die Firma")); + } + + @Test + public void testGetAllProjects() throws Exception { + //Arrange: Creat a list of mock projects + Project project = new Project(); + project.setId(1L); + project.setName("Skillmanagementsystem erstellen"); + project.setStartDate(LocalDate.of(2024, 11,8)); + project.setEndDate(LocalDate.of(2024, 11,20)); + project.setWorkload(12); + project.setDescription("Skillmanagementsystem erstellen für die Firma"); + + Project project2 = new Project(); + project2.setId(2L); + project2.setName("EAFC 25"); + project2.setStartDate(LocalDate.of(2024, 11,20)); + project2.setEndDate(LocalDate.of(2024, 11,30)); + project2.setWorkload(2); + project2.setDescription("Entwicklung von EAFC 25 für neues Spaß erlebnis"); + + //Define the behavior of the mocked ProjectService: return a list of projects when requested + when(projectService.getAllProjects()).thenReturn(Arrays.asList(project, project2)); + + //Act & Assert: Send GET request an expect a 200 Ok status and JSON response with the list of projects + mockMvc.perform(get("/api/project/all") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].name").value("Skillmanagementsystem erstellen")) + .andExpect(jsonPath("$[0].startDate").value("2024-11-08")) + .andExpect(jsonPath("$[0].endDate").value("2024-11-20")) + .andExpect(jsonPath("$[0].workload").value(12)) + .andExpect(jsonPath("$[0].description").value("Skillmanagementsystem erstellen für die Firma")) + .andExpect(jsonPath("$[1].id").value(2)) + .andExpect(jsonPath("$[1].name").value("EAFC 25")) + .andExpect(jsonPath("$[1].startDate").value("2024-11-20")) + .andExpect(jsonPath("$[1].endDate").value("2024-11-30")) + .andExpect(jsonPath("$[1].workload").value(2)) + .andExpect(jsonPath("$[1].description").value("Entwicklung von EAFC 25 für neues Spaß erlebnis")); + } + + @Test + public void testGetProjectsByUserId() throws Exception { + // Arrange: Mock projects for a specific user + Project project1 = new Project(); + project1.setId(1L); + project1.setName("Skill Management System"); + project1.setStartDate(LocalDate.of(2024, 11, 8)); + project1.setEndDate(LocalDate.of(2024, 11, 20)); + project1.setWorkload(12); + project1.setDescription("Create a skill management system for the company"); + + Project project2 = new Project(); + project2.setId(2L); + project2.setName("Project Management Tool"); + project2.setStartDate(LocalDate.of(2024, 12, 1)); + project2.setEndDate(LocalDate.of(2024, 12, 15)); + project2.setWorkload(10); + project2.setDescription("Develop a project management tool"); + + Long userId = 123L; + + // Mock the ProjectService to return projects for a specific user + when(projectService.getProjectsByUserId(userId)).thenReturn(Arrays.asList(project1, project2)); + + // Act & Assert: Send GET request and expect a 200 OK status with the correct JSON response + mockMvc.perform(get("/api/project/from-user") + .param("userId", String.valueOf(userId)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].name").value("Skill Management System")) + .andExpect(jsonPath("$[1].id").value(2)) + .andExpect(jsonPath("$[1].name").value("Project Management Tool")); + } + + @Test + public void testCreateProject() throws Exception { + // Arrange: Create a new project and set all required fields + Project savedProject = new Project(); + savedProject.setId(1L); // ID setzen + savedProject.setName("New Project"); + savedProject.setStartDate(LocalDate.of(2024, 11, 10)); + savedProject.setEndDate(LocalDate.of(2024, 12, 10)); + savedProject.setWorkload(15); + savedProject.setDescription("A new project for testing"); + + // Mocking the saveProject method to return this specific project + when(projectService.saveProject(any(Project.class))).thenReturn(savedProject); + + // Act & Assert: Send POST request and check for 201 Created with JSON response + mockMvc.perform(post("/api/project") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"name\":\"New Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":15,\"description\":\"A new project for testing\"}")) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.id").value(1)) // Ensure the response JSON contains id = 1 + .andExpect(jsonPath("$.name").value("New Project")) + .andExpect(jsonPath("$.workload").value(15)) + .andExpect(jsonPath("$.description").value("A new project for testing")); + } + + @Test + public void testUpdateProject() throws Exception { + // Arrange: Create an existing project + Project project = new Project(); + project.setId(1L); + project.setName("Updated Project"); + project.setStartDate(LocalDate.of(2024, 11, 10)); + project.setEndDate(LocalDate.of(2024, 12, 10)); + project.setWorkload(20); + project.setDescription("An updated project description"); + + // Mock the ProjectService to return the updated project + when(projectService.saveProject(project)).thenReturn(project); + + // Act & Assert: Send PUT request and expect a 200 OK status with the updated project as JSON response + mockMvc.perform(put("/api/project") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"id\":1,\"name\":\"Updated Project\",\"startDate\":\"2024-11-10\",\"endDate\":\"2024-12-10\",\"workload\":20,\"description\":\"An updated project description\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.name").value("Updated Project")) + .andExpect(jsonPath("$.workload").value(20)) + .andExpect(jsonPath("$.description").value("An updated project description")); + } + + @Test + public void testDeleteProject() throws Exception { + // Arrange: Define the ID of the project to delete + Long projectId = 1L; + + // No need to mock the delete method as it returns void, we just ensure no exception is thrown + + // Act & Assert: Send DELETE request and expect a 204 No Content status + mockMvc.perform(delete("/api/project") + .param("id", String.valueOf(projectId)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } +} From c97b33fdc39e5988356bff915a9551b9cabff708 Mon Sep 17 00:00:00 2001 From: 3001962 <3001962@stud.hs-mannheim.de> Date: Sat, 9 Nov 2024 14:51:18 +0100 Subject: [PATCH 3/8] test: Add unit tests for primaryskillcontrolle endpoints with mock validation --- .../api/PrimarySkillControllerTest.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/test/java/com/maradona/backend/controllers/api/PrimarySkillControllerTest.java diff --git a/src/test/java/com/maradona/backend/controllers/api/PrimarySkillControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/PrimarySkillControllerTest.java new file mode 100644 index 0000000..70f5e1a --- /dev/null +++ b/src/test/java/com/maradona/backend/controllers/api/PrimarySkillControllerTest.java @@ -0,0 +1,140 @@ +package com.maradona.backend.controllers.api; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.maradona.backend.entities.PrimarySkill; +import com.maradona.backend.services.PrimarySkillService; +import com.maradona.backend.services.SecondarySkillService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import java.util.List; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; + +public class PrimarySkillControllerTest { + + private MockMvc mockMvc; + + @Mock + private PrimarySkillService primarySkillService; + + @Mock + private SecondarySkillService secondarySkillService; + + @InjectMocks + private PrimarySkillController primarySkillController; + + private PrimarySkill primarySkill; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + mockMvc = MockMvcBuilders.standaloneSetup(primarySkillController).build(); + + // Initialisieren eines Beispiels für PrimarySkill + primarySkill = new PrimarySkill(); + primarySkill.setPsid(1L); + primarySkill.setDescription("Test Primary Skill"); + } + + @Test + public void testGetPrimarySkillById_Success() throws Exception { + // Mock das Service, um ein PrimarySkill zu liefern + Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.of(primarySkill)); + + // Führe die GET-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill") + .param("id", "1")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) + .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); + + // Verifiziere, dass der Service aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.times(1)).getPrimarySkillById(1L); + } + + @Test + public void testGetAllPrimarySkills() throws Exception { + // Mock das Service, um eine Liste von PrimarySkills zu liefern + Mockito.when(primarySkillService.getAllPrimarySkills()).thenReturn(List.of(primarySkill)); + + // Führe die GET-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill/all")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$[0].psid").value(1)) + .andExpect(MockMvcResultMatchers.jsonPath("$[0].description").value("Test Primary Skill")); + + // Verifiziere, dass der Service aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.times(1)).getAllPrimarySkills(); + } + + @Test + public void testCreatePrimarySkill() throws Exception { + // Mock das Service, um das PrimarySkill zu speichern + Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill); + + // Führe die POST-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.post("/api/primary-skill") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(primarySkill))) // Hier wird das PrimarySkill als JSON übermittelt + .andExpect(MockMvcResultMatchers.status().isCreated()) // Erwartet den Statuscode 201 Created + .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) // Überprüft das psid in der Antwort + .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); // Überprüft die Beschreibung + + // Verifiziere, dass der Service aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class)); + } + + + @Test + public void testUpdatePrimarySkill() throws Exception { + // Mock das Service, um das PrimarySkill zu speichern + Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill); + + // Führe die PUT-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.put("/api/primary-skill") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(primarySkill))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) + .andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); + + // Verifiziere, dass der Service aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class)); + } + + @Test + public void testDeletePrimarySkill_Success() throws Exception { + // Mock das Service, um das PrimarySkill zu liefern + Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.of(primarySkill)); + + // Führe die DELETE-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill") + .param("id", "1")) + .andExpect(MockMvcResultMatchers.status().isNoContent()); + + // Verifiziere, dass der Service aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.times(1)).deletePrimarySkill(1L); + } + + @Test + public void testDeletePrimarySkill_NotFound() throws Exception { + // Mock das Service, um das PrimarySkill nicht zu finden + Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.empty()); + + // Führe die DELETE-Anfrage aus + mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill") + .param("id", "1")) + .andExpect(MockMvcResultMatchers.status().isNotFound()); + + // Verifiziere, dass der Service nicht aufgerufen wurde + Mockito.verify(primarySkillService, Mockito.never()).deletePrimarySkill(1L); + } +} From d16c98ad04786b880db6b21034c73cdfab7d90d0 Mon Sep 17 00:00:00 2001 From: 3001291 <3001291@stud.hs-mannheim.de> Date: Sat, 9 Nov 2024 15:42:09 +0100 Subject: [PATCH 4/8] test: Add FormOfAdressControllerTest.java and SecondarySkillControllerTest.java --- .../api/FormOfAdressControllerTest.java | 131 ++++++++++++++++++ .../api/SecondarySkillControllerTest.java | 130 +++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 src/test/java/com/maradona/backend/controllers/api/FormOfAdressControllerTest.java create mode 100644 src/test/java/com/maradona/backend/controllers/api/SecondarySkillControllerTest.java diff --git a/src/test/java/com/maradona/backend/controllers/api/FormOfAdressControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/FormOfAdressControllerTest.java new file mode 100644 index 0000000..e7caa7d --- /dev/null +++ b/src/test/java/com/maradona/backend/controllers/api/FormOfAdressControllerTest.java @@ -0,0 +1,131 @@ +package com.maradona.backend.controllers.api; + +import com.maradona.backend.entities.FormOfAddress; +import com.maradona.backend.services.FormOfAddressService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Optional; +import java.util.Arrays; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@WebMvcTest(FormOfAdressController.class) +public class FormOfAdressControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private FormOfAddressService formOfAdressService; + + @Test + public void testGetFormOfAdressById() throws Exception { + FormOfAddress formOfAddress = new FormOfAddress(); + formOfAddress.setId(1L); + formOfAddress.setDescription("Mr."); + + when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(formOfAddress)); + + mockMvc.perform(get("/api/form-of-adress") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.description").value("Mr.")); + } + + @Test + public void testGetAllFormOfAdresses() throws Exception { + FormOfAddress form1 = new FormOfAddress(); + form1.setId(1L); + form1.setDescription("Mr."); + + FormOfAddress form2 = new FormOfAddress(); + form2.setId(2L); + form2.setDescription("Ms."); + + when(formOfAdressService.getAllFormOfAddresses()).thenReturn(Arrays.asList(form1, form2)); + + mockMvc.perform(get("/api/form-of-adress/all") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].description").value("Mr.")) + .andExpect(jsonPath("$[1].id").value(2)) + .andExpect(jsonPath("$[1].description").value("Ms.")); + } + + @Test + public void testCreateFormOfAdress() throws Exception { + FormOfAddress formOfAddress = new FormOfAddress(); + formOfAddress.setId(1L); + formOfAddress.setDescription("Dr."); + + when(formOfAdressService.saveFormOfAddress(any(FormOfAddress.class))).thenReturn(formOfAddress); + + mockMvc.perform(post("/api/form-of-adress") + .contentType(MediaType.APPLICATION_JSON) + .content("\"Dr.\"")) + .andExpect(status().isOk()); + } + + @Test + public void testUpdateFormOfAdress() throws Exception { + FormOfAddress existingForm = new FormOfAddress(); + existingForm.setId(1L); + existingForm.setDescription("Mr."); + + when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(existingForm)); + + mockMvc.perform(put("/api/form-of-adress") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON) + .content("\"Prof.\"")) + .andExpect(status().isOk()); + } + + @Test + public void testUpdateFormOfAdressNotFound() throws Exception { + when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.empty()); + + mockMvc.perform(put("/api/form-of-adress") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON) + .content("\"Prof.\"")) + .andExpect(status().isNotFound()); + } + + @Test + public void testDeleteFormOfAdress() throws Exception { + FormOfAddress formOfAddress = new FormOfAddress(); + formOfAddress.setId(1L); + formOfAddress.setDescription("Mr."); + + when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.of(formOfAddress)); + + mockMvc.perform(delete("/api/form-of-adress") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + @Test + public void testDeleteFormOfAdressNotFound() throws Exception { + when(formOfAdressService.getFormOfAddressById(1L)).thenReturn(Optional.empty()); + + mockMvc.perform(delete("/api/form-of-adress") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } +} \ No newline at end of file diff --git a/src/test/java/com/maradona/backend/controllers/api/SecondarySkillControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/SecondarySkillControllerTest.java new file mode 100644 index 0000000..f4198b8 --- /dev/null +++ b/src/test/java/com/maradona/backend/controllers/api/SecondarySkillControllerTest.java @@ -0,0 +1,130 @@ +package com.maradona.backend.controllers.api; + +import com.maradona.backend.entities.SecondarySkill; +import com.maradona.backend.services.SecondarySkillService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + +@WebMvcTest(SecondarySkillController.class) +public class SecondarySkillControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private SecondarySkillService secondarySkillService; + + @Test + public void testGetSecondarySkillById() throws Exception { + SecondarySkill skill = new SecondarySkill(); + skill.setSsid(1L); + skill.setDescription("Java Programming"); + + when(secondarySkillService.getSecondarySkillById(1L)).thenReturn(Optional.of(skill)); + + mockMvc.perform(get("/api/secondary-skill") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.ssid").value(1)) + .andExpect(jsonPath("$.description").value("Java Programming")); + } + + @Test + public void testGetAllSecondarySkills() throws Exception { + SecondarySkill skill1 = new SecondarySkill(); + skill1.setSsid(1L); + skill1.setDescription("Java Programming"); + + SecondarySkill skill2 = new SecondarySkill(); + skill2.setSsid(2L); + skill2.setDescription("Python Programming"); + + when(secondarySkillService.getAllSecondarySkills()).thenReturn(Arrays.asList(skill1, skill2)); + + mockMvc.perform(get("/api/secondary-skill/all") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[1].ssid").value(1)) + .andExpect(jsonPath("$[1].description").value("Java Programming")) + .andExpect(jsonPath("$[2].ssid").value(2)) + .andExpect(jsonPath("$[2].description").value("Python Programming")); + } + + @Test + public void testGetSecondarySkillsByPrimarySkillId() throws Exception { + SecondarySkill skill1 = new SecondarySkill(); + skill1.setSsid(1L); + skill1.setDescription("Data Analysis"); + + SecondarySkill skill2 = new SecondarySkill(); + skill2.setSsid(2L); + skill2.setDescription("Data Visualization"); + + when(secondarySkillService.getSecondarySkillsByPrimarySkillId(100L)).thenReturn(Arrays.asList(skill1, skill2)); + + mockMvc.perform(get("/api/secondary-skill/from-primary-skill") + .param("primarySkillId", "100") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[1].ssid").value(1)) + .andExpect(jsonPath("$[1].description").value("Data Analysis")) + .andExpect(jsonPath("$[2].ssid").value(2)) + .andExpect(jsonPath("$[2].description").value("Data Visualization")); + } + + @Test + public void testCreateSecondarySkill() throws Exception { + SecondarySkill skill = new SecondarySkill(); + skill.setSsid(1L); + skill.setDescription("Machine Learning"); + + when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(skill); + + mockMvc.perform(post("/api/secondary-skill") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"name\":\"Machine Learning\"}")) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.ssid").value(1)) + .andExpect(jsonPath("$.description").value("Machine Learning")); + } + + @Test + public void testUpdateSecondarySkill() throws Exception { + SecondarySkill updatedSkill = new SecondarySkill(); + updatedSkill.setSsid(1L); + updatedSkill.setDescription("Advanced Machine Learning"); + + when(secondarySkillService.saveSecondarySkill(any(SecondarySkill.class))).thenReturn(updatedSkill); + + mockMvc.perform(put("/api/secondary-skill") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"ssid\":1,\"description\":\"Advanced Machine Learning\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.ssid").value(1)) + .andExpect(jsonPath("$.description").value("Advanced Machine Learning")); + } + + @Test + public void testDeleteSecondarySkill() throws Exception { + Long skillId = 1L; + + mockMvc.perform(delete("/api/secondary-skill") + .param("id", String.valueOf(skillId)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } +} From 3b3361a646f2a70dcc30c4d22f1b1de0d37b83f5 Mon Sep 17 00:00:00 2001 From: mohammad Date: Mon, 11 Nov 2024 09:31:13 +0100 Subject: [PATCH 5/8] test: Adding new tests for the employeecontrollertest --- .../api/EmployeeControllerTest.java | 118 +++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java b/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java index 15b3048..bb8e442 100644 --- a/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java +++ b/src/test/java/com/maradona/backend/controllers/api/EmployeeControllerTest.java @@ -11,10 +11,11 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.time.LocalTime; +import java.util.List; import java.util.Optional; import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -39,7 +40,6 @@ public class EmployeeControllerTest { employee.setDEnd(LocalTime.of(17, 0)); // Assuming FormOfAddress and EmployeeSecondarySkill are also set up if needed for your test. - when(employeeService.getEmployeeById(1L)).thenReturn(Optional.of(employee)); // Act & Assert: Send GET request and expect a 200 OK status and JSON response @@ -54,4 +54,118 @@ public class EmployeeControllerTest { .andExpect(jsonPath("$.dStart").value("09:00:00")) .andExpect(jsonPath("$.dEnd").value("17:00:00")); } + + //Test if an employee is not found (404 Not Found) + @Test + public void testGetEmployeeByIdNotFound() throws Exception { + //Arrange + when(employeeService.getEmployeeById(999L)).thenReturn(Optional.empty()); + + //Act & Assert: Send GET request and expect a 404 Not Found status + mockMvc.perform(get("/api/employee") + .param("id", "999") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + + + //Testing the getAll method for all employees + @Test + public void testGetAllEmployees() throws Exception { + //Arrange: Mock a list of employees + Employee employee1 = new Employee(); + employee1.setId(1L); + employee1.setEmployeeNr(123); + employee1.setFirstName("Mohammad"); + employee1.setLastName("Hawrami"); + + Employee employee2 = new Employee(); + employee2.setId(2L); + employee2.setEmployeeNr(124); + employee2.setFirstName("Tarik"); + employee2.setLastName("Gökmen"); + + when(employeeService.getAllEmployees()).thenReturn(List.of(employee1, employee2)); + + //Act & Assert: Send GET request and expect a 200 OK status and JSON array response + mockMvc.perform(get("/api/employees/all") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].employeeNr").value(1)) + .andExpect(jsonPath("$[1].employeeNr").value(2)); + } + + + //Testing the getFromSkill method with a specific Skilld and level + @Test + public void testGetEmployeesBySecondarySkill() throws Exception { + //Arrange: Mock a list of employees with specific skills + Employee employee = new Employee(); + employee.setId(1L); + employee.setEmployeeNr(123); + employee.setFirstName("Mohammad"); + employee.setLastName("Hawrami"); + + when(employeeService.getEmployeesBySecondarySkill(1L,2)).thenReturn(List.of(employee)); + + //Act & Assert: Send GET request and expect a 200 OK status and JSON array response + mockMvc.perform(get("/api/employee/from-skill") + .param("skillId", "1") + .param("level", "2") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id").value(1)) + .andExpect(jsonPath("$[0].employeeNr").value(123)); + } + + + //Testing the postSkillPrototype method with a valid SkillPrototype object + @Test + public void testPostSkillPrototype() throws Exception { + //Arrange: Create a SkillPrototype JSON payload + String skillPrototypeJson = "{\"skillId\":\"1\",\"level\":\"3\"}"; + + //Act & Assert: Send POST request and expect a 201 Creat status + mockMvc.perform(post("/api/employee/skill/prototype") + .content(skillPrototypeJson) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()); + } + + + //Testing the putSkillLevel method to update an existing level + @Test + public void testPutSkillLevel() throws Exception { + // Act & Assert: Send PUT request and expect a 200 OK status + mockMvc.perform(put("/api/employee/skill/level") + .param("skillId", "1") + .param("level", "5") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } + + + //Testing the delete method for removing a secondary skill java + @Test + public void testDeleteSecondarySkill() throws Exception { + // Act & Assert: Send DELETE request and expect a 204 No Content status + mockMvc.perform(delete("/api/employee/skill") + .param("id", "1") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()); + } + + + //Testing the postSkillPrototype methode with invalid payload (e.g., no Skilld) + @Test + public void testPostSkillPrototype_BadRequest() throws Exception { + // Arrange: Create an invalid JSON payload (missing skillId) + String invalidSkillPrototypeJson = "{\"level\":3}"; + + // Act & Assert: Send POST request and expect a 400 Bad Request status + mockMvc.perform(post("/api/employee/skill/prototype") + .content(invalidSkillPrototypeJson) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()); + } } From b75614982c5c272cdedcd844ed43ea31bdd28871 Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Mon, 11 Nov 2024 09:42:28 +0100 Subject: [PATCH 6/8] fix: EmployeeId bullshit --- .../controllers/api/EmployeeController.java | 3 ++- .../com/maradona/backend/entities/Employee.java | 10 +++++++++- .../backend/services/EmployeeService.java | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java b/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java index 4a509a4..a9a3fb6 100644 --- a/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java +++ b/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java @@ -48,7 +48,8 @@ public class EmployeeController { */ @GetMapping({ "/", "" }) public ResponseEntity get(@RequestParam Long id) { - return employeeService.getEmployeeById(id) + var employee = employeeService.getEmployeeById(id); + return employee .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } diff --git a/src/main/java/com/maradona/backend/entities/Employee.java b/src/main/java/com/maradona/backend/entities/Employee.java index 0c211a1..5d06051 100644 --- a/src/main/java/com/maradona/backend/entities/Employee.java +++ b/src/main/java/com/maradona/backend/entities/Employee.java @@ -13,7 +13,7 @@ import java.util.stream.Collectors; public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Long eid; private Integer employeeNr; @@ -34,6 +34,14 @@ public class Employee { @JsonManagedReference private List secondarySkills; + public void setEid(Long eid) { + this.eid = eid; + } + + public Long getEid() { + return eid; + } + public void setEmployeeNr(Integer employeeNr) { this.employeeNr = employeeNr; } diff --git a/src/main/java/com/maradona/backend/services/EmployeeService.java b/src/main/java/com/maradona/backend/services/EmployeeService.java index 610c4b8..5d49343 100644 --- a/src/main/java/com/maradona/backend/services/EmployeeService.java +++ b/src/main/java/com/maradona/backend/services/EmployeeService.java @@ -22,11 +22,22 @@ public class EmployeeService { } public Optional getEmployeeById(Long id) { - return employeeRepository.findById(id); + var employees = employeeRepository.findAll(); + for (Employee employee : employees) { + if (employee.getEid().equals(id)) { + return Optional.of(employee); + } + } + return Optional.empty(); } public void deleteEmployee(Long id) { - employeeRepository.deleteById(id); + for (Employee employee : employeeRepository.findAll()) { + if (employee.getEid().equals(id)) { + employeeRepository.delete(employee); + return; + } + } } public Iterable getAllEmployees() { From 5258db4300f713fe04ecea0123c48131fc26d853 Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Mon, 11 Nov 2024 11:38:58 +0100 Subject: [PATCH 7/8] refactor: Rename all IDs --- .../controllers/api/EmployeeController.java | 47 ++++++++++++------- .../api/FormOfAdressController.java | 20 ++++---- .../api/PrimarySkillController.java | 16 +++---- .../controllers/api/ProjectController.java | 22 ++++----- .../api/SecondarySkillController.java | 18 +++---- .../backend/controllers/page/SkillsPage.java | 2 +- .../maradona/backend/dto/SkillPrototype.java | 20 ++++---- .../maradona/backend/entities/Employee.java | 2 +- .../entities/EmployeeSecondarySkill.java | 12 ++--- .../backend/entities/FormOfAddress.java | 10 ++-- .../maradona/backend/entities/Project.java | 10 +++- .../EmployeeSecondarySkillRepository.java | 2 - .../backend/services/EmployeeService.java | 38 ++++++++------- .../services/FormOfAddressService.java | 31 +++++++----- .../backend/services/PrimarySkillService.java | 17 +++++-- .../backend/services/ProjectService.java | 21 +++++++-- .../services/SecondarySkillService.java | 18 ++++--- .../backend/services/SkillService.java | 23 ++++----- 18 files changed, 192 insertions(+), 137 deletions(-) diff --git a/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java b/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java index a9a3fb6..ade2817 100644 --- a/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java +++ b/src/main/java/com/maradona/backend/controllers/api/EmployeeController.java @@ -21,6 +21,7 @@ import com.maradona.backend.services.EmployeeService; * * Endpoints: * - GET /api/employee + * - GET /api/employee/{eid} * - GET /api/employee/all * - GET /api/employee/from-skill * - GET /api/employee/skill/all @@ -40,15 +41,29 @@ public class EmployeeController { Long user = Long.valueOf(1); /** - * Returns a specific employee from the database. + * Returns the employee profile of the currently logged in user. * - * @param id The ID of the requested employee. - * @return The employee with the requested ID. + * @return The employee profile of the currently logged in user. * @see com.maradona.backend.entities.Employee */ @GetMapping({ "/", "" }) - public ResponseEntity get(@RequestParam Long id) { - var employee = employeeService.getEmployeeById(id); + public ResponseEntity get() { + var employee = employeeService.getEmployeeByEid(user); + return employee + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); + } + + /** + * Returns a specific employee from the database. + * + * @param eid The ID of the requested employee. + * @return The employee with the requested ID. + * @see com.maradona.backend.entities.Employee + */ + @GetMapping("/{eid}") + public ResponseEntity get(@RequestParam Long eid) { + var employee = employeeService.getEmployeeByEid(eid); return employee .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); @@ -68,15 +83,15 @@ public class EmployeeController { /** * Returns a list of employees for a given secondary skill and level. * - * @param skillId ID of the secondary skill - * @param level Level of the secondary skill + * @param ssid ID of the secondary skill + * @param level Level of the secondary skill * @return list of employees * @see com.maradona.backend.entities.Employee * @see com.maradona.backend.entities.SecondarySkill */ @GetMapping("/from-skill") - public ResponseEntity> getFromSkill(@RequestParam Long skillId, @RequestParam Integer level) { - return ResponseEntity.ok(employeeService.getEmployeesBySecondarySkill(skillId, level)); + public ResponseEntity> getFromSkill(@RequestParam Long ssid, @RequestParam Integer level) { + return ResponseEntity.ok(employeeService.getEmployeesBySecondarySkill(ssid, level)); } /** @@ -97,30 +112,30 @@ public class EmployeeController { /** * Updates the level of a secondary skill in the user profile. * - * @param skillId ID of the secondary skill - * @param level Level of the secondary skill + * @param ssid ID of the secondary skill + * @param level Level of the secondary skill * @return HTTP status indicating the outcome of the operation * @see com.maradona.backend.entities.Employee * @see com.maradona.backend.entities.SecondarySkill * @see com.maradona.backend.entities.EmployeeSecondarySkill */ @PutMapping("/skill/level") - public ResponseEntity putSkillLevel(@RequestParam Long skillId, @RequestParam Integer level) { - employeeService.updateSecondarySkillLevel(user, skillId, level); + public ResponseEntity putSkillLevel(@RequestParam Long ssid, @RequestParam Integer level) { + employeeService.updateSecondarySkillLevel(user, ssid, level); return ResponseEntity.ok().build(); } /** * Removes a secondary skill from the user profile. * - * @param id The ID of the secondary skill to be removed + * @param ssid The ID of the secondary skill to be removed * @return HTTP status indicating the outcome of the operation * @see com.maradona.backend.entities.Employee * @see com.maradona.backend.entities.SecondarySkill */ @DeleteMapping("/skill") - public ResponseEntity delete(@RequestParam Long id) { - employeeService.deleteSecondarySkillFromEmployee(user, id); + public ResponseEntity delete(@RequestParam Long ssid) { + employeeService.deleteSecondarySkillFromEmployee(user, ssid); return ResponseEntity.noContent().build(); } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/controllers/api/FormOfAdressController.java b/src/main/java/com/maradona/backend/controllers/api/FormOfAdressController.java index 7051ced..feffeef 100644 --- a/src/main/java/com/maradona/backend/controllers/api/FormOfAdressController.java +++ b/src/main/java/com/maradona/backend/controllers/api/FormOfAdressController.java @@ -37,13 +37,13 @@ class FormOfAdressController { /** * Returns the form of address with the given ID. * - * @param id The ID of the form of address to return. + * @param fid The ID of the form of address to return. * @return The description of the form of address with the given ID. * @see com.maradona.backend.entities.FormOfAddress */ @GetMapping({ "/", "" }) - public ResponseEntity getFormOfAdress(@RequestParam Long id) { - return formOfAdressService.getFormOfAddressById(id) + public ResponseEntity getFormOfAdress(@RequestParam Long fid) { + return formOfAdressService.getFormOfAddressByFid(fid) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @@ -78,14 +78,14 @@ class FormOfAdressController { /** * Updates the description of a form of address. * - * @param id The ID of the form of address to update. + * @param fid The ID of the form of address to update. * @param description The new description of the form of address. * @return The updated form of address. * @see com.maradona.backend.entities.FormOfAddress */ @PutMapping({ "/", "" }) - public ResponseEntity updateFormOfAdress(@RequestParam Long id, @RequestBody String description) { - var formOfAddress = formOfAdressService.getFormOfAddressById(id).orElse(null); + public ResponseEntity updateFormOfAdress(@RequestParam Long fid, @RequestBody String description) { + var formOfAddress = formOfAdressService.getFormOfAddressByFid(fid).orElse(null); if (formOfAddress == null) { return ResponseEntity.notFound().build(); } @@ -97,17 +97,17 @@ class FormOfAdressController { /** * Deletes a form of address. * - * @param id The ID of the form of address to delete. + * @param fid The ID of the form of address to delete. * @return The deleted form of address. * @see com.maradona.backend.entities.FormOfAddress */ @DeleteMapping({ "/", "" }) - public ResponseEntity deleteFormOfAdress(@RequestParam Long id) { - var formOfAddress = formOfAdressService.getFormOfAddressById(id).orElse(null); + public ResponseEntity deleteFormOfAdress(@RequestParam Long fid) { + var formOfAddress = formOfAdressService.getFormOfAddressByFid(fid).orElse(null); if (formOfAddress == null) { return ResponseEntity.notFound().build(); } - formOfAdressService.deleteFormOfAddress(id); + formOfAdressService.deleteFormOfAddress(fid); return ResponseEntity.ok().build(); } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/controllers/api/PrimarySkillController.java b/src/main/java/com/maradona/backend/controllers/api/PrimarySkillController.java index db7c709..ba6739d 100644 --- a/src/main/java/com/maradona/backend/controllers/api/PrimarySkillController.java +++ b/src/main/java/com/maradona/backend/controllers/api/PrimarySkillController.java @@ -42,13 +42,13 @@ public class PrimarySkillController { /** * Returns a specific primary skill from the database. * - * @param id The ID of the requested primary skill. + * @param pid The ID of the requested primary skill. * @return The primary skill with the requested ID. * @see com.maradona.backend.entities.PrimarySkill */ @GetMapping({ "/", "" }) - public ResponseEntity get(@RequestParam Long id) { - return primarySkillService.getPrimarySkillById(id) + public ResponseEntity get(@RequestParam Long pid) { + return primarySkillService.getPrimarySkillByPsid(pid) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @@ -93,21 +93,21 @@ public class PrimarySkillController { /** * Deletes a primary skill from the database. * - * @param id The ID of the primary skill to be deleted + * @param pid The ID of the primary skill to be deleted * @return HTTP status indicating the outcome of the operation * @see com.maradona.backend.entities.PrimarySkill */ @DeleteMapping({ "/", "" }) - public ResponseEntity delete(@RequestParam Long id) { - var primarySkill = primarySkillService.getPrimarySkillById(id); + public ResponseEntity delete(@RequestParam Long pid) { + var primarySkill = primarySkillService.getPrimarySkillByPsid(pid); if (primarySkill.isPresent()) { var secondarySkills = secondarySkillService.getAllSecondarySkills(); for (SecondarySkill secondarySkill : secondarySkills) { - if (secondarySkill.getPrimarySkill().getPsid().equals(id)) { + if (secondarySkill.getPrimarySkill().getPsid().equals(pid)) { secondarySkillService.deleteSecondarySkill(secondarySkill.getSsid()); } } - primarySkillService.deletePrimarySkill(id); + primarySkillService.deletePrimarySkill(pid); return ResponseEntity.noContent().build(); } else { return ResponseEntity.notFound().build(); 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 9520a28..4d6823d 100644 --- a/src/main/java/com/maradona/backend/controllers/api/ProjectController.java +++ b/src/main/java/com/maradona/backend/controllers/api/ProjectController.java @@ -41,13 +41,13 @@ public class ProjectController { /** * Returns a specific project from the database. * - * @param id The ID of the requested project. + * @param psid The ID of the requested project. * @return The project with the requested ID. * @see com.maradona.backend.entities.Project */ @GetMapping({ "/", "" }) - public ResponseEntity get(@RequestParam Long id) { - Optional project = projectService.getProjectById(id); + public ResponseEntity get(@RequestParam Long psid) { + Optional project = projectService.getProjectByPid(psid); return project.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @@ -63,15 +63,15 @@ public class ProjectController { } /** - * Returns a list of projects for a given user. + * Returns a list of projects for a given employee. * - * @param userId ID of the user + * @param eid ID of the employee * @return list of projects * @see com.maradona.backend.entities.Project */ - @GetMapping("/from-user") - public ResponseEntity> getFromUser(@RequestParam Long userId) { - return ResponseEntity.ok(projectService.getProjectsByUserId(userId)); + @GetMapping("/from-employee") + public ResponseEntity> getFromUser(@RequestParam Long eid) { + return ResponseEntity.ok(projectService.getProjectsByEid(eid)); } /** @@ -103,13 +103,13 @@ public class ProjectController { /** * Deletes a project from the database. * - * @param id The ID of the project to be deleted + * @param pid The ID of the project to be deleted * @return HTTP status indicating the outcome of the operation * @see com.maradona.backend.entities.Project */ @DeleteMapping({ "/", "" }) - public ResponseEntity delete(@RequestParam Long id) { - projectService.deleteProject(id); + public ResponseEntity delete(@RequestParam Long pid) { + projectService.deleteProject(pid); return ResponseEntity.noContent().build(); } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/controllers/api/SecondarySkillController.java b/src/main/java/com/maradona/backend/controllers/api/SecondarySkillController.java index 78cfc47..26985f6 100644 --- a/src/main/java/com/maradona/backend/controllers/api/SecondarySkillController.java +++ b/src/main/java/com/maradona/backend/controllers/api/SecondarySkillController.java @@ -40,13 +40,13 @@ public class SecondarySkillController { /** * Returns a specific secondary skill from the database. * - * @param id The ID of the requested secondary skill. + * @param ssid The ID of the requested secondary skill. * @return The secondary skill with the requested ID. * @see com.maradona.backend.entities.SecondarySkill */ @GetMapping({ "/", "" }) - public ResponseEntity get(@RequestParam Long id) { - Optional secondarySkill = secondarySkillService.getSecondarySkillById(id); + public ResponseEntity get(@RequestParam Long ssid) { + Optional secondarySkill = secondarySkillService.getSecondarySkillBySsid(ssid); return secondarySkill.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @@ -64,14 +64,14 @@ public class SecondarySkillController { /** * Returns a list of secondary skills for a given primary skill. * - * @param primarySkillId ID of the primary skill + * @param psid ID of the primary skill * @return list of secondary skills * @see com.maradona.backend.entities.SecondarySkill * @see com.maradona.backend.entities.PrimarySkill */ @GetMapping("/from-primary-skill") - public ResponseEntity> getSecondarySkills(@RequestParam Long primarySkillId) { - return ResponseEntity.ok(secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId)); + public ResponseEntity> getSecondarySkills(@RequestParam Long psid) { + return ResponseEntity.ok(secondarySkillService.getSecondarySkillsByPrimarySkillId(psid)); } /** @@ -103,13 +103,13 @@ public class SecondarySkillController { /** * Deletes a secondary skill from the database. * - * @param id The ID of the secondary skill to be deleted + * @param ssid The ID of the secondary skill to be deleted * @return HTTP status indicating the outcome of the operation * @see com.maradona.backend.entities.SecondarySkill */ @DeleteMapping({ "/", "" }) - public ResponseEntity delete(@RequestParam Long id) { - secondarySkillService.deleteSecondarySkill(id); + public ResponseEntity delete(@RequestParam Long ssid) { + secondarySkillService.deleteSecondarySkill(ssid); return ResponseEntity.noContent().build(); } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/controllers/page/SkillsPage.java b/src/main/java/com/maradona/backend/controllers/page/SkillsPage.java index 4611268..c92c075 100644 --- a/src/main/java/com/maradona/backend/controllers/page/SkillsPage.java +++ b/src/main/java/com/maradona/backend/controllers/page/SkillsPage.java @@ -52,7 +52,7 @@ public class SkillsPage { */ @GetMapping({ "/", "" }) public String profile(Model model) { - model.addAttribute("employee", employeeService.getEmployeeById(user).orElse(null)); + model.addAttribute("employee", employeeService.getEmployeeByEid(user).orElse(null)); model.addAttribute("skills", skillService.getUserSkills(user)); return "/pages/skills/overview"; } diff --git a/src/main/java/com/maradona/backend/dto/SkillPrototype.java b/src/main/java/com/maradona/backend/dto/SkillPrototype.java index 3fc33dd..0feb9de 100644 --- a/src/main/java/com/maradona/backend/dto/SkillPrototype.java +++ b/src/main/java/com/maradona/backend/dto/SkillPrototype.java @@ -1,24 +1,24 @@ package com.maradona.backend.dto; public class SkillPrototype { - private Long primarySkillId; - private Long secondarySkillId; + private Long psid; + private Long ssid; private Integer level; - public Long getPrimarySkillId() { - return primarySkillId; + public Long getPsid() { + return psid; } - public void setPrimarySkillId(Long primarySkillId) { - this.primarySkillId = primarySkillId; + public void setPsid(Long psid) { + this.psid = psid; } - public Long getSecondarySkillId() { - return secondarySkillId; + public Long getSsid() { + return ssid; } - public void setSecondarySkillId(Long secondarySkillId) { - this.secondarySkillId = secondarySkillId; + public void setSsid(Long ssid) { + this.ssid = ssid; } public Integer getLevel() { diff --git a/src/main/java/com/maradona/backend/entities/Employee.java b/src/main/java/com/maradona/backend/entities/Employee.java index 5d06051..7029b62 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 = "AID") + @JoinColumn(name = "FID") private FormOfAddress formOfAddress; private LocalTime dStart; diff --git a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java index 5c17779..1cdf4f8 100644 --- a/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java +++ b/src/main/java/com/maradona/backend/entities/EmployeeSecondarySkill.java @@ -9,10 +9,10 @@ import com.fasterxml.jackson.annotation.JsonBackReference; public class EmployeeSecondarySkill { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Long essid; @ManyToOne - @JoinColumn(name = "employee_id", nullable = false) + @JoinColumn(name = "employee_eid", nullable = false) @JsonBackReference private Employee employee; @@ -23,12 +23,12 @@ public class EmployeeSecondarySkill { @Column(nullable = false) private Integer level; - public void setId(Long id) { - this.id = id; + public void setEssid(Long essid) { + this.essid = essid; } - public Long getId() { - return id; + public Long getEssid() { + return essid; } public void setEmployee(Employee employee) { diff --git a/src/main/java/com/maradona/backend/entities/FormOfAddress.java b/src/main/java/com/maradona/backend/entities/FormOfAddress.java index c440eaa..3bf7cac 100644 --- a/src/main/java/com/maradona/backend/entities/FormOfAddress.java +++ b/src/main/java/com/maradona/backend/entities/FormOfAddress.java @@ -9,17 +9,17 @@ public class FormOfAddress { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Long fid; @Column(nullable = false, length = 50) private String description; - public void setId(Long id) { - this.id = id; + public void setFid(Long fid) { + this.fid = fid; } - public Long getId() { - return id; + public Long getFid() { + return fid; } public void setDescription(String description) { diff --git a/src/main/java/com/maradona/backend/entities/Project.java b/src/main/java/com/maradona/backend/entities/Project.java index 2536b77..c7f92cb 100644 --- a/src/main/java/com/maradona/backend/entities/Project.java +++ b/src/main/java/com/maradona/backend/entities/Project.java @@ -10,7 +10,7 @@ public class Project { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Long pid; @Column(nullable = false, length = 255) private String name; @@ -23,6 +23,14 @@ public class Project { @Column(columnDefinition = "TEXT") private String description; + public void setPid(Long pid) { + this.pid = pid; + } + + public Long getPid() { + return pid; + } + public void setName(String name) { this.name = name; } diff --git a/src/main/java/com/maradona/backend/repositories/EmployeeSecondarySkillRepository.java b/src/main/java/com/maradona/backend/repositories/EmployeeSecondarySkillRepository.java index bc8e110..0f4fdbf 100644 --- a/src/main/java/com/maradona/backend/repositories/EmployeeSecondarySkillRepository.java +++ b/src/main/java/com/maradona/backend/repositories/EmployeeSecondarySkillRepository.java @@ -2,8 +2,6 @@ package com.maradona.backend.repositories; import org.springframework.data.repository.CrudRepository; import com.maradona.backend.entities.EmployeeSecondarySkill; -import java.util.List; public interface EmployeeSecondarySkillRepository extends CrudRepository { - List findByEmployeeId(Long employeeId); } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/services/EmployeeService.java b/src/main/java/com/maradona/backend/services/EmployeeService.java index 5d49343..6fcd32c 100644 --- a/src/main/java/com/maradona/backend/services/EmployeeService.java +++ b/src/main/java/com/maradona/backend/services/EmployeeService.java @@ -21,19 +21,19 @@ public class EmployeeService { return employeeRepository.save(employee); } - public Optional getEmployeeById(Long id) { + public Optional getEmployeeByEid(Long eid) { var employees = employeeRepository.findAll(); for (Employee employee : employees) { - if (employee.getEid().equals(id)) { + if (employee.getEid().equals(eid)) { return Optional.of(employee); } } return Optional.empty(); } - public void deleteEmployee(Long id) { + public void deleteEmployee(Long eid) { for (Employee employee : employeeRepository.findAll()) { - if (employee.getEid().equals(id)) { + if (employee.getEid().equals(eid)) { employeeRepository.delete(employee); return; } @@ -44,13 +44,13 @@ public class EmployeeService { return employeeRepository.findAll(); } - public void addSecondarySkillToEmployee(Long userId, SkillPrototype skillPrototype) { - var employee = getEmployeeById(userId).orElseThrow(() -> new RuntimeException("Employee not found")); - var secondarySkill = secondarySkillService.getSecondarySkillById(skillPrototype.getSecondarySkillId()) + public void addSecondarySkillToEmployee(Long eid, SkillPrototype skillPrototype) { + var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); + var secondarySkill = secondarySkillService.getSecondarySkillBySsid(skillPrototype.getSsid()) .orElseThrow(() -> new RuntimeException("Secondary Skill not found")); for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) { - if (skill.getSecondarySkill().getSsid().equals(skillPrototype.getSecondarySkillId())) { + if (skill.getSecondarySkill().getSsid().equals(skillPrototype.getSsid())) { return; } } @@ -63,13 +63,14 @@ public class EmployeeService { saveEmployee(employee); } - public void updateSecondarySkillLevel(Long userId, Long skillId, Integer level) { - var employee = getEmployeeById(userId).orElseThrow(() -> new RuntimeException("Employee not found")); - secondarySkillService.getSecondarySkillById(skillId) + public void updateSecondarySkillLevel(Long eid, Long ssid, Integer level) { + var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); + secondarySkillService.getSecondarySkillBySsid( + ssid) .orElseThrow(() -> new RuntimeException("Secondary Skill not found")); for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) { - if (skill.getSecondarySkill().getSsid().equals(skillId)) { + if (skill.getSecondarySkill().getSsid().equals(ssid)) { skill.setLevel(level); break; } @@ -78,18 +79,19 @@ public class EmployeeService { saveEmployee(employee); } - public void deleteSecondarySkillFromEmployee(Long userId, Long skillId) { - var employee = getEmployeeById(userId).orElseThrow(() -> new RuntimeException("Employee not found")); - secondarySkillService.getSecondarySkillById(skillId) + public void deleteSecondarySkillFromEmployee(Long eid, Long ssid) { + var employee = getEmployeeByEid(eid).orElseThrow(() -> new RuntimeException("Employee not found")); + secondarySkillService.getSecondarySkillBySsid( + ssid) .orElseThrow(() -> new RuntimeException("Secondary Skill not found")); - employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(skillId)); + employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(ssid)); saveEmployee(employee); } - public Iterable getEmployeesBySecondarySkill(Long skillId, Integer level) { + public Iterable getEmployeesBySecondarySkill(Long ssid, Integer level) { // Implement logic to fetch employees by secondary skill and level - // This is a placeholder implementation + // TODO: This is a placeholder implementation return employeeRepository.findAll(); } } \ No newline at end of file diff --git a/src/main/java/com/maradona/backend/services/FormOfAddressService.java b/src/main/java/com/maradona/backend/services/FormOfAddressService.java index 54075b6..7ac7f5a 100644 --- a/src/main/java/com/maradona/backend/services/FormOfAddressService.java +++ b/src/main/java/com/maradona/backend/services/FormOfAddressService.java @@ -17,25 +17,32 @@ public class FormOfAddressService { return formOfAddressRepository.save(formOfAddress); } - public Optional getFormOfAddressById(Long id) { - return formOfAddressRepository.findById(id); + public Optional getFormOfAddressByFid(Long fid) { + var formOfAddresses = formOfAddressRepository.findAll(); + for (FormOfAddress formOfAddress : formOfAddresses) { + if (formOfAddress.getFid().equals(fid)) { + return Optional.of(formOfAddress); + } + } + return Optional.empty(); } - public void deleteFormOfAddress(Long id) { - formOfAddressRepository.deleteById(id); + public void deleteFormOfAddress(Long fid) { + for (FormOfAddress formOfAddress : formOfAddressRepository.findAll()) { + if (formOfAddress.getFid().equals(fid)) { + formOfAddressRepository.delete(formOfAddress); + return; + } + } } public Iterable getAllFormOfAddresses() { return formOfAddressRepository.findAll(); } - public Long updateFormOfAddress(Long id, String description) { - var formOfAddress = formOfAddressRepository.findById(id).orElse(null); - if (formOfAddress != null) { - formOfAddress.setDescription(description); - formOfAddressRepository.save(formOfAddress); - return id; - } - return null; + public void updateFormOfAddress(Long fid, String description) { + var formOfAddress = getFormOfAddressByFid(fid).orElseThrow(() -> new RuntimeException("Form of Address not found")); + formOfAddress.setDescription(description); + saveFormOfAddress(formOfAddress); } } diff --git a/src/main/java/com/maradona/backend/services/PrimarySkillService.java b/src/main/java/com/maradona/backend/services/PrimarySkillService.java index 0ca21a2..d9b0ee0 100644 --- a/src/main/java/com/maradona/backend/services/PrimarySkillService.java +++ b/src/main/java/com/maradona/backend/services/PrimarySkillService.java @@ -17,12 +17,23 @@ public class PrimarySkillService { return primarySkillRepository.save(primarySkill); } - public Optional getPrimarySkillById(Long id) { - return primarySkillRepository.findById(id); + public Optional getPrimarySkillByPsid(Long psid) { + var primarySkills = primarySkillRepository.findAll(); + for (PrimarySkill primarySkill : primarySkills) { + if (primarySkill.getPsid().equals(psid)) { + return Optional.of(primarySkill); + } + } + return Optional.empty(); } public void deletePrimarySkill(Long id) { - primarySkillRepository.deleteById(id); + var primarySkills = primarySkillRepository.findAll(); + for (PrimarySkill primarySkill : primarySkills) { + if (primarySkill.getPsid().equals(id)) { + primarySkillRepository.delete(primarySkill); + } + } } public Iterable getAllPrimarySkills() { diff --git a/src/main/java/com/maradona/backend/services/ProjectService.java b/src/main/java/com/maradona/backend/services/ProjectService.java index c9bcb36..4e6280d 100644 --- a/src/main/java/com/maradona/backend/services/ProjectService.java +++ b/src/main/java/com/maradona/backend/services/ProjectService.java @@ -17,19 +17,30 @@ public class ProjectService { return projectRepository.save(project); } - public Optional getProjectById(Long id) { - return projectRepository.findById(id); + public Optional getProjectByPid(Long pid) { + var projects = projectRepository.findAll(); + for (Project project : projects) { + if (project.getPid().equals(pid)) { + return Optional.of(project); + } + } + return Optional.empty(); } - public void deleteProject(Long id) { - projectRepository.deleteById(id); + public void deleteProject(Long pid) { + var projects = projectRepository.findAll(); + for (Project project : projects) { + if (project.getPid().equals(pid)) { + projectRepository.delete(project); + } + } } public Iterable getAllProjects() { return projectRepository.findAll(); } - public Iterable getProjectsByUserId(Long userId) { + public Iterable getProjectsByEid(Long eid) { // TODO: Actually filter by user return projectRepository.findAll(); } diff --git a/src/main/java/com/maradona/backend/services/SecondarySkillService.java b/src/main/java/com/maradona/backend/services/SecondarySkillService.java index e08d48b..07df7eb 100644 --- a/src/main/java/com/maradona/backend/services/SecondarySkillService.java +++ b/src/main/java/com/maradona/backend/services/SecondarySkillService.java @@ -28,23 +28,29 @@ public class SecondarySkillService { return secondarySkillRepository.save(secondarySkill); } - public Optional getSecondarySkillById(Long id) { - return secondarySkillRepository.findById(id); + public Optional getSecondarySkillBySsid(Long ssid) { + var secondarySkills = secondarySkillRepository.findAll(); + for (SecondarySkill secondarySkill : secondarySkills) { + if (secondarySkill.getSsid().equals(ssid)) { + return Optional.of(secondarySkill); + } + } + return Optional.empty(); } - public void deleteSecondarySkill(Long id) { - secondarySkillRepository.deleteById(id); + public void deleteSecondarySkill(Long ssid) { + secondarySkillRepository.deleteById(ssid); } public Iterable getAllSecondarySkills() { return secondarySkillRepository.findAll(); } - public Iterable getSecondarySkillsByPrimarySkillId(Long primarySkillId) { + public Iterable getSecondarySkillsByPrimarySkillId(Long psid) { var skills = secondarySkillRepository.findAll(); var result = new java.util.ArrayList(); for (SecondarySkill skill : skills) { - if (skill.getPrimarySkill().getPsid().equals(primarySkillId)) { + if (skill.getPrimarySkill().getPsid().equals(psid)) { result.add(skill); System.out.println(skill.getDescription()); } diff --git a/src/main/java/com/maradona/backend/services/SkillService.java b/src/main/java/com/maradona/backend/services/SkillService.java index b8d7162..b3a6a4b 100644 --- a/src/main/java/com/maradona/backend/services/SkillService.java +++ b/src/main/java/com/maradona/backend/services/SkillService.java @@ -4,9 +4,6 @@ import com.maradona.backend.dto.SkillList; 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; import org.springframework.stereotype.Service; import org.springframework.data.util.Pair; @@ -19,24 +16,24 @@ import java.util.Map; public class SkillService { @Autowired - private PrimarySkillRepository primarySkillRepository; + private PrimarySkillService primarySkillService; @Autowired - private SecondarySkillRepository secondarySkillRepository; + private SecondarySkillService secondarySkillService; @Autowired - private EmployeeRepository employeeRepository; + private EmployeeService employeeService; public Iterable getAllSkills() { - Iterable primarySkills = primarySkillRepository.findAll(); - Iterable secondarySkills = secondarySkillRepository.findAll(); + Iterable primarySkills = primarySkillService.getAllPrimarySkills(); + Iterable secondarySkills = secondarySkillService.getAllSecondarySkills(); List skills = new ArrayList<>(); for (PrimarySkill primarySkill : primarySkills) { List> secondarySkillList = new ArrayList<>(); for (SecondarySkill secondarySkill : secondarySkills) { if (secondarySkill.getPrimarySkill().getPsid().equals(primarySkill.getPsid())) { - secondarySkillList.add(Pair.of(secondarySkill, 3)); // Placeholder level + secondarySkillList.add(Pair.of(secondarySkill, 3)); } } skills.add(new SkillList(primarySkill, secondarySkillList)); @@ -44,11 +41,11 @@ public class SkillService { return skills; } - public Iterable getUserSkills(Long userId) { - if (userId == null) { + public Iterable getUserSkills(Long eid) { + if (eid == null) { return new ArrayList<>(); } - Employee employee = employeeRepository.findById(userId).orElse(null); + Employee employee = employeeService.getEmployeeByEid(eid).orElse(null); if (employee == null) { return new ArrayList<>(); } @@ -56,7 +53,7 @@ public class SkillService { Map secondarySkillLevels = employee.getSecondarySkillLevels(); List skills = new ArrayList<>(); - for (PrimarySkill primarySkill : primarySkillRepository.findAll()) { + for (PrimarySkill primarySkill : primarySkillService.getAllPrimarySkills()) { List> secondarySkillList = new ArrayList<>(); for (Map.Entry entry : secondarySkillLevels.entrySet()) { if (entry.getKey().getPrimarySkill().getPsid().equals(primarySkill.getPsid())) { From dcb3d0ec916b3811812849813efadf732525137f Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Mon, 11 Nov 2024 11:47:31 +0100 Subject: [PATCH 8/8] api: Update PrimarySkillService --- .../backend/services/PrimarySkillService.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/maradona/backend/services/PrimarySkillService.java b/src/main/java/com/maradona/backend/services/PrimarySkillService.java index d9b0ee0..169ad7a 100644 --- a/src/main/java/com/maradona/backend/services/PrimarySkillService.java +++ b/src/main/java/com/maradona/backend/services/PrimarySkillService.java @@ -3,6 +3,7 @@ package com.maradona.backend.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.maradona.backend.entities.PrimarySkill; +import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.repositories.PrimarySkillRepository; import java.util.Optional; @@ -13,6 +14,9 @@ public class PrimarySkillService { @Autowired private PrimarySkillRepository primarySkillRepository; + @Autowired + private SecondarySkillService secondarySkillRepository; + public PrimarySkill savePrimarySkill(PrimarySkill primarySkill) { return primarySkillRepository.save(primarySkill); } @@ -27,10 +31,16 @@ public class PrimarySkillService { return Optional.empty(); } - public void deletePrimarySkill(Long id) { + public void deletePrimarySkill(Long psid) { var primarySkills = primarySkillRepository.findAll(); + var secondarySkills = secondarySkillRepository.getAllSecondarySkills(); + for (SecondarySkill secondarySkill : secondarySkills) { + if (secondarySkill.getPrimarySkill().getPsid().equals(psid)) { + secondarySkillRepository.deleteSecondarySkill(secondarySkill.getSsid()); + } + } for (PrimarySkill primarySkill : primarySkills) { - if (primarySkill.getPsid().equals(id)) { + if (primarySkill.getPsid().equals(psid)) { primarySkillRepository.delete(primarySkill); } }