refacor: Update employee system
parent
386765f5a8
commit
136e8aab3f
Binary file not shown.
|
@ -0,0 +1,118 @@
|
|||
package com.maradona.backend.controllers.api;
|
||||
|
||||
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.stereotype.Controller;
|
||||
|
||||
import com.maradona.backend.dto.SkillPrototype;
|
||||
import com.maradona.backend.entities.Employee;
|
||||
import com.maradona.backend.services.EmployeeService;
|
||||
import com.maradona.backend.services.SecondarySkillService;
|
||||
|
||||
/**
|
||||
* Controller for handling employee related requests.
|
||||
*
|
||||
* Endpoints:
|
||||
* - GET /api/employee
|
||||
* - GET /api/employee/all
|
||||
* - GET /api/employee/from-skill
|
||||
* - GET /api/employee/skill/all
|
||||
* - POST /api/employee/skill/protoype
|
||||
* - PUT /api/employee/skill/level
|
||||
* - DELETE /api/employee/skill
|
||||
*
|
||||
* @see EmployeeService
|
||||
* @see SecondarySkillService
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/api/employee")
|
||||
public class EmployeeController {
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
|
||||
/**
|
||||
* Hardcoded user ID for testing purposes.
|
||||
*/
|
||||
Long user = Long.valueOf(1);
|
||||
|
||||
/**
|
||||
* Returns a specific employee from the database.
|
||||
*
|
||||
* @param id The ID of the requested employee.
|
||||
* @param model The model with the requested data.
|
||||
* @see Employee
|
||||
*/
|
||||
@GetMapping({ "/", "" })
|
||||
public void get(@RequestParam Long id, Model model) {
|
||||
var employee = employeeService.getEmployeeById(id).orElse(null);
|
||||
model.addAttribute("employee", employee);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all employees from the database.
|
||||
*
|
||||
* @param model The model with the requested data.
|
||||
* @see Employee
|
||||
*/
|
||||
@GetMapping("/all")
|
||||
public void getAll(Model model) {
|
||||
var employees = employeeService.getAllEmployees();
|
||||
model.addAttribute("employees", employees);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return list of employees
|
||||
* @see Employee
|
||||
*/
|
||||
@GetMapping("/from-skill")
|
||||
public void getFromSkill(@RequestParam Long skillId, @RequestParam Integer level, Model model) {
|
||||
var employees = employeeService.getEmployeesBySecondarySkill(skillId, level);
|
||||
model.addAttribute("employees", employees);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a secondary skill based on a skillPrototype object to the user profile.
|
||||
*
|
||||
* @param skillPrototype The skillPrototype object containing the secondary
|
||||
* skill ID and level
|
||||
* @see SkillPrototype
|
||||
*/
|
||||
@PostMapping("/skill/protoype")
|
||||
public void postSkillProtoype(@ModelAttribute SkillPrototype skillPrototype) {
|
||||
employeeService.addSecondarySkillToEmployee(user, skillPrototype);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @see SecondarySkill
|
||||
*/
|
||||
@PutMapping("/skill/level")
|
||||
public void putSkillLevel(@RequestParam Long skillId, @RequestParam Integer level) {
|
||||
employeeService.updateSecondarySkillLevel(user, skillId, level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes secondary skill from user profile
|
||||
*
|
||||
* @param id ID of the secondary skill
|
||||
* @see SecondarySkill
|
||||
*/
|
||||
@DeleteMapping("/skill")
|
||||
public void delete(@RequestParam Long id) {
|
||||
employeeService.deleteSecondarySkillFromEmployee(user, id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.maradona.backend.controllers.api;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.maradona.backend.entities.PrimarySkill;
|
||||
import com.maradona.backend.entities.SecondarySkill;
|
||||
import com.maradona.backend.services.PrimarySkillService;
|
||||
import com.maradona.backend.services.SecondarySkillService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/primary-skill")
|
||||
public class PrimarySkillController {
|
||||
|
||||
@Autowired
|
||||
private PrimarySkillService primarySkillService;
|
||||
|
||||
@Autowired
|
||||
private SecondarySkillService secondarySkillService;
|
||||
|
||||
@DeleteMapping({ "/", "" })
|
||||
public void delete(@RequestParam Long id) {
|
||||
// Find and delete all secondary skills associated with the primary skill
|
||||
var primarySkill = primarySkillService.getPrimarySkillById(id);
|
||||
if (primarySkill.isPresent()) {
|
||||
var secondarySkills = secondarySkillService.getAllSecondarySkills();
|
||||
for (SecondarySkill secondarySkill : secondarySkills) {
|
||||
if (secondarySkill.getPrimarySkill().getPsid().equals(id)) {
|
||||
secondarySkillService.deleteSecondarySkill(secondarySkill.getSsid());
|
||||
}
|
||||
}
|
||||
// Delete the primary skill
|
||||
primarySkillService.deletePrimarySkill(id);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,7 +63,7 @@ public class SecondarySkillController {
|
|||
* @param model The model with the requested data.
|
||||
* @see SecondarySkill
|
||||
*/
|
||||
@GetMapping({ "/all", "" })
|
||||
@GetMapping("/all")
|
||||
public void getAll(Model model) {
|
||||
var skills = secondarySkillService.getAllSecondarySkills();
|
||||
model.addAttribute("secondarySkills", skills);
|
||||
|
@ -108,13 +108,12 @@ public class SecondarySkillController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Removes secondary skill from user profile
|
||||
* Deletes a specific secondary skill from the database.
|
||||
*
|
||||
* @param id ID of secondary skill to be removed
|
||||
* @see SecondarySkill
|
||||
* @param id The ID of the secondary skill to be deleted
|
||||
*/
|
||||
@DeleteMapping({ "/", "" })
|
||||
public void delete(@RequestParam Long id) {
|
||||
secondarySkillService.deleteSecondarySkillFromEmployee(user, id);
|
||||
secondarySkillService.deleteSecondarySkill(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,20 +3,15 @@ package com.maradona.backend.controllers.page;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ui.Model;
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
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;
|
||||
import com.maradona.backend.services.SecondarySkillService;
|
||||
import com.maradona.backend.dto.SkillForm;
|
||||
import com.maradona.backend.dto.SkillPrototype;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -32,9 +27,6 @@ public class SkillsPage {
|
|||
@Autowired
|
||||
private PrimarySkillService primarySkillService;
|
||||
|
||||
@Autowired
|
||||
private SecondarySkillService secondarySkillService;
|
||||
|
||||
// Hardcoded placeholder user ID for now
|
||||
Long user = Long.valueOf(1);
|
||||
|
||||
|
@ -89,97 +81,15 @@ public class SkillsPage {
|
|||
* @return The add skill page template
|
||||
* @see PrimarySkill
|
||||
* @see SecondarySkill
|
||||
* @see SkillForm
|
||||
* @see SkillPrototype
|
||||
*/
|
||||
@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());
|
||||
model.addAttribute("skillForm", new SkillPrototype());
|
||||
return "skills/skills-add";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
var employee = employeeService.getEmployeeById(user).orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
var secondarySkill = secondarySkillService.getSecondarySkillById(skillForm.getSecondarySkillId())
|
||||
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
|
||||
|
||||
// Test id employee already has the skill
|
||||
for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) {
|
||||
if (skill.getSecondarySkill().getSsid().equals(skillForm.getSecondarySkillId())) {
|
||||
return "redirect:/skills";
|
||||
}
|
||||
}
|
||||
|
||||
// Add the skill to the user profile
|
||||
EmployeeSecondarySkill employeeSecondarySkill = new EmployeeSecondarySkill();
|
||||
employeeSecondarySkill.setEmployee(employee);
|
||||
employeeSecondarySkill.setSecondarySkill(secondarySkill);
|
||||
employeeSecondarySkill.setLevel(skillForm.getLevel());
|
||||
employee.getSecondarySkills().add(employeeSecondarySkill);
|
||||
employeeService.saveEmployee(employee);
|
||||
|
||||
// Redirect to the skills overview page
|
||||
return "redirect:/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
|
||||
var primarySkill = primarySkillService.getPrimarySkillById(id);
|
||||
if (primarySkill.isPresent()) {
|
||||
var secondarySkills = secondarySkillService.getAllSecondarySkills();
|
||||
for (SecondarySkill secondarySkill : secondarySkills) {
|
||||
if (secondarySkill.getPrimarySkill().getPsid().equals(id)) {
|
||||
secondarySkillService.deleteSecondarySkill(secondarySkill.getSsid());
|
||||
}
|
||||
}
|
||||
// Delete the primary skill
|
||||
primarySkillService.deletePrimarySkill(id);
|
||||
}
|
||||
return "redirect:/skills";
|
||||
}
|
||||
|
||||
// Update skill level
|
||||
@PostMapping("/update-level")
|
||||
public String updateLevel(@RequestParam Long skillId, @RequestParam Integer level) {
|
||||
var employee = employeeService.getEmployeeById(user).orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
secondarySkillService.getSecondarySkillById(skillId)
|
||||
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
|
||||
|
||||
for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) {
|
||||
if (skill.getSecondarySkill().getSsid().equals(skillId)) {
|
||||
skill.setLevel(level);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
employeeService.saveEmployee(employee);
|
||||
|
||||
return "redirect:/skills";
|
||||
}
|
||||
|
||||
}
|
|
@ -5,11 +5,11 @@ import com.maradona.backend.entities.SecondarySkill;
|
|||
import org.springframework.data.util.Pair;
|
||||
import java.util.List;
|
||||
|
||||
public class SkillDto {
|
||||
public class SkillList {
|
||||
private PrimarySkill primarySkill;
|
||||
private List<Pair<SecondarySkill, Integer>> secondarySkills;
|
||||
|
||||
public SkillDto(PrimarySkill primarySkill, List<Pair<SecondarySkill, Integer>> secondarySkills) {
|
||||
public SkillList(PrimarySkill primarySkill, List<Pair<SecondarySkill, Integer>> secondarySkills) {
|
||||
this.primarySkill = primarySkill;
|
||||
this.secondarySkills = secondarySkills;
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.maradona.backend.dto;
|
||||
|
||||
public class SkillForm {
|
||||
public class SkillPrototype {
|
||||
private Long primarySkillId;
|
||||
private Long secondarySkillId;
|
||||
private Integer level;
|
|
@ -3,16 +3,20 @@ package com.maradona.backend.services;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.maradona.backend.entities.Employee;
|
||||
import com.maradona.backend.entities.EmployeeSecondarySkill;
|
||||
import com.maradona.backend.repositories.EmployeeRepository;
|
||||
import com.maradona.backend.dto.SkillPrototype;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Autowired
|
||||
private SecondarySkillService secondarySkillService;
|
||||
|
||||
public Employee saveEmployee(Employee employee) {
|
||||
return employeeRepository.save(employee);
|
||||
}
|
||||
|
@ -28,4 +32,53 @@ public class EmployeeService {
|
|||
public Iterable<Employee> getAllEmployees() {
|
||||
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())
|
||||
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
|
||||
|
||||
for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) {
|
||||
if (skill.getSecondarySkill().getSsid().equals(skillPrototype.getSecondarySkillId())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
EmployeeSecondarySkill employeeSecondarySkill = new EmployeeSecondarySkill();
|
||||
employeeSecondarySkill.setEmployee(employee);
|
||||
employeeSecondarySkill.setSecondarySkill(secondarySkill);
|
||||
employeeSecondarySkill.setLevel(skillPrototype.getLevel());
|
||||
employee.getSecondarySkills().add(employeeSecondarySkill);
|
||||
saveEmployee(employee);
|
||||
}
|
||||
|
||||
public void updateSecondarySkillLevel(Long userId, Long skillId, Integer level) {
|
||||
var employee = getEmployeeById(userId).orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
secondarySkillService.getSecondarySkillById(skillId)
|
||||
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
|
||||
|
||||
for (EmployeeSecondarySkill skill : employee.getSecondarySkills()) {
|
||||
if (skill.getSecondarySkill().getSsid().equals(skillId)) {
|
||||
skill.setLevel(level);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
saveEmployee(employee);
|
||||
}
|
||||
|
||||
public void deleteSecondarySkillFromEmployee(Long userId, Long skillId) {
|
||||
var employee = getEmployeeById(userId).orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
secondarySkillService.getSecondarySkillById(skillId)
|
||||
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
|
||||
|
||||
employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(skillId));
|
||||
saveEmployee(employee);
|
||||
}
|
||||
|
||||
public Iterable<Employee> getEmployeesBySecondarySkill(Long skillId, Integer level) {
|
||||
// Implement logic to fetch employees by secondary skill and level
|
||||
// This is a placeholder implementation
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.maradona.backend.services;
|
||||
|
||||
import com.maradona.backend.dto.SkillDto;
|
||||
import com.maradona.backend.dto.SkillList;
|
||||
import com.maradona.backend.entities.Employee;
|
||||
import com.maradona.backend.entities.PrimarySkill;
|
||||
import com.maradona.backend.entities.SecondarySkill;
|
||||
|
@ -27,10 +27,10 @@ public class SkillService {
|
|||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
public Iterable<SkillDto> getAllSkills() {
|
||||
public Iterable<SkillList> getAllSkills() {
|
||||
Iterable<PrimarySkill> primarySkills = primarySkillRepository.findAll();
|
||||
Iterable<SecondarySkill> secondarySkills = secondarySkillRepository.findAll();
|
||||
List<SkillDto> skills = new ArrayList<>();
|
||||
List<SkillList> skills = new ArrayList<>();
|
||||
|
||||
for (PrimarySkill primarySkill : primarySkills) {
|
||||
List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
|
||||
|
@ -39,12 +39,12 @@ public class SkillService {
|
|||
secondarySkillList.add(Pair.of(secondarySkill, 3)); // Placeholder level
|
||||
}
|
||||
}
|
||||
skills.add(new SkillDto(primarySkill, secondarySkillList));
|
||||
skills.add(new SkillList(primarySkill, secondarySkillList));
|
||||
}
|
||||
return skills;
|
||||
}
|
||||
|
||||
public Iterable<SkillDto> getUserSkills(Long userId) {
|
||||
public Iterable<SkillList> getUserSkills(Long userId) {
|
||||
if (userId == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ public class SkillService {
|
|||
}
|
||||
|
||||
Map<SecondarySkill, Integer> secondarySkillLevels = employee.getSecondarySkillLevels();
|
||||
List<SkillDto> skills = new ArrayList<>();
|
||||
List<SkillList> skills = new ArrayList<>();
|
||||
|
||||
for (PrimarySkill primarySkill : primarySkillRepository.findAll()) {
|
||||
List<Pair<SecondarySkill, Integer>> secondarySkillList = new ArrayList<>();
|
||||
|
@ -64,7 +64,7 @@ public class SkillService {
|
|||
}
|
||||
}
|
||||
if (!secondarySkillList.isEmpty()) {
|
||||
skills.add(new SkillDto(primarySkill, secondarySkillList));
|
||||
skills.add(new SkillList(primarySkill, secondarySkillList));
|
||||
}
|
||||
secondarySkillList.sort((a, b) -> a.getFirst().getDescription().compareTo(b.getFirst().getDescription()));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue