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

Reviewed-on: 3001291/Backend#9
pull/1/head
Tarik Gökmen 2024-11-08 10:53:12 +01:00
commit 67918b226a
18 changed files with 730 additions and 232 deletions

Binary file not shown.

View File

@ -94,6 +94,14 @@
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<destDir>${project.build.directory}/docs</destDir>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -9,8 +9,6 @@ import com.maradona.backend.repositories.EmployeeRepository;
import com.maradona.backend.repositories.FormOfAddressRepository;
import com.maradona.backend.repositories.PrimarySkillRepository;
import com.maradona.backend.repositories.SecondarySkillRepository;
import com.maradona.backend.repositories.EmployeeSecondarySkillRepository;
import com.maradona.backend.repositories.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@ -36,6 +34,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

@ -1,41 +0,0 @@
package com.maradona.backend.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.maradona.backend.entities.Project;
import com.maradona.backend.services.ProjectService;
@Controller
@RequestMapping("/projects")
public class ProjectController {
@Autowired
private ProjectService projectService;
@GetMapping({ "/", "" })
public String projects(Model model) {
var projectData = projectService.getAllProjects();
model.addAttribute("projectData", projectData);
return "projects/projects";
}
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("project", new Project());
return "projects/projects-create";
}
@PostMapping("/save")
public String save(@ModelAttribute Project project) {
projectService.saveProject(project);
return "redirect:/projects";
}
@PostMapping("/delete")
public String delete(@RequestParam Long id) {
projectService.deleteProject(id);
return "redirect:/projects";
}
}

View File

@ -1,173 +0,0 @@
package com.maradona.backend.controllers;
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.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.entities.EmployeeSecondarySkill;
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 java.util.List;
@Controller
@RequestMapping("/skills")
public class SkillsController {
@Autowired
private EmployeeService employeeService;
@Autowired
private SkillService skillService;
@Autowired
private PrimarySkillService primarySkillService;
@Autowired
private SecondarySkillService secondarySkillService;
// Hardcoded placeholder user ID for now
Long user = Long.valueOf(1);
// Returns the skills overview page
@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
return "skills/skills";
}
// Returns the create-skill page
@GetMapping("/create")
public String createSkill(Model model) {
model.addAttribute("secondarySkill", new SecondarySkill());
return "skills/skills-create";
}
// Add a new skill to the user profile
@GetMapping("/add")
public String addSkill(Model model) {
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
@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";
}
// Returns a list of secondary skills for a given primary skill
@GetMapping("/secondary-skills")
@ResponseBody
public Iterable<SecondarySkill> 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
@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";
}
// 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) {
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

