refactor: Split and document SkillsController

pull/1/head
Lunix-420 2024-11-07 11:52:55 +01:00
parent eb52b4a4b2
commit a2c474239d
2 changed files with 193 additions and 51 deletions

View File

@ -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<SecondarySkill> 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);
}
}

View File

@ -12,6 +12,7 @@ import org.springframework.stereotype.Controller;
import com.maradona.backend.entities.SecondarySkill; import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.entities.EmployeeSecondarySkill; import com.maradona.backend.entities.EmployeeSecondarySkill;
import com.maradona.backend.entities.PrimarySkill;
import com.maradona.backend.services.EmployeeService; import com.maradona.backend.services.EmployeeService;
import com.maradona.backend.services.SkillService; import com.maradona.backend.services.SkillService;
import com.maradona.backend.services.PrimarySkillService; import com.maradona.backend.services.PrimarySkillService;
@ -38,38 +39,79 @@ public class SkillsController {
// Hardcoded placeholder user ID for now // Hardcoded placeholder user ID for now
Long user = Long.valueOf(1); 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({ "/", "" }) @GetMapping({ "/", "" })
public String profile(Model model) { public String profile(Model model) {
// Gather the employee and skills data model.addAttribute("employee", employeeService.getEmployeeById(user).orElse(null));
var employeeData = employeeService.getEmployeeById(user); model.addAttribute("skills", skillService.getUserSkills(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
return "skills/skills"; 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") @GetMapping("/create")
public String createSkill(Model model) { public String createSkill(Model model) {
model.addAttribute("secondarySkill", new SecondarySkill()); model.addAttribute("secondarySkill", new SecondarySkill());
return "skills/skills-create"; 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") @GetMapping("/add")
public String addSkill(Model model) { public String addSkill(Model model) {
// TODO: Make sure it returns the correct initail data for secondary skills
model.addAttribute("primarySkills", primarySkillService.getAllPrimarySkills()); model.addAttribute("primarySkills", primarySkillService.getAllPrimarySkills());
model.addAttribute("secondarySkills", List.of()); model.addAttribute("secondarySkills", List.of());
model.addAttribute("skillForm", new SkillForm()); model.addAttribute("skillForm", new SkillForm());
return "skills/skills-add"; 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") @PostMapping("/add")
public String addSkill(@ModelAttribute SkillForm skillForm) { public String addSkill(@ModelAttribute SkillForm skillForm) {
// Gather the user and skill // Gather the user and skill
@ -96,31 +138,15 @@ public class SkillsController {
return "redirect:/skills"; return "redirect:/skills";
} }
// Returns a list of secondary skills for a given primary skill /**
@GetMapping("/secondary-skills") * Deletes a primary skill and all associated secondary skills
@ResponseBody *
public Iterable<SecondarySkill> getSecondarySkills(@RequestParam Long primarySkillId) { * @return Redirect to skills page
var list = secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId); *
return list; * @param id ID of primary skill
} * @see PrimarySkill
* @see SecondarySkill
// 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
@PostMapping("/delete") @PostMapping("/delete")
public String delete(@RequestParam Long id) { public String delete(@RequestParam Long id) {
// Find and delete all secondary skills associated with the primary skill // Find and delete all secondary skills associated with the primary skill
@ -138,19 +164,6 @@ public class SkillsController {
return "redirect:/skills"; 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 // Update skill level
@PostMapping("/update-level") @PostMapping("/update-level")
public String updateLevel(@RequestParam Long skillId, @RequestParam Integer level) { public String updateLevel(@RequestParam Long skillId, @RequestParam Integer level) {