diff --git a/WebService/pom.xml b/WebService/pom.xml index fb20a79..dbbd774 100644 --- a/WebService/pom.xml +++ b/WebService/pom.xml @@ -26,6 +26,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-hateoas + org.springframework.boot spring-boot-starter-web diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java index 9a39d55..548cfbe 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java @@ -4,7 +4,10 @@ import SAE.ApiREST.WebService.exception.TeacherAdvice; import SAE.ApiREST.WebService.exception.TeacherException; import SAE.ApiREST.WebService.model.Teacher; import SAE.ApiREST.WebService.service.ITeacherService; +import jakarta.persistence.Entity; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.EntityModel; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; @@ -15,7 +18,9 @@ import java.awt.*; import java.util.ArrayList; import java.util.List; -//Todo() Response = Type de retour pour toutes les méthodes qui ont void +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; + @Controller @RequestMapping("/ProfWebService") public class TeacherController { @@ -29,37 +34,48 @@ public class TeacherController { @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public @ResponseBody List getAllTeacher(){ - return iTeacherServ.getAllTeacher(); + public @ResponseBody CollectionModel getAllTeacher(){ + return CollectionModel.of( + iTeacherServ.getAllTeacher(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel()); } @PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){ - return teach; + public @ResponseBody EntityModel createTeacher( @RequestBody Teacher teach){ + + return EntityModel.of(teach, + linkTo(methodOn(TeacherController.class).createTeacher(teach)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } @GetMapping(value = "/getid/{id}") - public @ResponseBody Teacher getTeachById(@PathVariable("id") Integer id){ + public @ResponseBody EntityModel getTeachById(@PathVariable("id") Integer id){ Teacher tt = iTeacherServ.getTeacherById(id); if( tt == null ){ throw new TeacherException("No teacher found for this id !"); } - return tt; + return EntityModel.of(tt, + linkTo(methodOn(TeacherController.class).getTeachById(id)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } @GetMapping(value = "/getusername/{username}") - public @ResponseBody Teacher getTeachByUsername(@PathVariable("username") String username) { + public @ResponseBody EntityModel getTeachByUsername(@PathVariable("username") String username) { Teacher tt = iTeacherServ.getTeacherByUsername(username); if (tt == null) { throw new TeacherException("No teacher found for this username"); } - return tt; + return EntityModel.of(tt, + linkTo(methodOn(TeacherController.class).getTeachByUsername(username)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } @GetMapping( value = "/getmail/{mail}" ) - public @ResponseBody Teacher getTeachByMail(@PathVariable("mail") String mail) { + public @ResponseBody EntityModel getTeachByMail(@PathVariable("mail") String mail) { Teacher tt = iTeacherServ.getTeacherByMail(mail); if( tt == null ) { throw new TeacherException("No teacher found for this mail"); } - return tt; + return EntityModel.of(tt, + linkTo(methodOn(TeacherController.class).getTeachByMail(mail)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } @GetMapping( value = "/getdate/{date}" ) public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) { @@ -70,15 +86,19 @@ public class TeacherController { return tt; } @PutMapping( value = "/modify/{username}" ) - public @ResponseBody Teacher modifyTeachUsername(@PathVariable("username") String username, Teacher tt){ + public @ResponseBody EntityModel modifyTeachUsername(@PathVariable("username") String username, Teacher tt){ if( username == "" ){ throw new TeacherException("Username provided for modification is empty"); } iTeacherServ.modifyUsername(tt,username); - return tt; + return EntityModel.of(tt, + linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } @DeleteMapping( value = "delete") - public @ResponseBody List deleteTeacher(Integer id){ - return iTeacherServ.deleteTeacher(id); + public @ResponseBody EntityModel> deleteTeacher(Integer id){ + return EntityModel.of(iTeacherServ.deleteTeacher(id), + linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(), + linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); } } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java index 68f617f..a4e9f51 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java @@ -40,6 +40,7 @@ public class TeacherServiceStub implements ITeacherService { return new Teacher(20, "24-12-2021", mail, "tructruc"); } + //Todo() Before date, After date, between date @Override public Teacher getTeacherByDate(String date) { return new Teacher(5, date, "doudouda@gmail.com", "username");