diff --git a/apps/java-api/src/main/java/org/scoalaonline/api/config/WebConfig.Java b/apps/java-api/src/main/java/org/scoalaonline/api/config/WebConfig.Java new file mode 100644 index 0000000..44cfb01 --- /dev/null +++ b/apps/java-api/src/main/java/org/scoalaonline/api/config/WebConfig.Java @@ -0,0 +1,19 @@ +package org.scoalaonline.api.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("DELETE", "GET", "PUT", "POST") + .maxAge(3600); + } +} diff --git a/apps/java-api/src/main/java/org/scoalaonline/api/controller/CorporatistController.java b/apps/java-api/src/main/java/org/scoalaonline/api/controller/CorporatistController.java index 3e794a2..d141601 100644 --- a/apps/java-api/src/main/java/org/scoalaonline/api/controller/CorporatistController.java +++ b/apps/java-api/src/main/java/org/scoalaonline/api/controller/CorporatistController.java @@ -62,7 +62,6 @@ public ResponseEntity addCorporatist (@RequestBody Corporatist corp * @param corporatist which is the updated entity * @return the updated Corporatist with the Http status created. */ - @PutMapping(value = ("/{id}")) public ResponseEntity updateCorporatist (@PathVariable("id") long id, @RequestBody Corporatist corporatist) { @@ -75,4 +74,21 @@ public ResponseEntity updateCorporatist (@PathVariable("id") long i ); } } + + /** + * Deletes a Corporatist entity from the database. + * @param id which is the entity's id to be deleted + * @return the deleted Corporatist with the Http status created. + */ + @DeleteMapping(value = ("/{id}")) + public ResponseEntity deleteCorporatist (@PathVariable("id") long id) { + if (corporatistService.corporatistExists(id)) { + corporatistService.deleteCorporatist(id); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } else { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cannot delete non-existing Corporatist", new ResourceNotFoundException() + ); + } + } } diff --git a/apps/java-api/src/main/java/org/scoalaonline/api/controller/DepartmentController.java b/apps/java-api/src/main/java/org/scoalaonline/api/controller/DepartmentController.java index 881f5ba..02835ab 100644 --- a/apps/java-api/src/main/java/org/scoalaonline/api/controller/DepartmentController.java +++ b/apps/java-api/src/main/java/org/scoalaonline/api/controller/DepartmentController.java @@ -45,7 +45,6 @@ public ResponseEntity getDepartmentById(@PathVariable("id") long id) return new ResponseEntity<>(department, HttpStatus.OK); } - /** * Adds a department entity into the database. * @param department which is the entity to be added @@ -57,16 +56,16 @@ public ResponseEntity addDepartment (@RequestBody Department departm return new ResponseEntity<>(savedDepartment, HttpStatus.CREATED); } + /** * Updates a Department entity from the database. * @param id which is the entity's id to be changed * @param department which is the updated entity * @return the updated Department with the Http status created. */ - @PutMapping(value = ("/{id}")) public ResponseEntity updateDepartment (@PathVariable("id") long id, - @RequestBody Department department) { + @RequestBody Department department) { if (departmentService.departmentExists(id)) { Department updatedDepartment = departmentService.updateDepartment(id, department); return new ResponseEntity<>(updatedDepartment, HttpStatus.ACCEPTED); @@ -76,4 +75,21 @@ public ResponseEntity updateDepartment (@PathVariable("id") long id, ); } } + + /** + * Deletes a Department entity from the database. + * @param id which is the entity's id to be deleted + * @return the deleted Department with the Http status created. + */ + @DeleteMapping(value = ("/{id}")) + public ResponseEntity deleteDepartment (@PathVariable("id") long id) { + if (departmentService.departmentExists(id)) { + departmentService.deleteDepartment(id); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } else { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cannot delete non-existing Department", new ResourceNotFoundException() + ); + } + } } diff --git a/apps/java-api/src/main/java/org/scoalaonline/api/controller/SupervisorController.java b/apps/java-api/src/main/java/org/scoalaonline/api/controller/SupervisorController.java index a91dc0f..dc017f0 100644 --- a/apps/java-api/src/main/java/org/scoalaonline/api/controller/SupervisorController.java +++ b/apps/java-api/src/main/java/org/scoalaonline/api/controller/SupervisorController.java @@ -1,8 +1,6 @@ package org.scoalaonline.api.controller; import net.bytebuddy.implementation.bind.annotation.Super; -import org.scoalaonline.api.model.Corporatist; -import org.scoalaonline.api.model.Department; import org.scoalaonline.api.model.Supervisor; import org.scoalaonline.api.service.SupervisorService; import org.springframework.beans.factory.annotation.Autowired; @@ -59,13 +57,13 @@ public ResponseEntity addSupervisor (@RequestBody Supervisor supervi return new ResponseEntity<>(savedSupervisor, HttpStatus.CREATED); } + /** * Updates a Supervisor entity from the database. * @param id which is the entity's id to be changed * @param supervisor which is the updated entity * @return the updated Supervisor with the Http status created. */ - @PutMapping(value = ("/{id}")) public ResponseEntity updateSupervisor (@PathVariable("id") long id, @RequestBody Supervisor supervisor) { @@ -78,4 +76,22 @@ public ResponseEntity updateSupervisor (@PathVariable("id") long id, ); } } + + /** + * Deletes a Supervisor entity from the database. + * @param id which is the entity's id to be deleted + * @return the deleted Supervisor with the Http status created. + */ + @DeleteMapping(value = ("/{id}")) + public ResponseEntity deleteSupervisor (@PathVariable("id") long id) { + if (supervisorService.supervisorExists(id)) { + supervisorService.deleteSupervisor(id); + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } else { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "Cannot delete non-existing Supervisor", new ResourceNotFoundException() + ); + } + } + }