adding Hateos on the Teacher part

feature/ProfSection
Roxane ROSSETTO 1 year ago
parent d57ed6e020
commit 6a7968d2b5

@ -26,6 +26,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

@ -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<Teacher> getAllTeacher(){
return iTeacherServ.getAllTeacher();
public @ResponseBody CollectionModel<Teacher> 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<Teacher> 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<Teacher> 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<Teacher> 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<Teacher> 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<Teacher> 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<Teacher> deleteTeacher(Integer id){
return iTeacherServ.deleteTeacher(id);
public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){
return EntityModel.of(iTeacherServ.deleteTeacher(id),
linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
}
}

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

Loading…
Cancel
Save