From dbad7174ec386877b861a0245f20f2da2b14c90e Mon Sep 17 00:00:00 2001 From: felix Date: Tue, 27 Feb 2024 16:18:21 +0100 Subject: [PATCH 1/7] Finshed api + tested --- .../controller/ArticleControler.java | 172 +++++++++++++++++- .../SAE/ApiREST/WebService/model/Article.java | 44 ++--- .../WebService/service/IArticleService.java | 25 +++ .../service/StubArticleService.java | 131 ++++++++++++- 4 files changed, 346 insertions(+), 26 deletions(-) diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java index 4449e34..222846c 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java @@ -5,6 +5,7 @@ import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import SAE.ApiREST.WebService.Response; import SAE.ApiREST.WebService.exception.ArticleException; import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.service.IArticleService; @@ -19,15 +20,92 @@ public class ArticleControler { IArticleService articleService; // region POST - + @PostMapping( + value = "/addArticle", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response addArticle( + @RequestBody Article article + ) { + return articleService.addArticle(article); + } // endregion // region PUT - + @PutMapping( + value = "/updateTitle/{title}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article updateTitle( + @PathVariable("title") String title, + @RequestBody Article article + ) { + return articleService.updateTitle(article, title); + } + + @PutMapping( + value = "/updateUrl/{url}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article updateUrl( + @PathVariable("url") String url, + @RequestBody Article article + ) { + return articleService.updateUrl(article, url); + } + + @PutMapping( + value = "/updateDatePublished/{datePublished}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article updateDatePublished( + @PathVariable("datePublished") String datePublished, + @RequestBody Article article + ) { + return articleService.updateDatePublished(article, datePublished); + } + + @PutMapping( + value = "/updateDateAdded/{dateAdded}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article updateDateAdded( + @PathVariable("dateAdded") String dateAdded, + @RequestBody Article article + ) { + return articleService.updateDateAdded(article, dateAdded); + } + + @PutMapping( + value = "/changeVisibility", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article changeVisibility( + @RequestBody Article article + ) { + return articleService.changeVisibility(article); + } + + @PutMapping( + value = "/updateType/{type}", + consumes = MediaType.APPLICATION_JSON_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Article updateType( + @PathVariable("type") Integer type, + @RequestBody Article article + ) { + return articleService.updateType(article, type); + } // endregion // region GET - @GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE) public @ResponseBody List
getAllArticles() { ArrayList
results = (ArrayList
) articleService.getAllArticles(); @@ -160,6 +238,94 @@ public class ArticleControler { // endregion // region DELETE + @DeleteMapping( + value = "/deleteArticleFromId/{id}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleFromId( + @PathVariable("id") Integer id + ) { + return articleService.deleteArticleFromId(id); + } + + @DeleteMapping( + value = "/deleteArticleFromTitle/{title}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleFromTitle( + @PathVariable("title") String title + ) { + return articleService.deleteArticleFromTitle(title); + } + + @DeleteMapping( + value = "/deleteArticleFromUrl/{url}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleFromUrl( + @PathVariable("url") String url + ) { + return articleService.deleteArticleFromUrl(url); + } + + @DeleteMapping( + value = "/deleteArticleAddedBefore/{date}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleAddedBefore( + @PathVariable("date") String date + ) { + return articleService.deleteArticleAddedBefore(date); + } + @DeleteMapping( + value = "/deleteArticleAddedAfter/{date}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleAddedAfter( + @PathVariable("date") String date + ) { + return articleService.deleteArticleAddedAfter(date); + }@DeleteMapping( + value = "/deleteArticleAddedBetween/{beginning}/{end}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticleAddedBetween( + @PathVariable("beginning") String beginning, + @PathVariable("end") String end + ) { + return articleService.deleteArticleAddedBetween(beginning, end); + } + + @DeleteMapping( + value = "/deleteArticlePublishedBefore/{date}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticlePublishedBefore( + @PathVariable("date") String date + ) { + return articleService.deleteArticlePublishedBefore(date); + } + + @DeleteMapping( + value = "/deleteArticlePublishedAfter/{date}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticlePublishedAfter( + @PathVariable("date") String date + ) { + return articleService.deleteArticlePublishedAfter(date); + } + + @DeleteMapping( + value = "/deleteArticlePublishedBetween/{beginning}/{end}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody Response deleteArticlePublishedBetween( + @PathVariable("beginning") String beginning, + @PathVariable("end") String end + ) { + return articleService.deleteArticlePublishedBetween(beginning, end); + } // endregion } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java index 550d3b0..41475a6 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java @@ -1,7 +1,7 @@ package SAE.ApiREST.WebService.model; - import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -12,32 +12,32 @@ import jakarta.persistence.Id; public class Article { @Id @GeneratedValue(strategy = GenerationType.AUTO) - String id; + Integer id; String title; - String URL; + String url; LocalDate dateAdded; LocalDate datePublished; - Boolean isVisible; + Boolean visible; Integer type; // ArrayList keywords = new ArrayList<>(); public Article() {} - public Article(String title, String URL, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) { - this.id = "1"; + public Article(String title, String url, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) { + this.id = 1; this.title = title; - this.URL = URL; + this.url = url; this.dateAdded = dateAdded; this.datePublished = datePublished; - this.isVisible = visibility; + this.visible = visibility; this.type = type; } - public String getId() { + public Integer getId() { return this.id; } - public void setId(String id) { + public void setId(Integer id) { this.id = id; } @@ -49,36 +49,36 @@ public class Article { this.title = title; } - public String getURL() { - return this.URL; + public String getUrl() { + return this.url; } - public void setURL(String URL) { - this.URL = URL; + public void setUrl(String url) { + this.url = url; } public LocalDate getDateAdded() { return this.dateAdded; } - public void setDateAdded(LocalDate dateAdded) { - this.dateAdded = dateAdded; + public void setDateAdded(String dateAdded) { + this.dateAdded = LocalDate.parse(dateAdded, DateTimeFormatter.ofPattern("dd-MM-yyyy")); } public LocalDate getDatePublished() { return this.datePublished; } - public void setDatePublished(LocalDate datePublished) { - this.datePublished = datePublished; + public void setDatePublished(String datePublished) { + this.datePublished = LocalDate.parse(datePublished, DateTimeFormatter.ofPattern("dd-MM-yyyy")); } - public Boolean isVisible() { - return this.isVisible; + public Boolean getVisible() { + return this.visible; } - public void setVisibility(Boolean isVisible) { - this.isVisible = isVisible; + public void setVisibility(Boolean visible) { + this.visible = visible; } public Integer getType() { diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java index 427d987..eaa12ff 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java @@ -2,10 +2,24 @@ package SAE.ApiREST.WebService.service; import java.util.List; +import SAE.ApiREST.WebService.Response; import SAE.ApiREST.WebService.model.Article; public interface IArticleService { + // region POST + public Response addArticle(Article article); + // endregion + + // region PUT + public Article updateTitle(Article article, String title); + public Article updateUrl(Article article, String url); + public Article updateDatePublished(Article article, String datePublished); + public Article updateDateAdded(Article article, String dateAdded); + public Article changeVisibility(Article article); + public Article updateType(Article article, Integer type); + // endregion + // region GET public List
getAllArticles(); Article getArticlesById(Integer id); @@ -21,4 +35,15 @@ public interface IArticleService { public List
getArticlesPublishedBetween(String beginning, String end); // endregion + // region DELETE + public Response deleteArticleFromId(Integer id); + public Response deleteArticleFromTitle(String title); + public Response deleteArticleFromUrl(String url); + public Response deleteArticleAddedBefore(String date); + public Response deleteArticleAddedAfter(String date); + public Response deleteArticleAddedBetween(String beginning, String end); + public Response deleteArticlePublishedBefore(String date); + public Response deleteArticlePublishedAfter(String date); + public Response deleteArticlePublishedBetween(String beginning, String end); + // endregion } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java index 2ea238a..026dcbc 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java @@ -7,11 +7,66 @@ import java.util.List; import org.springframework.stereotype.Service; +import SAE.ApiREST.WebService.Response; import SAE.ApiREST.WebService.model.Article; @Service public class StubArticleService implements IArticleService { + // region POST + @Override + public Response addArticle(Article article) { + return new Response( + article.getId(), + "Article successfully created" + ); + } + // endregion + + // region PUT + @Override + public Article updateTitle(Article article, String title) { + Article newArticle = article; + newArticle.setTitle(title); + return newArticle; + } + + @Override + public Article updateUrl(Article article, String url) { + Article newArticle = article; + newArticle.setUrl(url); + return newArticle; + } + + @Override + public Article updateDatePublished(Article article, String datePublished) { + Article newArticle = article; + newArticle.setDatePublished(datePublished); + return newArticle; + } + + @Override + public Article updateDateAdded(Article article, String dateAdded) { + Article newArticle = article; + newArticle.setDateAdded(dateAdded); + return newArticle; + } + + @Override + public Article changeVisibility(Article article) { + Article newArticle = article; + newArticle.setVisibility(!article.getVisible()); + return newArticle; + } + + @Override + public Article updateType(Article article, Integer type) { + Article newArticle = article; + newArticle.setType(type); + return newArticle; + } + // endregion + // region GET @Override public List
getAllArticles() { @@ -224,4 +279,78 @@ public class StubArticleService implements IArticleService { return articles; } // endregion -} + + // region DELETE + @Override + public Response deleteArticleFromId(Integer id) { + return new Response( + id, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticleFromUrl(String url) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticleFromTitle(String title) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticleAddedBefore(String url) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticleAddedAfter(String url) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticleAddedBetween(String beginning, String end) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticlePublishedBefore(String url) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticlePublishedAfter(String url) { + return new Response( + 1, + "Article successfully deleted" + ); + } + + @Override + public Response deleteArticlePublishedBetween(String beginning, String end) { + return new Response( + 1, + "Article successfully deleted" + ); + } + // endregion +} \ No newline at end of file From d57ed6e0202e07003169299dd3fe81743b53f450 Mon Sep 17 00:00:00 2001 From: Roxane Date: Tue, 27 Feb 2024 16:43:35 +0100 Subject: [PATCH 2/7] construct of Tearcher's server part finished --- WebService/pom.xml | 44 -------------- .../controller/TeacherController.java | 58 ++++++++++++++++--- .../WebService/exception/TeacherAdvice.java | 17 ++++++ .../exception/TeacherException.java | 7 +++ .../SAE/ApiREST/WebService/model/Article.java | 11 ++-- .../SAE/ApiREST/WebService/model/Teacher.java | 6 +- .../WebService/service/ITeacherService.java | 9 +-- .../service/TeacherServiceStub.java | 18 ++++-- 8 files changed, 98 insertions(+), 72 deletions(-) create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherAdvice.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherException.java diff --git a/WebService/pom.xml b/WebService/pom.xml index 0f7f3be..fb20a79 100644 --- a/WebService/pom.xml +++ b/WebService/pom.xml @@ -1,34 +1,5 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.2 - - - com.example - WebService - 0.0.1-SNAPSHOT - tp2 - Demo project for Spring Boot - - 17 - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-test - test - -======= xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -50,13 +21,11 @@ org.springframework.boot spring-boot-starter - org.springframework.boot spring-boot-starter-test test - org.springframework.boot spring-boot-starter-web @@ -66,18 +35,6 @@ jakarta.persistence-api ->>>>>>> origin/feature/response - - - org.springframework.boot - spring-boot-starter-web - - - jakarta.persistence - jakarta.persistence-api - - - @@ -86,5 +43,4 @@ - \ No newline at end of file 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 5d44e1b..9a39d55 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/TeacherController.java @@ -1,5 +1,7 @@ package SAE.ApiREST.WebService.controller; +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 org.springframework.beans.factory.annotation.Autowired; @@ -10,8 +12,10 @@ import org.springframework.web.bind.annotation.*; import javax.print.attribute.standard.Media; import java.awt.*; +import java.util.ArrayList; import java.util.List; +//Todo() Response = Type de retour pour toutes les méthodes qui ont void @Controller @RequestMapping("/ProfWebService") public class TeacherController { @@ -25,16 +29,56 @@ public class TeacherController { @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public List getAllTeacher(){ + public @ResponseBody List getAllTeacher(){ return iTeacherServ.getAllTeacher(); } - - @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) + @PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public Teacher createTeacher( @RequestBody Teacher teach){ + public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){ return teach; } - - //@GetMapping(value = "/{id}") - //public + @GetMapping(value = "/getid/{id}") + public @ResponseBody 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; + } + @GetMapping(value = "/getusername/{username}") + public @ResponseBody 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; + } + @GetMapping( value = "/getmail/{mail}" ) + public @ResponseBody 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; + } + @GetMapping( value = "/getdate/{date}" ) + public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) { + Teacher tt = iTeacherServ.getTeacherByMail(date); + if( tt == null ) { + throw new TeacherException("No teacher found for this mail"); + } + return tt; + } + @PutMapping( value = "/modify/{username}" ) + public @ResponseBody 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; + } + @DeleteMapping( value = "delete") + public @ResponseBody List deleteTeacher(Integer id){ + return iTeacherServ.deleteTeacher(id); + } } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherAdvice.java b/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherAdvice.java new file mode 100644 index 0000000..e781d8f --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherAdvice.java @@ -0,0 +1,17 @@ +package SAE.ApiREST.WebService.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice +public class TeacherAdvice { + @ResponseBody + @ExceptionHandler(TeacherException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public String teacherNFHandler( TeacherException e) { + return e.getMessage(); + } +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherException.java b/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherException.java new file mode 100644 index 0000000..ee61655 --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/exception/TeacherException.java @@ -0,0 +1,7 @@ +package SAE.ApiREST.WebService.exception; + +public class TeacherException extends RuntimeException { + public TeacherException(String exception) { + super(exception); + } +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java index dc3876f..9de95af 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Article.java @@ -12,11 +12,8 @@ import jakarta.persistence.Id; public class Article { @Id @GeneratedValue(strategy = GenerationType.AUTO) -<<<<<<< HEAD -======= - String id; ->>>>>>> origin/feature/response + Integer id; String title; String URL; LocalDate dateAdded; @@ -28,7 +25,7 @@ public class Article { public Article() {} public Article(String title, String URL, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) { - this.id = "1"; + this.id = 1; this.title = title; this.URL = URL; this.dateAdded = dateAdded; @@ -37,11 +34,11 @@ public class Article { this.type = type; } - public String getId() { + public Integer getId() { return this.id; } - public void setId(String id) { + public void setId(Integer id) { this.id = id; } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java index a121920..d153cd7 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Teacher.java @@ -21,7 +21,7 @@ public class Teacher { public Teacher(Integer id, String date, String mail, String username) { this.id = id; - this.date = LocalDate.parse(date, DateTimeFormatter.ISO_DATE); + this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")); this.mail = mail; this.username = username; } @@ -38,8 +38,8 @@ public class Teacher { return date; } - public void setDate(LocalDate date) { - this.date = date; + public void setDate(String date) { + this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")); } public String getMail() { diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/ITeacherService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/ITeacherService.java index 329a2de..e610d0e 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/ITeacherService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/ITeacherService.java @@ -1,21 +1,18 @@ package SAE.ApiREST.WebService.service; +import SAE.ApiREST.WebService.Response; import SAE.ApiREST.WebService.model.Teacher; import java.time.LocalDate; import java.util.List; public interface ITeacherService { - - //Todo() by id, by mail, by username, allProf, by date (order), suppression, ajout, FAIRE DES REGIONS! public List getAllTeacher(); - - Teacher getTeacherById(Integer id); - + public Teacher getTeacherById(Integer id); public Teacher getTeacherByUsername(String username); public Teacher getTeacherByMail(String mail); public Teacher getTeacherByDate(String date); public List addTeacher(Teacher t); - public List deleteTeacher(Integer id); + public Response modifyUsername(Teacher t, String newUsername); } 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 0b494f8..68f617f 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java @@ -1,5 +1,7 @@ package SAE.ApiREST.WebService.service; +import SAE.ApiREST.WebService.Response; +import SAE.ApiREST.WebService.exception.TeacherException; import SAE.ApiREST.WebService.model.Teacher; import org.springframework.stereotype.Service; @@ -31,9 +33,7 @@ public class TeacherServiceStub implements ITeacherService { } @Override - public Teacher getTeacherByUsername(String username) { - return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username); - } + public Teacher getTeacherByUsername(String username) { return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username); } @Override public Teacher getTeacherByMail(String mail) { @@ -59,7 +59,15 @@ public class TeacherServiceStub implements ITeacherService { allTeacher.add(new Teacher(1,"12-01-2023", "aline.alipres@gmail.com", "MsGarconManque")); allTeacher.add(new Teacher(2, "20-08-2023", "Viviane.Delvecchio@gmail.com", "MmeMath")); - allTeacher.remove(getTeacherById(id)); - return allTeacher; + if(allTeacher.remove(getTeacherById(id))){ + return allTeacher; + } else { + throw new TeacherException(String.format("Teacher {id} isn't removed", id)); + } + } + + public Response modifyUsername(Teacher t, String newUsername){ + t.setUsername(newUsername); + return new Response(t.getId(),String.format("This user %s has changed username", t.getMail())); } } From 6a7968d2b5546a67b29af018f85b5a063188413c Mon Sep 17 00:00:00 2001 From: Roxane Date: Tue, 27 Feb 2024 17:31:18 +0100 Subject: [PATCH 3/7] adding Hateos on the Teacher part --- WebService/pom.xml | 4 ++ .../controller/TeacherController.java | 50 +++++++++++++------ .../service/TeacherServiceStub.java | 1 + 3 files changed, 40 insertions(+), 15 deletions(-) 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"); From fce7c6d7e90446b43caece0eb397b6a5aea2e80e Mon Sep 17 00:00:00 2001 From: felix Date: Wed, 28 Feb 2024 11:42:04 +0100 Subject: [PATCH 4/7] Implementing Hateos --- WebService/pom.xml | 96 ++--- .../controller/ArticleControler.java | 340 ++++++++++++++---- .../WebService/service/IArticleService.java | 2 + .../service/StubArticleService.java | 24 ++ 4 files changed, 351 insertions(+), 111 deletions(-) diff --git a/WebService/pom.xml b/WebService/pom.xml index de41c7d..23c71db 100644 --- a/WebService/pom.xml +++ b/WebService/pom.xml @@ -1,50 +1,50 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.2 - - - com.example - WebService - 0.0.1-SNAPSHOT - tp2 - Demo project for Spring Boot - - 17 - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.springframework.boot - spring-boot-starter-web - - - jakarta.persistence - jakarta.persistence-api - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.2 + + + com.example + WebService + 0.0.1-SNAPSHOT + tp2 + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.springframework.boot + spring-boot-starter-web + + + jakarta.persistence + jakarta.persistence-api + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java index 222846c..6f524c7 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java @@ -1,6 +1,7 @@ package SAE.ApiREST.WebService.controller; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.EntityModel; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -13,6 +14,9 @@ import SAE.ApiREST.WebService.service.IArticleService; import java.util.ArrayList; import java.util.List; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; + @Controller @RequestMapping("/ArticleWebService") public class ArticleControler { @@ -25,10 +29,18 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response addArticle( + public @ResponseBody EntityModel addArticle( @RequestBody Article article ) { - return articleService.addArticle(article); + Response res = articleService.addArticle(article); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).addArticle(article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } // endregion @@ -38,11 +50,20 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article updateTitle( + public @ResponseBody EntityModel
updateTitle( @PathVariable("title") String title, @RequestBody Article article ) { - return articleService.updateTitle(article, title); + Article res = articleService.updateTitle(article, title); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).updateTitle(title, article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getArticlesByTitle(article.getTitle())).withRel("getArticlesByTitle"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } @PutMapping( @@ -50,11 +71,20 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article updateUrl( + public @ResponseBody EntityModel
updateUrl( @PathVariable("url") String url, @RequestBody Article article ) { - return articleService.updateUrl(article, url); + Article res = articleService.updateUrl(article, url); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).updateUrl(url, article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getArticlesByUrl(article.getUrl())).withRel("getArticlesByUrl"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } @PutMapping( @@ -62,11 +92,21 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article updateDatePublished( + public @ResponseBody EntityModel
updateDatePublished( @PathVariable("datePublished") String datePublished, @RequestBody Article article ) { - return articleService.updateDatePublished(article, datePublished); + Article res = articleService.updateDatePublished(article, datePublished); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).updateDatePublished(datePublished, article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getArticlesPublishedBefore(article.getDatePublished().toString())).withRel("getArticlesPublishedBefore"), + linkTo(methodOn(ArticleControler.class).getArticlesPublishedAfter(article.getDatePublished().toString())).withRel("getArticlesPublishedAfter"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } @PutMapping( @@ -74,11 +114,21 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article updateDateAdded( + public @ResponseBody EntityModel
updateDateAdded( @PathVariable("dateAdded") String dateAdded, @RequestBody Article article ) { - return articleService.updateDateAdded(article, dateAdded); + Article res = articleService.updateDateAdded(article, dateAdded); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).updateDateAdded(dateAdded, article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getArticlesAddedBefore(article.getDateAdded().toString())).withRel("getArticlesAddedBefore"), + linkTo(methodOn(ArticleControler.class).getArticlesAddedAfter(article.getDateAdded().toString())).withRel("getArticlesAddedAfter"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } @PutMapping( @@ -86,10 +136,20 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article changeVisibility( + public @ResponseBody EntityModel
changeVisibility( @RequestBody Article article ) { - return articleService.changeVisibility(article); + Article res = articleService.changeVisibility(article); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).changeVisibility(article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getVisibleArticles()).withRel("getVisibleArticle"), + linkTo(methodOn(ArticleControler.class).getInvisibleArticles()).withRel("getInvisibleArticle"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } @PutMapping( @@ -97,142 +157,214 @@ public class ArticleControler { consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Article updateType( + public @ResponseBody EntityModel
updateType( @PathVariable("type") Integer type, @RequestBody Article article ) { - return articleService.updateType(article, type); + Article res = articleService.updateType(article, type); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).updateType(type, article)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), + linkTo(methodOn(ArticleControler.class).getArticlesByType(article.getType())).withRel("getArticlesByType"), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle") + ); } // endregion // region GET @GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getAllArticles() { + public @ResponseBody EntityModel> getAllArticles() { ArrayList
results = (ArrayList
) articleService.getAllArticles(); if(results.isEmpty()) { throw new ArticleException("No articles available"); } - return results; + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getAllArticles()).withSelfRel() + ); } @GetMapping(value = "/getArticleById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody Article getArticlesById(@PathVariable(value = "id") Integer id) { + public @ResponseBody EntityModel
getArticleById(@PathVariable(value = "id") Integer id) { Article results = articleService.getArticlesById(id); if(results == null) { throw new ArticleException("Undefined id"); } - return results; + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticleById(id)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(id)).withRel("deleteArticleFromUrl") + ); } @GetMapping(value = "/getArticlesByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesByTitle(@PathVariable(value = "title") String title) { + public @ResponseBody EntityModel> getArticlesByTitle(@PathVariable(value = "title") String title) { ArrayList
results = (ArrayList
) articleService.getArticlesByTitle(title); if(results.isEmpty()) { throw new ArticleException("Undefined title"); } - return results; + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesByTitle(title)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleFromTitle(title)).withRel("deleteArticleFromTitle") + ); + } + + @GetMapping(value = "/getArticlesByUrl/{url}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody EntityModel> getArticlesByUrl(@PathVariable(value = "url") String url) { + ArrayList
results = (ArrayList
) articleService.getArticlesByUrl(url); + + if(results.isEmpty()) { + throw new ArticleException("Undefined title"); + } + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesByUrl(url)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleFromUrl(url)).withRel("deleteArticleFromId") + ); } @GetMapping(value = "/getArticlesByType/{type}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesByType(@PathVariable(value = "type") Integer type) { + public @ResponseBody EntityModel> getArticlesByType(@PathVariable(value = "type") Integer type) { ArrayList
results = (ArrayList
) articleService.getArticlesByType(type); if (results.isEmpty()) { throw new ArticleException(String.format("No content of type %d", type)); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesByType(type)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleFromType(type)).withRel("deleteArticleFromType") + ); } @GetMapping(value = "/getVisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getVisibleArticles() { + public @ResponseBody EntityModel> getVisibleArticles() { ArrayList
results = (ArrayList
) articleService.getVisibleArticles(); if (results.isEmpty()) { throw new ArticleException("No visible article"); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getVisibleArticles()).withSelfRel() + ); } @GetMapping(value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getInvisibleArticles() { + public @ResponseBody EntityModel> getInvisibleArticles() { ArrayList
results = (ArrayList
) articleService.getInvisibleArticles(); if (results.isEmpty()) { throw new ArticleException("No invisible article"); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getInvisibleArticles()).withSelfRel() + ); } @GetMapping(value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { + public @ResponseBody EntityModel> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBefore(dateAdded); if (results.isEmpty()) { throw new ArticleException(String.format("No article added before %t", dateAdded)); } - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesAddedBefore(dateAdded)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleAddedBefore(dateAdded)).withRel("deleteArticleAddedBefore") + ); } @GetMapping(value = "/getArticlesAddedAfter/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { + public @ResponseBody EntityModel> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedAfter(dateAdded); if (results.isEmpty()) { throw new ArticleException(String.format("No article added after %t", dateAdded)); } - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesAddedAfter(dateAdded)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleAddedAfter(dateAdded)).withRel("deleteArticleAddedAfter") + ); } @GetMapping(value = "/getArticlesAddedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesAddedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { + public @ResponseBody EntityModel> getArticlesAddedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBetween(beginning, end); if (results.isEmpty()) { throw new ArticleException(String.format("No article added between %t and %t", beginning, end)); } - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesAddedBetween(beginning, end)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticleAddedBetween(beginning, end)).withRel("deleteArticleAddedBetween") + ); } @GetMapping(value = "/getArticlesPublishedBefore/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { + public @ResponseBody EntityModel> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBefore(datePublished); if (results.isEmpty()) { throw new ArticleException(String.format("No article published before %t", datePublished)); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesPublishedBefore(datePublished)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBefore(datePublished)).withRel("deleteArticlePublishedBefore") + ); } @GetMapping(value = "/getArticlesPublishedAfter/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { + public @ResponseBody EntityModel> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedAfter(datePublished); if (results.isEmpty()) { throw new ArticleException(String.format("No article published after %t", datePublished)); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesPublishedAfter(datePublished)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedAfter(datePublished)).withRel("deleteArticlePublishedAfter") + ); } @GetMapping(value = "/getArticlesPublishedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List
getArticlesPublishedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { + public @ResponseBody EntityModel> getArticlesPublishedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBetween(beginning, end); if (results.isEmpty()) { throw new ArticleException(String.format("No article published between %t and %t", beginning, end)); } - - return results; + + return EntityModel.of( + results, + linkTo(methodOn(ArticleControler.class).getArticlesPublishedBetween(beginning, end)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBetween(beginning, end)).withRel("getArticlesPublishedBetween") + ); } // endregion @@ -242,90 +374,172 @@ public class ArticleControler { value = "/deleteArticleFromId/{id}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleFromId( + public @ResponseBody EntityModel deleteArticleFromId( @PathVariable("id") Integer id ) { - return articleService.deleteArticleFromId(id); + Response res = articleService.deleteArticleFromId(id); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleFromId(id)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticleById(id)).withRel("getArticleById") + ); } @DeleteMapping( value = "/deleteArticleFromTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleFromTitle( + public @ResponseBody EntityModel deleteArticleFromTitle( @PathVariable("title") String title ) { - return articleService.deleteArticleFromTitle(title); + Response res = articleService.deleteArticleFromTitle(title); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleFromTitle(title)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesByTitle(title)).withRel("getArticleByTitle") + ); } @DeleteMapping( value = "/deleteArticleFromUrl/{url}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleFromUrl( + public @ResponseBody EntityModel deleteArticleFromUrl( @PathVariable("url") String url ) { - return articleService.deleteArticleFromUrl(url); + Response res = articleService.deleteArticleFromTitle(url); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleFromUrl(url)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesByUrl(url)).withRel("getArticlesByUrl") + ); + } + + @DeleteMapping( + value = "/deleteArticleFromType/{type}", + produces = MediaType.APPLICATION_JSON_VALUE + ) + public @ResponseBody EntityModel deleteArticleFromType( + @PathVariable("type") Integer type + ) { + Response res = articleService.deleteArticleFromType(type); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleFromType(type)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesByType(type)).withRel("getArticlesByType") + ); } @DeleteMapping( value = "/deleteArticleAddedBefore/{date}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleAddedBefore( + public @ResponseBody EntityModel deleteArticleAddedBefore( @PathVariable("date") String date ) { - return articleService.deleteArticleAddedBefore(date); + Response res = articleService.deleteArticleAddedBefore(date); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleAddedBefore(date)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesAddedBefore(date)).withRel("getArticlesAddedBefore") + ); } @DeleteMapping( value = "/deleteArticleAddedAfter/{date}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleAddedAfter( + public @ResponseBody EntityModel deleteArticleAddedAfter( @PathVariable("date") String date ) { - return articleService.deleteArticleAddedAfter(date); - }@DeleteMapping( + Response res = articleService.deleteArticleAddedAfter(date); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleAddedAfter(date)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesAddedAfter(date)).withRel("getArticlesAddedAfter") + ); + } + + @DeleteMapping( value = "/deleteArticleAddedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticleAddedBetween( + public @ResponseBody EntityModel deleteArticleAddedBetween( @PathVariable("beginning") String beginning, @PathVariable("end") String end ) { - return articleService.deleteArticleAddedBetween(beginning, end); + Response res = articleService.deleteArticleAddedBetween(beginning, end); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticleAddedBetween(beginning, end)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesAddedBetween(beginning, end)).withRel("getArticlesAddedBetween") + ); } @DeleteMapping( value = "/deleteArticlePublishedBefore/{date}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticlePublishedBefore( + public @ResponseBody EntityModel deleteArticlePublishedBefore( @PathVariable("date") String date ) { - return articleService.deleteArticlePublishedBefore(date); + Response res = articleService.deleteArticlePublishedBefore(date); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBefore(date)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesPublishedBefore(date)).withRel("getArticlesPublishedBefore") + ); } @DeleteMapping( value = "/deleteArticlePublishedAfter/{date}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticlePublishedAfter( + public @ResponseBody EntityModel deleteArticlePublishedAfter( @PathVariable("date") String date ) { - return articleService.deleteArticlePublishedAfter(date); + Response res = articleService.deleteArticlePublishedBefore(date); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedAfter(date)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesPublishedAfter(date)).withRel("getArticlesPublishedAfter") + ); } @DeleteMapping( value = "/deleteArticlePublishedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody Response deleteArticlePublishedBetween( + public @ResponseBody EntityModel deleteArticlePublishedBetween( @PathVariable("beginning") String beginning, @PathVariable("end") String end ) { - return articleService.deleteArticlePublishedBetween(beginning, end); + Response res = articleService.deleteArticlePublishedBetween(beginning, end); + + return EntityModel.of( + res, + linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBetween(beginning, end)).withSelfRel(), + linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), + linkTo(methodOn(ArticleControler.class).getArticlesPublishedBetween(beginning, end)).withRel("getArticlesPublishedBetween") + ); } // endregion } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java index eaa12ff..38d0f82 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/IArticleService.java @@ -24,6 +24,7 @@ public interface IArticleService { public List
getAllArticles(); Article getArticlesById(Integer id); public List
getArticlesByTitle(String title); + public List
getArticlesByUrl(String url); public List
getArticlesByType(Integer type); public List
getVisibleArticles(); public List
getInvisibleArticles(); @@ -39,6 +40,7 @@ public interface IArticleService { public Response deleteArticleFromId(Integer id); public Response deleteArticleFromTitle(String title); public Response deleteArticleFromUrl(String url); + public Response deleteArticleFromType(Integer type); public Response deleteArticleAddedBefore(String date); public Response deleteArticleAddedAfter(String date); public Response deleteArticleAddedBetween(String beginning, String end); diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java index 026dcbc..9c17905 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubArticleService.java @@ -135,6 +135,22 @@ public class StubArticleService implements IArticleService { return articles; } + @Override + public List
getArticlesByUrl(String url) { + List
articles = new ArrayList<>(); + + articles.add(new Article( + "title", + url, + LocalDate.now().minusMonths(1), + LocalDate.now().minusMonths(2), + true, + 1) + ); + + return articles; + } + @Override public List
getArticlesByType(Integer type) { List
articles = new ArrayList<>(); @@ -296,6 +312,14 @@ public class StubArticleService implements IArticleService { "Article successfully deleted" ); } + + @Override + public Response deleteArticleFromType(Integer type) { + return new Response( + 1, + "Article successfully deleted" + ); + } @Override public Response deleteArticleFromTitle(String title) { From 1cd2e8d71450b25c3440f2ee5b112dc2558fe5ed Mon Sep 17 00:00:00 2001 From: felix Date: Wed, 28 Feb 2024 12:03:27 +0100 Subject: [PATCH 5/7] mise en forme --- .../controller/ArticleControler.java | 133 +++++++++++------- 1 file changed, 86 insertions(+), 47 deletions(-) diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java index 6f524c7..e914def 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java @@ -32,10 +32,10 @@ public class ArticleControler { public @ResponseBody EntityModel addArticle( @RequestBody Article article ) { - Response res = articleService.addArticle(article); + Response results = articleService.addArticle(article); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).addArticle(article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -54,10 +54,10 @@ public class ArticleControler { @PathVariable("title") String title, @RequestBody Article article ) { - Article res = articleService.updateTitle(article, title); + Article results = articleService.updateTitle(article, title); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).updateTitle(title, article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -75,10 +75,10 @@ public class ArticleControler { @PathVariable("url") String url, @RequestBody Article article ) { - Article res = articleService.updateUrl(article, url); + Article results = articleService.updateUrl(article, url); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).updateUrl(url, article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -96,10 +96,10 @@ public class ArticleControler { @PathVariable("datePublished") String datePublished, @RequestBody Article article ) { - Article res = articleService.updateDatePublished(article, datePublished); + Article results = articleService.updateDatePublished(article, datePublished); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).updateDatePublished(datePublished, article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -118,10 +118,10 @@ public class ArticleControler { @PathVariable("dateAdded") String dateAdded, @RequestBody Article article ) { - Article res = articleService.updateDateAdded(article, dateAdded); + Article results = articleService.updateDateAdded(article, dateAdded); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).updateDateAdded(dateAdded, article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -139,10 +139,10 @@ public class ArticleControler { public @ResponseBody EntityModel
changeVisibility( @RequestBody Article article ) { - Article res = articleService.changeVisibility(article); + Article results = articleService.changeVisibility(article); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).changeVisibility(article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -161,10 +161,10 @@ public class ArticleControler { @PathVariable("type") Integer type, @RequestBody Article article ) { - Article res = articleService.updateType(article, type); + Article results = articleService.updateType(article, type); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).updateType(type, article)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("allArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(article.getId())).withRel("getArticleById"), @@ -175,7 +175,10 @@ public class ArticleControler { // endregion // region GET - @GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getAllArticle", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getAllArticles() { ArrayList
results = (ArrayList
) articleService.getAllArticles(); @@ -189,7 +192,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticleById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticleById/{id}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel
getArticleById(@PathVariable(value = "id") Integer id) { Article results = articleService.getArticlesById(id); @@ -204,7 +210,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesByTitle/{title}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesByTitle(@PathVariable(value = "title") String title) { ArrayList
results = (ArrayList
) articleService.getArticlesByTitle(title); @@ -219,7 +228,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesByUrl/{url}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesByUrl/{url}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesByUrl(@PathVariable(value = "url") String url) { ArrayList
results = (ArrayList
) articleService.getArticlesByUrl(url); @@ -234,7 +246,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesByType/{type}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesByType/{type}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesByType(@PathVariable(value = "type") Integer type) { ArrayList
results = (ArrayList
) articleService.getArticlesByType(type); @@ -249,7 +264,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getVisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getVisibleArticles", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getVisibleArticles() { ArrayList
results = (ArrayList
) articleService.getVisibleArticles(); @@ -263,7 +281,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getInvisibleArticles", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getInvisibleArticles() { ArrayList
results = (ArrayList
) articleService.getInvisibleArticles(); @@ -277,7 +298,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesAddedBefore/{dateAdded}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBefore(dateAdded); @@ -292,7 +316,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesAddedAfter/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesAddedAfter/{dateAdded}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedAfter(dateAdded); @@ -307,7 +334,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesAddedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesAddedBetween/{beginning}/{end}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesAddedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBetween(beginning, end); @@ -322,7 +352,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesPublishedBefore/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesPublishedBefore/{datePublished}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBefore(datePublished); @@ -337,7 +370,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesPublishedAfter/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesPublishedAfter/{datePublished}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedAfter(datePublished); @@ -352,7 +388,10 @@ public class ArticleControler { ); } - @GetMapping(value = "/getArticlesPublishedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE) + @GetMapping( + value = "/getArticlesPublishedBetween/{beginning}/{end}", + produces = MediaType.APPLICATION_JSON_VALUE + ) public @ResponseBody EntityModel> getArticlesPublishedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBetween(beginning, end); @@ -377,10 +416,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleFromId( @PathVariable("id") Integer id ) { - Response res = articleService.deleteArticleFromId(id); + Response results = articleService.deleteArticleFromId(id); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleFromId(id)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticleById(id)).withRel("getArticleById") @@ -394,10 +433,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleFromTitle( @PathVariable("title") String title ) { - Response res = articleService.deleteArticleFromTitle(title); + Response results = articleService.deleteArticleFromTitle(title); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleFromTitle(title)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesByTitle(title)).withRel("getArticleByTitle") @@ -411,10 +450,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleFromUrl( @PathVariable("url") String url ) { - Response res = articleService.deleteArticleFromTitle(url); + Response results = articleService.deleteArticleFromTitle(url); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleFromUrl(url)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesByUrl(url)).withRel("getArticlesByUrl") @@ -428,10 +467,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleFromType( @PathVariable("type") Integer type ) { - Response res = articleService.deleteArticleFromType(type); + Response results = articleService.deleteArticleFromType(type); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleFromType(type)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesByType(type)).withRel("getArticlesByType") @@ -445,10 +484,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleAddedBefore( @PathVariable("date") String date ) { - Response res = articleService.deleteArticleAddedBefore(date); + Response results = articleService.deleteArticleAddedBefore(date); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleAddedBefore(date)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesAddedBefore(date)).withRel("getArticlesAddedBefore") @@ -462,10 +501,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticleAddedAfter( @PathVariable("date") String date ) { - Response res = articleService.deleteArticleAddedAfter(date); + Response results = articleService.deleteArticleAddedAfter(date); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleAddedAfter(date)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesAddedAfter(date)).withRel("getArticlesAddedAfter") @@ -480,10 +519,10 @@ public class ArticleControler { @PathVariable("beginning") String beginning, @PathVariable("end") String end ) { - Response res = articleService.deleteArticleAddedBetween(beginning, end); + Response results = articleService.deleteArticleAddedBetween(beginning, end); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticleAddedBetween(beginning, end)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesAddedBetween(beginning, end)).withRel("getArticlesAddedBetween") @@ -497,10 +536,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticlePublishedBefore( @PathVariable("date") String date ) { - Response res = articleService.deleteArticlePublishedBefore(date); + Response results = articleService.deleteArticlePublishedBefore(date); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBefore(date)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesPublishedBefore(date)).withRel("getArticlesPublishedBefore") @@ -514,10 +553,10 @@ public class ArticleControler { public @ResponseBody EntityModel deleteArticlePublishedAfter( @PathVariable("date") String date ) { - Response res = articleService.deleteArticlePublishedBefore(date); + Response results = articleService.deleteArticlePublishedBefore(date); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticlePublishedAfter(date)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesPublishedAfter(date)).withRel("getArticlesPublishedAfter") @@ -532,10 +571,10 @@ public class ArticleControler { @PathVariable("beginning") String beginning, @PathVariable("end") String end ) { - Response res = articleService.deleteArticlePublishedBetween(beginning, end); + Response results = articleService.deleteArticlePublishedBetween(beginning, end); return EntityModel.of( - res, + results, linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBetween(beginning, end)).withSelfRel(), linkTo(methodOn(ArticleControler.class).getAllArticles()).withRel("getAllArticles"), linkTo(methodOn(ArticleControler.class).getArticlesPublishedBetween(beginning, end)).withRel("getArticlesPublishedBetween") From 630a1c0813cdd53590b724ceee75ddd8ebbf2052 Mon Sep 17 00:00:00 2001 From: felix Date: Wed, 28 Feb 2024 15:45:45 +0100 Subject: [PATCH 6/7] fix EntityModel> to CollectionModel<> --- .../controller/ArticleControler.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java index e914def..7c86921 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/ArticleControler.java @@ -1,6 +1,7 @@ package SAE.ApiREST.WebService.controller; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; @@ -12,7 +13,6 @@ import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.service.IArticleService; import java.util.ArrayList; -import java.util.List; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; @@ -179,14 +179,14 @@ public class ArticleControler { value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getAllArticles() { + public @ResponseBody CollectionModel
getAllArticles() { ArrayList
results = (ArrayList
) articleService.getAllArticles(); if(results.isEmpty()) { throw new ArticleException("No articles available"); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getAllArticles()).withSelfRel() ); @@ -214,14 +214,14 @@ public class ArticleControler { value = "/getArticlesByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesByTitle(@PathVariable(value = "title") String title) { + public @ResponseBody CollectionModel
getArticlesByTitle(@PathVariable(value = "title") String title) { ArrayList
results = (ArrayList
) articleService.getArticlesByTitle(title); if(results.isEmpty()) { throw new ArticleException("Undefined title"); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesByTitle(title)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleFromTitle(title)).withRel("deleteArticleFromTitle") @@ -232,14 +232,14 @@ public class ArticleControler { value = "/getArticlesByUrl/{url}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesByUrl(@PathVariable(value = "url") String url) { + public @ResponseBody CollectionModel
getArticlesByUrl(@PathVariable(value = "url") String url) { ArrayList
results = (ArrayList
) articleService.getArticlesByUrl(url); if(results.isEmpty()) { throw new ArticleException("Undefined title"); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesByUrl(url)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleFromUrl(url)).withRel("deleteArticleFromId") @@ -250,14 +250,14 @@ public class ArticleControler { value = "/getArticlesByType/{type}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesByType(@PathVariable(value = "type") Integer type) { + public @ResponseBody CollectionModel
getArticlesByType(@PathVariable(value = "type") Integer type) { ArrayList
results = (ArrayList
) articleService.getArticlesByType(type); if (results.isEmpty()) { throw new ArticleException(String.format("No content of type %d", type)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesByType(type)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleFromType(type)).withRel("deleteArticleFromType") @@ -268,14 +268,14 @@ public class ArticleControler { value = "/getVisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getVisibleArticles() { + public @ResponseBody CollectionModel
getVisibleArticles() { ArrayList
results = (ArrayList
) articleService.getVisibleArticles(); if (results.isEmpty()) { throw new ArticleException("No visible article"); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getVisibleArticles()).withSelfRel() ); @@ -285,14 +285,14 @@ public class ArticleControler { value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getInvisibleArticles() { + public @ResponseBody CollectionModel
getInvisibleArticles() { ArrayList
results = (ArrayList
) articleService.getInvisibleArticles(); if (results.isEmpty()) { throw new ArticleException("No invisible article"); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getInvisibleArticles()).withSelfRel() ); @@ -302,14 +302,14 @@ public class ArticleControler { value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { + public @ResponseBody CollectionModel
getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBefore(dateAdded); if (results.isEmpty()) { throw new ArticleException(String.format("No article added before %t", dateAdded)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesAddedBefore(dateAdded)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleAddedBefore(dateAdded)).withRel("deleteArticleAddedBefore") @@ -320,14 +320,14 @@ public class ArticleControler { value = "/getArticlesAddedAfter/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { + public @ResponseBody CollectionModel
getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedAfter(dateAdded); if (results.isEmpty()) { throw new ArticleException(String.format("No article added after %t", dateAdded)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesAddedAfter(dateAdded)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleAddedAfter(dateAdded)).withRel("deleteArticleAddedAfter") @@ -338,14 +338,14 @@ public class ArticleControler { value = "/getArticlesAddedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesAddedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { + public @ResponseBody CollectionModel
getArticlesAddedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesAddedBetween(beginning, end); if (results.isEmpty()) { throw new ArticleException(String.format("No article added between %t and %t", beginning, end)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesAddedBetween(beginning, end)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticleAddedBetween(beginning, end)).withRel("deleteArticleAddedBetween") @@ -356,14 +356,14 @@ public class ArticleControler { value = "/getArticlesPublishedBefore/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { + public @ResponseBody CollectionModel
getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBefore(datePublished); if (results.isEmpty()) { throw new ArticleException(String.format("No article published before %t", datePublished)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesPublishedBefore(datePublished)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBefore(datePublished)).withRel("deleteArticlePublishedBefore") @@ -374,14 +374,14 @@ public class ArticleControler { value = "/getArticlesPublishedAfter/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { + public @ResponseBody CollectionModel
getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedAfter(datePublished); if (results.isEmpty()) { throw new ArticleException(String.format("No article published after %t", datePublished)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesPublishedAfter(datePublished)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticlePublishedAfter(datePublished)).withRel("deleteArticlePublishedAfter") @@ -392,14 +392,14 @@ public class ArticleControler { value = "/getArticlesPublishedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE ) - public @ResponseBody EntityModel> getArticlesPublishedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { + public @ResponseBody CollectionModel
getArticlesPublishedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { ArrayList
results = (ArrayList
) articleService.getArticlesPublishedBetween(beginning, end); if (results.isEmpty()) { throw new ArticleException(String.format("No article published between %t and %t", beginning, end)); } - return EntityModel.of( + return CollectionModel.of( results, linkTo(methodOn(ArticleControler.class).getArticlesPublishedBetween(beginning, end)).withSelfRel(), linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBetween(beginning, end)).withRel("getArticlesPublishedBetween") From f1340fe6a748dc1c6db7f104a39e09059ab6fa3f Mon Sep 17 00:00:00 2001 From: Roxane Date: Fri, 1 Mar 2024 10:14:13 +0100 Subject: [PATCH 7/7] Back to the version without repository --- .../java/SAE/ApiREST/WebService/service/TeacherServiceStub.java | 2 -- 1 file changed, 2 deletions(-) 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 a4e9f51..43e5923 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/TeacherServiceStub.java @@ -14,7 +14,6 @@ import java.util.List; @Service public class TeacherServiceStub implements ITeacherService { - //todo() recevoir collections, ajouter collections, supprimer collections @Override public List getAllTeacher() { List allTeacher = new ArrayList(); @@ -40,7 +39,6 @@ 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");