Merge pull request 'chore: Update branch' (#56) from 3002833/Backend:main into restfull

Reviewed-on: Maradona/Backend#56
pull/1/head
David Hess 2024-11-07 16:14:49 +01:00
commit fba9ea5313
11 changed files with 309 additions and 126 deletions

Binary file not shown.

View File

@ -36,6 +36,13 @@ public class DefaultValueLoader implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Clear all tables
// formOfAddressRepository.deleteAll();
// employeeRepository.deleteAll();
// secondarySkillRepository.deleteAll();
// primarySkillRepository.deleteAll();
// Create form of addresses
FormOfAddress formOfAddress1 = new FormOfAddress();
formOfAddress1.setDescription("Herr");

View File

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

View File

@ -0,0 +1,102 @@
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.PutMapping;
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;
/**
* Controller for handling primary skill data.
*
* List of endpoints:
* - GET /api/primary-skill
* - GET /api/primary-skill/all
* - POST /api/primary-skill
* - PUT /api/primary-skill
* - DELETE /api/primary-skill
*
* @see PrimarySkill
*/
@RestController
@RequestMapping("/api/primary-skill")
public class PrimarySkillController {
@Autowired
private PrimarySkillService primarySkillService;
@Autowired
private SecondarySkillService secondarySkillService;
/**
* Returns a specific primary skill from the database.
*
* @param id The ID of the requested primary skill.
* @return The primary skill with the requested ID.
* @see PrimarySkill
*/
@GetMapping({ "/", "" })
public PrimarySkill get(@RequestParam Long id) {
return primarySkillService.getPrimarySkillById(id).orElse(null);
}
/**
* Returns all primary skills from the database.
*
* @return A list of all primary skills.
* @see PrimarySkill
*/
@GetMapping("/all")
public Iterable<PrimarySkill> getAll() {
return primarySkillService.getAllPrimarySkills();
}
/**
* Adds a new primary skill to the database.
*
* @param primarySkill The primary skill to be added.
* @return The added primary skill.
* @see PrimarySkill
*/
@PostMapping({ "/", "" })
public void post(@RequestBody PrimarySkill primarySkill) {
primarySkillService.savePrimarySkill(primarySkill);
}
/**
* Updates an existing primary skill in the database.
*
* @param primarySkill The primary skill to be updated.
* @return The updated primary skill.
* @see PrimarySkill
*/
@PutMapping({ "/", "" })
public void put(@RequestBody PrimarySkill primarySkill) {
primarySkillService.savePrimarySkill(primarySkill);
}
@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);
}
}
}

View File

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

View File

@ -3,23 +3,27 @@ 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;
/**
* Controller for routing to the skills related pages.
*
* List of endpoints:
* - GET /skills
* - GET /skills/create
* - GET /skills/add
*
*/
@Controller
@RequestMapping("/skills")
public class SkillsPage {
@ -32,9 +36,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 +90,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";
}
}

View File

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

View File

@ -1,6 +1,6 @@
package com.maradona.backend.dto;
public class SkillForm {
public class SkillPrototype {
private Long primarySkillId;
private Long secondarySkillId;
private Integer level;

View File

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

View File

@ -9,18 +9,13 @@ import java.util.Optional;
@Service
public class SecondarySkillService {
@Autowired
private SecondarySkillRepository secondarySkillRepository;
@Autowired
private PrimarySkillService primarySkillService;
@Autowired
private EmployeeService employeeService;
public SecondarySkill saveSecondarySkill(SecondarySkill secondarySkill) {
// TODO: Use the primaries ID to find the primary skill not the description
var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription();
var existingPrimarySkill = primarySkillService.findByDescription(primarySkillDescription);
@ -56,14 +51,4 @@ public class SecondarySkillService {
}
return result;
}
public void deleteSecondarySkillFromEmployee(Long userId, Long skillId) {
var employee = employeeService.getEmployeeById(userId)
.orElseThrow(() -> new RuntimeException("Employee not found"));
getSecondarySkillById(skillId)
.orElseThrow(() -> new RuntimeException("Secondary Skill not found"));
employee.getSecondarySkills().removeIf(skill -> skill.getSecondarySkill().getSsid().equals(skillId));
employeeService.saveEmployee(employee);
}
}
}

View File

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