Implementing Hateos

feature/Article
felix 1 year ago
parent dbad7174ec
commit fce7c6d7e9

@ -1,50 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version> <version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.example</groupId> <groupId>com.example</groupId>
<artifactId>WebService</artifactId> <artifactId>WebService</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>tp2</name> <name>tp2</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<dependency> <groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
<scope>test</scope> </dependency>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<dependency> <artifactId>spring-boot-starter-hateoas</artifactId>
<groupId>org.springframework.boot</groupId> </dependency>
<artifactId>spring-boot-starter-web</artifactId> <dependency>
</dependency> <groupId>org.springframework.boot</groupId>
<dependency> <artifactId>spring-boot-starter-web</artifactId>
<groupId>jakarta.persistence</groupId> </dependency>
<artifactId>jakarta.persistence-api</artifactId> <dependency>
</dependency> <groupId>jakarta.persistence</groupId>
</dependencies> <artifactId>jakarta.persistence-api</artifactId>
</dependency>
<build> </dependencies>
<plugins> <build>
<plugin> <plugins>
<groupId>org.springframework.boot</groupId> <plugin>
<artifactId>spring-boot-maven-plugin</artifactId> <groupId>org.springframework.boot</groupId>
</plugin> <artifactId>spring-boot-maven-plugin</artifactId>
</plugins> </plugin>
</build> </plugins>
</build>
</project> </project>

