refactor: Use ResponseEntity

pull/1/head
Lunix-420 2024-11-08 10:08:12 +01:00
parent 228bcf2221
commit 39d57e8991
4 changed files with 79 additions and 54 deletions

View File

@ -1,18 +1,22 @@
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.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.RequestBody;
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.entities.SecondarySkill;
import com.maradona.backend.services.EmployeeService;
import com.maradona.backend.services.SecondarySkillService;
@ -50,9 +54,10 @@ public class EmployeeController {
* @see Employee
*/
@GetMapping({ "/", "" })
public void get(@RequestParam Long id, Model model) {
var employee = employeeService.getEmployeeById(id).orElse(null);
model.addAttribute("employee", employee);
public ResponseEntity<Employee> get(@RequestParam Long id) {
return employeeService.getEmployeeById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
/**
@ -62,9 +67,8 @@ public class EmployeeController {
* @see Employee
*/
@GetMapping("/all")
public void getAll(Model model) {
var employees = employeeService.getAllEmployees();
model.addAttribute("employees", employees);
public ResponseEntity<Iterable<Employee>> getAll() {
return ResponseEntity.ok(employeeService.getAllEmployees());
}
/**
@ -76,9 +80,8 @@ public class EmployeeController {
* @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);
public ResponseEntity<Iterable<Employee>> getFromSkill(@RequestParam Long skillId, @RequestParam Integer level) {
return ResponseEntity.ok(employeeService.getEmployeesBySecondarySkill(skillId, level));
}
/**
@ -89,8 +92,9 @@ public class EmployeeController {
* @see SkillPrototype
*/
@PostMapping("/skill/protoype")
public void postSkillProtoype(@ModelAttribute SkillPrototype skillPrototype) {
public ResponseEntity<Void> postSkillProtoype(@RequestBody SkillPrototype skillPrototype) {
employeeService.addSecondarySkillToEmployee(user, skillPrototype);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
/**
@ -101,8 +105,9 @@ public class EmployeeController {
* @see SecondarySkill
*/
@PutMapping("/skill/level")
public void putSkillLevel(@RequestParam Long skillId, @RequestParam Integer level) {
public ResponseEntity<Void> putSkillLevel(@RequestParam Long skillId, @RequestParam Integer level) {
employeeService.updateSecondarySkillLevel(user, skillId, level);
return ResponseEntity.ok().build();
}
/**
@ -112,7 +117,8 @@ public class EmployeeController {
* @see SecondarySkill
*/
@DeleteMapping("/skill")
public void delete(@RequestParam Long id) {
public ResponseEntity<Void> delete(@RequestParam Long id) {
employeeService.deleteSecondarySkillFromEmployee(user, id);
return ResponseEntity.noContent().build();
}
}

View File

@ -1,6 +1,8 @@
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;
@ -45,8 +47,10 @@ public class PrimarySkillController {
* @see PrimarySkill
*/
@GetMapping({ "/", "" })
public PrimarySkill get(@RequestParam Long id) {
return primarySkillService.getPrimarySkillById(id).orElse(null);
public ResponseEntity<PrimarySkill> get(@RequestParam Long id) {
return primarySkillService.getPrimarySkillById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
/**
@ -56,8 +60,8 @@ public class PrimarySkillController {
* @see PrimarySkill
*/
@GetMapping("/all")
public Iterable<PrimarySkill> getAll() {
return primarySkillService.getAllPrimarySkills();
public ResponseEntity<Iterable<PrimarySkill>> getAll() {
return ResponseEntity.ok(primarySkillService.getAllPrimarySkills());
}
/**
@ -68,8 +72,9 @@ public class PrimarySkillController {
* @see PrimarySkill
*/
@PostMapping({ "/", "" })
public void post(@RequestBody PrimarySkill primarySkill) {
primarySkillService.savePrimarySkill(primarySkill);
public ResponseEntity<PrimarySkill> post(@RequestBody PrimarySkill primarySkill) {
PrimarySkill savedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedPrimarySkill);
}
/**
@ -80,13 +85,13 @@ public class PrimarySkillController {
* @see PrimarySkill
*/
@PutMapping({ "/", "" })
public void put(@RequestBody PrimarySkill primarySkill) {
primarySkillService.savePrimarySkill(primarySkill);
public ResponseEntity<PrimarySkill> put(@RequestBody PrimarySkill primarySkill) {
PrimarySkill updatedPrimarySkill = primarySkillService.savePrimarySkill(primarySkill);
return ResponseEntity.ok(updatedPrimarySkill);
}
@DeleteMapping({ "/", "" })
public void delete(@RequestParam Long id) {
// Find and delete all secondary skills associated with the primary skill
public ResponseEntity<Void> delete(@RequestParam Long id) {
var primarySkill = primarySkillService.getPrimarySkillById(id);
if (primarySkill.isPresent()) {
var secondarySkills = secondarySkillService.getAllSecondarySkills();
@ -95,8 +100,10 @@ public class PrimarySkillController {
secondarySkillService.deleteSecondarySkill(secondarySkill.getSsid());
}
}
// Delete the primary skill
primarySkillService.deletePrimarySkill(id);
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
}
}

View File

@ -1,6 +1,10 @@
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -8,6 +12,7 @@ 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -52,9 +57,9 @@ public class ProjectController {
* @see Project
*/
@GetMapping({ "/", "" })
public void get(@RequestParam Long id, Model model) {
var project = projectService.getProjectById(id).orElse(null);
model.addAttribute("project", project);
public ResponseEntity<Project> get(@RequestParam Long id) {
Optional<Project> project = projectService.getProjectById(id);
return project.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
@ -64,9 +69,8 @@ public class ProjectController {
* @see Project
*/
@GetMapping("/all")
public void getAll(Model model) {
var projects = projectService.getAllProjects();
model.addAttribute("projects", projects);
public ResponseEntity<Iterable<Project>> getAll() {
return ResponseEntity.ok(projectService.getAllProjects());
}
/**
@ -78,9 +82,8 @@ public class ProjectController {
* @see Project
*/
@GetMapping("/from-user")
public void getFromUser(@ModelAttribute Project project, Model model) {
var projects = projectService.getProjectsByUser(user);
model.addAttribute("projects", projects);
public ResponseEntity<Iterable<Project>> getFromUser(@RequestParam Long userId) {
return ResponseEntity.ok(projectService.getProjectsByUser(userId));
}
/**
@ -89,9 +92,10 @@ public class ProjectController {
* @param project The project to be created.
* @see Project
*/
@PostMapping
public void create(@ModelAttribute Project project) {
projectService.saveProject(project);
@PostMapping({ "/", "" })
public ResponseEntity<Project> post(@RequestBody Project project) {
Project savedProject = projectService.saveProject(project);
return ResponseEntity.status(HttpStatus.CREATED).body(savedProject);
}
/**
@ -100,9 +104,10 @@ public class ProjectController {
* @param project The project to be updated.
* @see Project
*/
@PutMapping
public void update(@ModelAttribute Project project) {
projectService.saveProject(project);
@PutMapping({ "/", "" })
public ResponseEntity<Project> put(@RequestBody Project project) {
Project updatedProject = projectService.saveProject(project);
return ResponseEntity.ok(updatedProject);
}
/**
@ -110,8 +115,9 @@ public class ProjectController {
*
* @param id The ID of the project to be deleted.
*/
@DeleteMapping
public void delete(@RequestParam Long id) {
@DeleteMapping({ "/", "" })
public ResponseEntity<Void> delete(@RequestParam Long id) {
projectService.deleteProject(id);
return ResponseEntity.noContent().build();
}
}

View File

@ -1,6 +1,10 @@
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -8,6 +12,7 @@ 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@ -52,9 +57,9 @@ public class SecondarySkillController {
* @see SecondarySkill
*/
@GetMapping({ "/", "" })
public void get(@RequestParam Long id, Model model) {
var skill = secondarySkillService.getSecondarySkillById(id).orElse(null);
model.addAttribute("secondarySkill", skill);
public ResponseEntity<SecondarySkill> get(@RequestParam Long id) {
Optional<SecondarySkill> secondarySkill = secondarySkillService.getSecondarySkillById(id);
return secondarySkill.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
@ -64,9 +69,8 @@ public class SecondarySkillController {
* @see SecondarySkill
*/
@GetMapping("/all")
public void getAll(Model model) {
var skills = secondarySkillService.getAllSecondarySkills();
model.addAttribute("secondarySkills", skills);
public ResponseEntity<Iterable<SecondarySkill>> getAll() {
return ResponseEntity.ok(secondarySkillService.getAllSecondarySkills());
}
/**
@ -78,9 +82,8 @@ public class SecondarySkillController {
* @see PrimarySkill
*/
@GetMapping("/from-primary-skill")
@ResponseBody
public Iterable<SecondarySkill> getSecondarySkills(@RequestParam Long primarySkillId) {
return secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId);
public ResponseEntity<Iterable<SecondarySkill>> getSecondarySkills(@RequestParam Long primarySkillId) {
return ResponseEntity.ok(secondarySkillService.getSecondarySkillsByPrimarySkillId(primarySkillId));
}
/**
@ -92,8 +95,9 @@ public class SecondarySkillController {
* @see PrimarySkill
*/
@PostMapping({ "/", "" })
public void post(@ModelAttribute SecondarySkill secondarySkill) {
secondarySkillService.saveSecondarySkill(secondarySkill);
public ResponseEntity<SecondarySkill> post(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill savedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill);
return ResponseEntity.status(HttpStatus.CREATED).body(savedSecondarySkill);
}
/**
@ -103,8 +107,9 @@ public class SecondarySkillController {
* @see SecondarySkill
*/
@PutMapping({ "/", "" })
public void put(@ModelAttribute SecondarySkill secondarySkill) {
secondarySkillService.saveSecondarySkill(secondarySkill);
public ResponseEntity<SecondarySkill> put(@RequestBody SecondarySkill secondarySkill) {
SecondarySkill updatedSecondarySkill = secondarySkillService.saveSecondarySkill(secondarySkill);
return ResponseEntity.ok(updatedSecondarySkill);
}
/**
@ -113,7 +118,8 @@ public class SecondarySkillController {
* @param id The ID of the secondary skill to be deleted
*/
@DeleteMapping({ "/", "" })
public void delete(@RequestParam Long id) {
public ResponseEntity<Void> delete(@RequestParam Long id) {
secondarySkillService.deleteSecondarySkill(id);
return ResponseEntity.noContent().build();
}
}