From a2c474239d9b3de8fb9337c57e1bdb9583e58cea Mon Sep 17 00:00:00 2001 From: Lunix-420 Date: Thu, 7 Nov 2024 11:52:55 +0100 Subject: [PATCH] refactor: Split and document SkillsController --- .../SecondarySkillsController.java | 129 ++++++++++++++++++ .../backend/controllers/SkillsController.java | 115 +++++++++------- 2 files changed, 193 insertions(+), 51 deletions(-) create mode 100644 src/main/java/com/maradona/backend/controllers/SecondarySkillsController.java diff --git a/src/main/java/com/maradona/backend/controllers/SecondarySkillsController.java b/src/main/java/com/maradona/backend/controllers/SecondarySkillsController.java new file mode 100644 index 0000000..ced2330 --- /dev/null +++ b/src/main/java/com/maradona/backend/controllers/SecondarySkillsController.java @@ -0,0 +1,129 @@ +package com.maradona.backend.controllers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.stereotype.Controller; + +import com.maradona.backend.entities.SecondarySkill; +import com.maradona.backend.entities.PrimarySkill; +import com.maradona.backend.services.EmployeeService; +import com.maradona.backend.services.PrimarySkillService; +import com.maradona.backend.services.SecondarySkillService; + +/** + * Controller for handling secondary skill data. + * + * @see SecondarySkill + */ +@Controller +@RequestMapping("/api/secondary-skills") +public class SecondarySkillsController { + @Autowired + private EmployeeService employeeService; + + @Autowired + private PrimarySkillService primarySkillService; + + @Autowired + private SecondarySkillService secondarySkillService; + + // Hardcoded placeholder user ID for now + Long user = Long.valueOf(1); + + /** + * Returns a specific secondary skill from the database. + * + * @param id The ID of the requested secondary skill. + * @param model The model with the requested data. + * @see SecondarySkill + */ + @GetMapping({ "/", "" }) + public void get(@RequestParam Long id, Model model) { + var skill = secondarySkillService.getSecondarySkillById(id).orElse(null); + model.addAttribute("secondarySkill", skill); + } + + /** + * Returns all secondary skills from the database. + * + * @param model The model with the requested data. + * @see SecondarySkill + */ + @GetMapping({ "/all", "" }) + public void getAll(Model model) { + var skills = secondarySkillService.getAllSecondarySkills(); + model.addAttribute("secondarySkills", skills); + } + + /** + * Returns a list of secondary skills for a given primary skill + * + * @param primarySkillId ID of the primary skill + * @return list of secondary skills + * @see SecondarySkill + * @see PrimarySkill + */ + @GetMapping("/from-primary-skill") + @ResponseBody + public Iterable getSecondarySkills(@RequestParam Long primarySkillId) { + var list = secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId); + return list; + } + + /** + * Saves a new secondary skill to the database. + * If the associated primary skill does not exist, it will be created. + * + * @param secondarySkill The secondary skill to be saved + * @see SecondarySkill + * @see PrimarySkill + */ + @PostMapping({ "/", "" }) + public void post(@ModelAttribute SecondarySkill secondarySkill) { + var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription(); + var existingPrimarySkill = primarySkillService.findByDescription(primarySkillDescription); + + if (existingPrimarySkill.isPresent()) { + secondarySkill.setPrimarySkill(existingPrimarySkill.get()); + } else { + primarySkillService.savePrimarySkill(secondarySkill.getPrimarySkill()); + } + + secondarySkillService.saveSecondarySkill(secondarySkill); + } + + /** + * Modifies an existing secondary skill in the database. + * + * @param secondarySkill The secondary skill to be modified + * @see SecondarySkill + */ + @PutMapping({ "/", "" }) + public void put(@ModelAttribute SecondarySkill secondarySkill) { + secondarySkillService.saveSecondarySkill(secondarySkill); + } + + /** + * Removes secondary skill from user profile + * + * @param id ID of secondary skill to be removed + * @see SecondarySkill + */ + @DeleteMapping({ "/", "" }) + public void delete(@RequestParam Long id) { + var employee = employeeService.getEmployeeById(user).orElseThrow(() -> new RuntimeException("Employee not found")); + secondarySkillService.getSecondarySkillById(id) + .orElseThrow(() -> new RuntimeException("Secondary Skill not found")); + + employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(id)); + employeeService.saveEmployee(employee); + } +} diff --git a/src/main/java/com/maradona/backend/controllers/SkillsController.java b/src/main/java/com/maradona/backend/controllers/SkillsController.java index 22690e9..e95530b 100644 --- a/src/main/java/com/maradona/backend/controllers/SkillsController.java +++ b/src/main/java/com/maradona/backend/controllers/SkillsController.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Controller; import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.EmployeeSecondarySkill; +import com.maradona.backend.entities.PrimarySkill; import com.maradona.backend.services.EmployeeService; import com.maradona.backend.services.SkillService; import com.maradona.backend.services.PrimarySkillService; @@ -38,38 +39,79 @@ public class SkillsController { // Hardcoded placeholder user ID for now Long user = Long.valueOf(1); - // Returns the skills overview page + /** + * Returns the skills overview page. + * Collects the employee and skills data and adds it to the model for the + * frontend. + * + * Attributes that can be used by the frontend are a Employee object + * "employee" and a SkillsDto object "skills". + * + * @param model The model with the data to be displayed. + * @return The skills overview page template + * @see Employee + * @see SkillsDto + */ @GetMapping({ "/", "" }) public String profile(Model model) { - // Gather the employee and skills data - var employeeData = employeeService.getEmployeeById(user); - var skillData = skillService.getUserSkills(user); - - // Add the data to the model for the frontend - model.addAttribute("employee", employeeData.orElse(null)); - model.addAttribute("skillData", skillData); - - // Return the skills overview page + model.addAttribute("employee", employeeService.getEmployeeById(user).orElse(null)); + model.addAttribute("skills", skillService.getUserSkills(user)); return "skills/skills"; } - // Returns the create-skill page + /** + * Returns the create skill page. + * + * Attributes that can be used by the frontend are a SecondarySkill object + * "secondarySkill". + * + * @param model The model with the data to be displayed + * @return The create skill page template + * @see SecondarySkill + */ @GetMapping("/create") public String createSkill(Model model) { model.addAttribute("secondarySkill", new SecondarySkill()); return "skills/skills-create"; } - // Add a new skill to the user profile + /** + * Returns the page used to add a skill to the user profile. + * + * Attributes that can be used by the frontend are a PrimarySkill object + * "primarySkill", a SecondarySkill object "secondarySkill" and a + * SkillForm object "skillForm". + * + * The skillForm object contains the data needed to add a skill to the user + * profile. + * To add it to the user profile, a POST request to /skills/add is needed. + * + * @param model The model with the data to be displayed + * @return The add skill page template + * @see PrimarySkill + * @see SecondarySkill + * @see SkillForm + */ @GetMapping("/add") public String addSkill(Model model) { + // TODO: Make sure it returns the correct initail data for secondary skills model.addAttribute("primarySkills", primarySkillService.getAllPrimarySkills()); model.addAttribute("secondarySkills", List.of()); model.addAttribute("skillForm", new SkillForm()); return "skills/skills-add"; } - // Add a new skill to the user profile + /** + * Adds a skill to the user profile. Redirects to the skills overview page. + * + * The skillForm object that this function receives can be obtained using a GET + * request to /skills/add. + * + * @param skillForm The form data containing the skill to be added to the user + * profile + * @return Redirect to the skills overview page + * @see SkillForm + */ @PostMapping("/add") public String addSkill(@ModelAttribute SkillForm skillForm) { // Gather the user and skill @@ -96,31 +138,15 @@ public class SkillsController { return "redirect:/skills"; } - // Returns a list of secondary skills for a given primary skill - @GetMapping("/secondary-skills") - @ResponseBody - public Iterable getSecondarySkills(@RequestParam Long primarySkillId) { - var list = secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId); - return list; - } - - // Saves a new skill to the database - @PostMapping("/save") - public String save(@ModelAttribute SecondarySkill secondarySkill) { - var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription(); - var existingPrimarySkill = primarySkillService.findByDescription(primarySkillDescription); - - if (existingPrimarySkill.isPresent()) { - secondarySkill.setPrimarySkill(existingPrimarySkill.get()); - } else { - primarySkillService.savePrimarySkill(secondarySkill.getPrimarySkill()); - } - - secondarySkillService.saveSecondarySkill(secondarySkill); - return "redirect:/skills"; - } - - // Deletes a primary skill and all associated secondary skills + /** + * Deletes a primary skill and all associated secondary skills + * + * @return Redirect to skills page + * + * @param id ID of primary skill + * @see PrimarySkill + * @see SecondarySkill + */ @PostMapping("/delete") public String delete(@RequestParam Long id) { // Find and delete all secondary skills associated with the primary skill @@ -138,19 +164,6 @@ public class SkillsController { return "redirect:/skills"; } - // Removes secondary skill from user profile - @PostMapping("/remove") - public String remove(@RequestParam Long id) { - var employee = employeeService.getEmployeeById(user).orElseThrow(() -> new RuntimeException("Employee not found")); - secondarySkillService.getSecondarySkillById(id) - .orElseThrow(() -> new RuntimeException("Secondary Skill not found")); - - employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(id)); - employeeService.saveEmployee(employee); - - return "redirect:/skills"; - } - // Update skill level @PostMapping("/update-level") public String updateLevel(@RequestParam Long skillId, @RequestParam Integer level) {