@ -1,6 +1,7 @@
package SAE.ApiREST.WebService.controller; package SAE.ApiREST.WebService.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -13,6 +14,9 @@ import SAE.ApiREST.WebService.service.IArticleService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller @Controller
@RequestMapping("/ArticleWebService") @RequestMapping("/ArticleWebService")
public class ArticleControler { public class ArticleControler {
@ -25,10 +29,18 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response addArticle( public @ResponseBody EntityModel<Response> addArticle(
@RequestBody Article article @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 // endregion
@ -38,11 +50,20 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article updateTitle( public @ResponseBody EntityModel<Article> updateTitle(
@PathVariable("title") String title, @PathVariable("title") String title,
@RequestBody Article article @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( @PutMapping(
@ -50,11 +71,20 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article updateUrl( public @ResponseBody EntityModel<Article> updateUrl(
@PathVariable("url") String url, @PathVariable("url") String url,
@RequestBody Article article @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( @PutMapping(
@ -62,11 +92,21 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article updateDatePublished( public @ResponseBody EntityModel<Article> updateDatePublished(
@PathVariable("datePublished") String datePublished, @PathVariable("datePublished") String datePublished,
@RequestBody Article article @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( @PutMapping(
@ -74,11 +114,21 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article updateDateAdded( public @ResponseBody EntityModel<Article> updateDateAdded(
@PathVariable("dateAdded") String dateAdded, @PathVariable("dateAdded") String dateAdded,
@RequestBody Article article @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( @PutMapping(
@ -86,10 +136,20 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article changeVisibility( public @ResponseBody EntityModel<Article> changeVisibility(
@RequestBody Article article @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( @PutMapping(
@ -97,142 +157,214 @@ public class ArticleControler {
consumes = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Article updateType( public @ResponseBody EntityModel<Article> updateType(
@PathVariable("type") Integer type, @PathVariable("type") Integer type,
@RequestBody Article article @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 // endregion
// region GET // region GET
@GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getAllArticles() { public @ResponseBody EntityModel<List<Article>> getAllArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getAllArticles(); ArrayList<Article> results = (ArrayList<Article>) articleService.getAllArticles();
if(results.isEmpty()) { if(results.isEmpty()) {
throw new ArticleException("No articles available"); 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) @GetMapping(value = "/getArticleById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Article getArticlesById(@PathVariable(value = "id") Integer id) { public @ResponseBody EntityModel<Article> getArticleById(@PathVariable(value = "id") Integer id) {
Article results = articleService.getArticlesById(id); Article results = articleService.getArticlesById(id);
if(results == null) { if(results == null) {
throw new ArticleException("Undefined id"); 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) @GetMapping(value = "/getArticlesByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesByTitle(@PathVariable(value = "title") String title) { public @ResponseBody EntityModel<List<Article>> getArticlesByTitle(@PathVariable(value = "title") String title) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByTitle(title); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByTitle(title);
if(results.isEmpty()) { if(results.isEmpty()) {
throw new ArticleException("Undefined title"); 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<List<Article>> getArticlesByUrl(@PathVariable(value = "url") String url) {
ArrayList<Article> results = (ArrayList<Article>) 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) @GetMapping(value = "/getArticlesByType/{type}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesByType(@PathVariable(value = "type") Integer type) { public @ResponseBody EntityModel<List<Article>> getArticlesByType(@PathVariable(value = "type") Integer type) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByType(type); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByType(type);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No content of type %d", type)); 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) @GetMapping(value = "/getVisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getVisibleArticles() { public @ResponseBody EntityModel<List<Article>> getVisibleArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getVisibleArticles(); ArrayList<Article> results = (ArrayList<Article>) articleService.getVisibleArticles();
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException("No visible article"); 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) @GetMapping(value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getInvisibleArticles() { public @ResponseBody EntityModel<List<Article>> getInvisibleArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getInvisibleArticles(); ArrayList<Article> results = (ArrayList<Article>) articleService.getInvisibleArticles();
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException("No invisible article"); 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) @GetMapping(value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { public @ResponseBody EntityModel<List<Article>> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBefore(dateAdded); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBefore(dateAdded);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article added before %t", dateAdded)); 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) @GetMapping(value = "/getArticlesAddedAfter/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { public @ResponseBody EntityModel<List<Article>> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedAfter(dateAdded); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedAfter(dateAdded);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article added after %t", dateAdded)); 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) @GetMapping(value = "/getArticlesAddedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesAddedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { public @ResponseBody EntityModel<List<Article>> getArticlesAddedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBetween(beginning, end); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBetween(beginning, end);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article added between %t and %t", beginning, end)); 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) @GetMapping(value = "/getArticlesPublishedBefore/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { public @ResponseBody EntityModel<List<Article>> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBefore(datePublished); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBefore(datePublished);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article published before %t", datePublished)); 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) @GetMapping(value = "/getArticlesPublishedAfter/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { public @ResponseBody EntityModel<List<Article>> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedAfter(datePublished); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedAfter(datePublished);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article published after %t", datePublished)); 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) @GetMapping(value = "/getArticlesPublishedBetween/{beginning}/{end}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesPublishedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { public @ResponseBody EntityModel<List<Article>> getArticlesPublishedBetween(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBetween(beginning, end); ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBetween(beginning, end);
if (results.isEmpty()) { if (results.isEmpty()) {
throw new ArticleException(String.format("No article published between %t and %t", beginning, end)); 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 // endregion
@ -242,90 +374,172 @@ public class ArticleControler {
value = "/deleteArticleFromId/{id}", value = "/deleteArticleFromId/{id}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleFromId( public @ResponseBody EntityModel<Response> deleteArticleFromId(
@PathVariable("id") Integer id @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( @DeleteMapping(
value = "/deleteArticleFromTitle/{title}", value = "/deleteArticleFromTitle/{title}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleFromTitle( public @ResponseBody EntityModel<Response> deleteArticleFromTitle(
@PathVariable("title") String title @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( @DeleteMapping(
value = "/deleteArticleFromUrl/{url}", value = "/deleteArticleFromUrl/{url}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleFromUrl( public @ResponseBody EntityModel<Response> deleteArticleFromUrl(
@PathVariable("url") String url @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<Response> 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( @DeleteMapping(
value = "/deleteArticleAddedBefore/{date}", value = "/deleteArticleAddedBefore/{date}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleAddedBefore( public @ResponseBody EntityModel<Response> deleteArticleAddedBefore(
@PathVariable("date") String date @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( @DeleteMapping(
value = "/deleteArticleAddedAfter/{date}", value = "/deleteArticleAddedAfter/{date}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleAddedAfter( public @ResponseBody EntityModel<Response> deleteArticleAddedAfter(
@PathVariable("date") String date @PathVariable("date") String date
) { ) {
return articleService.deleteArticleAddedAfter(date); Response res = articleService.deleteArticleAddedAfter(date);
}@DeleteMapping(
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}", value = "/deleteArticleAddedBetween/{beginning}/{end}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticleAddedBetween( public @ResponseBody EntityModel<Response> deleteArticleAddedBetween(
@PathVariable("beginning") String beginning, @PathVariable("beginning") String beginning,
@PathVariable("end") String end @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( @DeleteMapping(
value = "/deleteArticlePublishedBefore/{date}", value = "/deleteArticlePublishedBefore/{date}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticlePublishedBefore( public @ResponseBody EntityModel<Response> deleteArticlePublishedBefore(
@PathVariable("date") String date @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( @DeleteMapping(
value = "/deleteArticlePublishedAfter/{date}", value = "/deleteArticlePublishedAfter/{date}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticlePublishedAfter( public @ResponseBody EntityModel<Response> deleteArticlePublishedAfter(
@PathVariable("date") String date @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( @DeleteMapping(
value = "/deleteArticlePublishedBetween/{beginning}/{end}", value = "/deleteArticlePublishedBetween/{beginning}/{end}",
produces = MediaType.APPLICATION_JSON_VALUE produces = MediaType.APPLICATION_JSON_VALUE
) )
public @ResponseBody Response deleteArticlePublishedBetween( public @ResponseBody EntityModel<Response> deleteArticlePublishedBetween(
@PathVariable("beginning") String beginning, @PathVariable("beginning") String beginning,
@PathVariable("end") String end @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 // endregion
} }

@ -24,6 +24,7 @@ public interface IArticleService {
public List<Article> getAllArticles(); public List<Article> getAllArticles();
Article getArticlesById(Integer id); Article getArticlesById(Integer id);
public List<Article> getArticlesByTitle(String title); public List<Article> getArticlesByTitle(String title);
public List<Article> getArticlesByUrl(String url);
public List<Article> getArticlesByType(Integer type); public List<Article> getArticlesByType(Integer type);
public List<Article> getVisibleArticles(); public List<Article> getVisibleArticles();
public List<Article> getInvisibleArticles(); public List<Article> getInvisibleArticles();
@ -39,6 +40,7 @@ public interface IArticleService {
public Response deleteArticleFromId(Integer id); public Response deleteArticleFromId(Integer id);
public Response deleteArticleFromTitle(String title); public Response deleteArticleFromTitle(String title);
public Response deleteArticleFromUrl(String url); public Response deleteArticleFromUrl(String url);
public Response deleteArticleFromType(Integer type);
public Response deleteArticleAddedBefore(String date); public Response deleteArticleAddedBefore(String date);
public Response deleteArticleAddedAfter(String date); public Response deleteArticleAddedAfter(String date);
public Response deleteArticleAddedBetween(String beginning, String end); public Response deleteArticleAddedBetween(String beginning, String end);

@ -135,6 +135,22 @@ public class StubArticleService implements IArticleService {
return articles; return articles;
} }
@Override
public List<Article> getArticlesByUrl(String url) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"title",
url,
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override @Override
public List<Article> getArticlesByType(Integer type) { public List<Article> getArticlesByType(Integer type) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
@ -297,6 +313,14 @@ public class StubArticleService implements IArticleService {
); );
} }
@Override
public Response deleteArticleFromType(Integer type) {
return new Response(
1,
"Article successfully deleted"
);
}
@Override @Override
public Response deleteArticleFromTitle(String title) { public Response deleteArticleFromTitle(String title) {
return new Response( return new Response(

Loading…
Cancel
Save