@ -0,0 +1,125 @@
package com.maradona.backend.controllers.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.dto.SkillPrototype;
import com.maradona.backend.entities.Employee;
import com.maradona.backend.services.EmployeeService;
/**
* 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 com.maradona.backend.entities.Employee
*/
@RestController
@RequestMapping("/api/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
Long user = Long.valueOf(1);
/**
* Returns a specific employee from the database.
*
* @param id The ID of the requested employee.
* @return The employee with the requested ID.
* @see com.maradona.backend.entities.Employee
*/
@GetMapping({ "/", "" })
public ResponseEntity<Employee> get(@RequestParam Long id) {
return employeeService.getEmployeeById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
/**
* Returns all employees from the database.
*
* @return list of employees
* @see com.maradona.backend.entities.Employee
*/
@GetMapping("/all")
public ResponseEntity<Iterable<Employee>> getAll() {
return ResponseEntity.ok(employeeService.getAllEmployees());
}
/**
* 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 com.maradona.backend.entities.Employee
* @see com.maradona.backend.entities.SecondarySkill
*/
@GetMapping("/from-skill")
public ResponseEntity<Iterable<Employee>> getFromSkill(@RequestParam Long skillId, @RequestParam Integer level) {
return ResponseEntity.ok(employeeService.getEmployeesBySecondarySkill(skillId, level));
}
/**
* 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
* @return HTTP status indicating the outcome of the operation
* @see com.maradona.backend.entities.Employee
* @see com.maradona.backend.entities.SecondarySkill
*/
@PostMapping("/skill/protoype")
public ResponseEntity<Void> postSkillProtoype(@RequestBody SkillPrototype skillPrototype) {
employeeService.addSecondarySkillToEmployee(user, skillPrototype);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
/**
* 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
* @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<Void> putSkillLevel(@RequestParam Long skillId, @RequestParam Integer level) {
employeeService.updateSecondarySkillLevel(user, skillId, level);
return ResponseEntity.ok().build();
}
/**
* Removes a secondary skill from the user profile.
*
* @param id 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<Void> delete(@RequestParam Long id) {
employeeService.deleteSecondarySkillFromEmployee(user, id);
return ResponseEntity.noContent().build();
}
}

View File

@ -0,0 +1,116 @@
package com.maradona.backend.controllers.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 com.maradona.backend.entities.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 com.maradona.backend.entities.PrimarySkill
*/
@GetMapping({ "/", "" })
public ResponseEntity<PrimarySkill> get(@RequestParam Long id) {
return primarySkillService.getPrimarySkillById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
/**
* Returns all primary skills from the database.
*
* @return list of primary skills
* @see com.maradona.backend.entities.PrimarySkill
*/
@GetMapping("/all")
public ResponseEntity<Iterable<PrimarySkill>> getAll() {
return ResponseEntity.ok(primarySkillService.getAllPrimarySkills());
}
/**
* Adds a new primary skill to the database.
*
* @param primarySkill The primary skill to be added
* @return The added primary skill
* @see com.maradona.backend.entities.PrimarySkill
*/
@PostMapping({ "/", "" })
public ResponseEntity<PrimarySkill> post(@RequestBody PrimarySkill primarySkill) {
PrimarySkill savedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedPrimarySkill);
}
/**
* Modifies an existing primary skill in the database.
*
* @param primarySkill The primary skill to be modified
* @return The modified primary skill
* @see com.maradona.backend.entities.PrimarySkill
*/
@PutMapping({ "/", "" })
public ResponseEntity<PrimarySkill> put(@RequestBody PrimarySkill primarySkill) {
PrimarySkill updatedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill);
return ResponseEntity.ok(updatedPrimarySkill);
}
/**
* Deletes a primary skill from the database.
*
* @param id 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<Void> delete(@RequestParam Long id) {
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());
}
}
primarySkillService.deletePrimarySkill(id);
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
}
}

View File

@ -0,0 +1,116 @@
package com.maradona.backend.controllers.api;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.Project;
import com.maradona.backend.services.ProjectService;
/**
* Controller for handling project data.
*
* List of endpoints:
* - GET /api/project
* - GET /api/project/all
* - GET /api/project/from-user
* - POST /api/project
* - PUT /api/project
* - DELETE /api/project
*
* @warning The ProjectService is not accounting for the user yet.
* @see com.maradona.backend.entities.Project
* @see com.maradona.backend.services.ProjectService
*/
@RestController
@RequestMapping("/api/project")
public class ProjectController {
@Autowired
private ProjectService projectService;
/**
* Returns a specific project from the database.
*
* @param id The ID of the requested project.
* @return The project with the requested ID.
* @see com.maradona.backend.entities.Project
*/
@GetMapping({ "/", "" })
public ResponseEntity<Project> get(@RequestParam Long id) {
Optional<Project> project = projectService.getProjectById(id);
return project.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Returns all projects from the database.
*
* @return list of projects
* @see com.maradona.backend.entities.Project
*/
@GetMapping("/all")
public ResponseEntity<Iterable<Project>> getAll() {
return ResponseEntity.ok(projectService.getAllProjects());
}
/**
* Returns a list of projects for a given user.
*
* @param userId ID of the user
* @return list of projects
* @see com.maradona.backend.entities.Project
*/
@GetMapping("/from-user")
public ResponseEntity<Iterable<Project>> getFromUser(@RequestParam Long userId) {
return ResponseEntity.ok(projectService.getProjectsByUserId(userId));
}
/**
* Adds a new project to the database.
*
* @param project The project to be added
* @return The added project
* @see com.maradona.backend.entities.Project
*/
@PostMapping({ "/", "" })
public ResponseEntity<Project> post(@RequestBody Project project) {
Project savedProject = projectService.saveProject(project);
return ResponseEntity.status(HttpStatus.CREATED).body(savedProject);
}
/**
* Modifies an existing project in the database.
*
* @param project The project to be modified
* @return The modified project
* @see com.maradona.backend.entities.Project
*/
@PutMapping({ "/", "" })
public ResponseEntity<Project> put(@RequestBody Project project) {
Project updatedProject = projectService.saveProject(project);
return ResponseEntity.ok(updatedProject);
}
/**
* Deletes a project from the database.
*
* @param id 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<Void> delete(@RequestParam Long id) {
projectService.deleteProject(id);
return ResponseEntity.noContent().build();
}
}

View File

@ -0,0 +1,115 @@
package com.maradona.backend.controllers.api;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.SecondarySkill;
import com.maradona.backend.services.SecondarySkillService;
/**
* Controller for handling secondary skill data.
*
* List of endpoints:
* - GET /api/secondary-skill
* - GET /api/secondary-skill/all
* - GET /api/secondary-skill/from-primary-skill
* - POST /api/secondary-skill
* - PUT /api/secondary-skill
* - DELETE /api/secondary-skill
*
* @see com.maradona.backend.entities.SecondarySkill
*/
@RestController
@RequestMapping("/api/secondary-skill")
public class SecondarySkillController {
@Autowired
private SecondarySkillService secondarySkillService;
/**
* Returns a specific secondary skill from the database.
*
* @param id The ID of the requested secondary skill.
* @return The secondary skill with the requested ID.
* @see com.maradona.backend.entities.SecondarySkill
*/
@GetMapping({ "/", "" })
public ResponseEntity<SecondarySkill> get(@RequestParam Long id) {
Optional<SecondarySkill> secondarySkill = secondarySkillService.getSecondarySkillById(id);
return secondarySkill.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Returns all secondary skills from the database.
*
* @return list of secondary skills
* @see com.maradona.backend.entities.SecondarySkill
*/
@GetMapping("/all")
public ResponseEntity<Iterable<SecondarySkill>> getAll() {
return ResponseEntity.ok(secondarySkillService.getAllSecondarySkills());
}
/**
* Returns a list of secondary skills for a given primary skill.
*
* @param primarySkillId 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<Iterable<SecondarySkill>> getSecondarySkills(@RequestParam Long primarySkillId) {
return ResponseEntity.ok(secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId));
}
/**
* Adds a new secondary skill to the database.
*
* @param secondarySkill The secondary skill to be added
* @return The added secondary skill
* @see com.maradona.backend.entities.SecondarySkill
*/
@PostMapping({ "/", "" })
public ResponseEntity<SecondarySkill> post(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill savedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedSecondarySkill);
}
/**
* Modifies an existing secondary skill in the database.
*
* @param secondarySkill The secondary skill to be modified
* @return The modified secondary skill
* @see com.maradona.backend.entities.SecondarySkill
*/
@PutMapping({ "/", "" })
public ResponseEntity<SecondarySkill> put(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill updatedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill);
return ResponseEntity.ok(updatedSecondarySkill);
}
/**
* Deletes a secondary skill from the database.
*
* @param id 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<Void> delete(@RequestParam Long id) {
secondarySkillService.deleteSecondarySkill(id);
return ResponseEntity.noContent().build();
}
}

View File

@ -1,11 +1,11 @@
package com.maradona.backend.controllers;
package com.maradona.backend.controllers.page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PageController {
public class Page {
@GetMapping({ "/", "" })
public String home(Model model) {

View File

@ -0,0 +1,56 @@
package com.maradona.backend.controllers.page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.maradona.backend.entities.Project;
import com.maradona.backend.services.ProjectService;
/**
* Controller for routing to project related pages.
*
* List of endpoints:
* - GET /projects
* - GET /projects/create
*
* @see Project
*/
@Controller
@RequestMapping("/projects")
public class ProjectPage {
@Autowired
private ProjectService projectService;
/**
* Returns the projects overview page.
* Collects the project list and adds it to the model for the frontend.
*
* Attributes that can be used by the frontend are a List of Project objects
* "projects".
*
* @param model The model with the data to be displayed.
* @return The projects overview page template
* @see Project
*/
@GetMapping({ "/", "" })
public String projects(Model model) {
var projects = projectService.getAllProjects();
model.addAttribute("projects", projects);
return "projects/projects";
}
/**
* Returns the project creation page.
*
* @param model The model with the data to be displayed.
* @return The project creation page template
* @see Project
*/
@GetMapping("/create")
public String create(Model model) {
model.addAttribute("project", new Project());
return "projects/projects-create";
}
}

View File

@ -0,0 +1,102 @@
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.RequestMapping;
import org.springframework.stereotype.Controller;
import com.maradona.backend.entities.SecondarySkill;
import com.maradona.backend.entities.Employee;
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.dto.SkillPrototype;
/**
* 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 {
@Autowired
private EmployeeService employeeService;
@Autowired
private SkillService skillService;
@Autowired
private PrimarySkillService primarySkillService;
// Hardcoded placeholder user ID for now
Long user = Long.valueOf(1);
/**
* 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) {
model.addAttribute("employee", employeeService.getEmployeeById(user).orElse(null));
model.addAttribute("skills", skillService.getUserSkills(user));
return "skills/skills";
}
/**
* 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";
}
/**
* 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
* SkillProtoype object "skillProtoype".
*
* The skillProtoype object contains the data needed to add a skill to the user
* profile.
* To add it to a user profile, POST to /api/employee/skill/protoype.
*
* @param model The model with the data to be displayed
* @return The add skill page template
* @see PrimarySkill
* @see SecondarySkill
* @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("skillProtoype", new SkillPrototype());
return "skills/skills-add";
}
}

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

@ -28,4 +28,9 @@ public class ProjectService {
public Iterable<Project> getAllProjects() {
return projectRepository.findAll();
}
public Iterable<Project> getProjectsByUserId(Long userId) {
// TODO: Actually filter by user
return projectRepository.findAll();
}
}

View File

@ -9,11 +9,22 @@ import java.util.Optional;
@Service
public class SecondarySkillService {
@Autowired
private SecondarySkillRepository secondarySkillRepository;
@Autowired
private PrimarySkillService primarySkillService;
public SecondarySkill saveSecondarySkill(SecondarySkill secondarySkill) {
var primarySkillDescription = secondarySkill.getPrimarySkill().getDescription();
var existingPrimarySkill = primarySkillService.findByDescription(primarySkillDescription);
if (existingPrimarySkill.isPresent()) {
secondarySkill.setPrimarySkill(existingPrimarySkill.get());
} else {
primarySkillService.savePrimarySkill(secondarySkill.getPrimarySkill());
}
return secondarySkillRepository.save(secondarySkill);
}
@ -40,4 +51,4 @@ public class SecondarySkillService {
}
return result;
}
}
}

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