GET requests

feature/response
felix 1 year ago
parent dea039601c
commit de0aac4769

@ -5,13 +5,13 @@
<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.3</version> <version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>SAE.ApiREST</groupId> <groupId>com.example</groupId>
<artifactId>WebService</artifactId> <artifactId>WebService</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>WebService</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>
@ -27,6 +27,15 @@
<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>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -0,0 +1,165 @@
package SAE.ApiREST.WebService.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import SAE.ApiREST.WebService.exception.ArticleException;
import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.service.IArticleService;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/ArticleWebService")
public class ArticleControler {
@Autowired
IArticleService articleService;
// region POST
// endregion
// region PUT
// endregion
// region GET
@GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getAllArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getAllArticles();
if(results.isEmpty()) {
throw new ArticleException("No articles available");
}
return results;
}
@GetMapping(value = "/getArticleById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Article getArticlesById(@PathVariable(value = "id") Integer id) {
Article results = articleService.getArticlesById(id);
if(results == null) {
throw new ArticleException("Undefined id");
}
return results;
}
@GetMapping(value = "/getArticlesByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesByTitle(@PathVariable(value = "title") String title) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByTitle(title);
if(results.isEmpty()) {
throw new ArticleException("Undefined title");
}
return results;
}
@GetMapping(value = "/getArticlesByType/{type}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesByType(@PathVariable(value = "type") Integer type) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByType(type);
if (results.isEmpty()) {
throw new ArticleException(String.format("No content of type %d", type));
}
return results;
}
@GetMapping(value = "/getVisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getVisibleArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getVisibleArticles();
if (results.isEmpty()) {
throw new ArticleException("No visible article");
}
return results;
}
@GetMapping(value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getInvisibleArticles() {
ArrayList<Article> results = (ArrayList<Article>) articleService.getInvisibleArticles();
if (results.isEmpty()) {
throw new ArticleException("No invisible article");
}
return results;
}
@GetMapping(value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBefore(dateAdded);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article added before %t", dateAdded));
}
return results;
}
@GetMapping(value = "/getArticlesAddedAfter/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedAfter(dateAdded);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article added after %t", dateAdded));
}
return results;
}
@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) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesAddedBetween(beginning, end);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article added between %t and %t", beginning, end));
}
return results;
}
@GetMapping(value = "/getArticlesPublishedBefore/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBefore(datePublished);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article published before %t", datePublished));
}
return results;
}
@GetMapping(value = "/getArticlesPublishedAfter/{datePublished}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Article> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedAfter(datePublished);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article published after %t", datePublished));
}
return results;
}
@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) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesPublishedBetween(beginning, end);
if (results.isEmpty()) {
throw new ArticleException(String.format("No article published between %t and %t", beginning, end));
}
return results;
}
// endregion
// region DELETE
// endregion
}

@ -0,0 +1,17 @@
package SAE.ApiREST.WebService.exception;
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;
import org.springframework.http.HttpStatus;
@ControllerAdvice
public class ArticleAdvice {
@ResponseBody
@ExceptionHandler(ArticleException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String articleHandler(ArticleException ex) {
return ex.getMessage();
}
}

@ -0,0 +1,7 @@
package SAE.ApiREST.WebService.exception;
public class ArticleException extends RuntimeException {
public ArticleException(String exception) {
super(exception);
}
}

@ -12,6 +12,7 @@ import jakarta.persistence.Id;
public class Article { public class Article {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
String id;
String title; String title;
String URL; String URL;
LocalDate dateAdded; LocalDate dateAdded;
@ -23,6 +24,7 @@ public class Article {
public Article() {} public Article() {}
public Article(String title, String URL, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) { public Article(String title, String URL, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) {
this.id = "1";
this.title = title; this.title = title;
this.URL = URL; this.URL = URL;
this.dateAdded = dateAdded; this.dateAdded = dateAdded;
@ -31,6 +33,14 @@ public class Article {
this.type = type; this.type = type;
} }
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() { public String getTitle() {
return this.title; return this.title;
} }

@ -0,0 +1,24 @@
package SAE.ApiREST.WebService.service;
import java.util.List;
import SAE.ApiREST.WebService.model.Article;
public interface IArticleService {
// region GET
public List<Article> getAllArticles();
Article getArticlesById(Integer id);
public List<Article> getArticlesByTitle(String title);
public List<Article> getArticlesByType(Integer type);
public List<Article> getVisibleArticles();
public List<Article> getInvisibleArticles();
public List<Article> getArticlesAddedBefore(String dateAdded);
public List<Article> getArticlesAddedAfter(String dateAdded);
public List<Article> getArticlesAddedBetween(String beginning, String end);
public List<Article> getArticlesPublishedBefore(String datePublished);
public List<Article> getArticlesPublishedAfter(String datePublished);
public List<Article> getArticlesPublishedBetween(String beginning, String end);
// endregion
}

@ -0,0 +1,227 @@
package SAE.ApiREST.WebService.service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import SAE.ApiREST.WebService.model.Article;
@Service
public class StubArticleService implements IArticleService {
// region GET
@Override
public List<Article> getAllArticles() {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
articles.add(new Article(
"moi",
"zaeaeaeazeza",
LocalDate.now().minusMonths(2),
LocalDate.now().minusMonths(3),
false,
1)
);
articles.add(new Article(
"eux",
"erfdhdh",
LocalDate.now().minusMonths(3),
LocalDate.now().minusMonths(4),
true,
1)
);
articles.add(new Article(
"tout ceux qui le veulent",
"azersdfgg",
LocalDate.now().minusMonths(4),
LocalDate.now().minusMonths(5),
false,
2)
);
return articles;
}
@Override
public Article getArticlesById(Integer id) {
return new Article(
"azeaeaze",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1);
}
@Override
public List<Article> getArticlesByTitle(String title) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
title,
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesByType(Integer type) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"aeazeazeaz",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
type)
);
return articles;
}
@Override
public List<Article> getVisibleArticles() {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getInvisibleArticles() {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.now().minusMonths(2),
false,
1)
);
return articles;
}
@Override
public List<Article> getArticlesAddedBefore(String dateAdded) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).minusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesAddedAfter(String dateAdded) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).plusMonths(1),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesAddedBetween(String beginning, String end) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.parse(beginning, DateTimeFormatter.ISO_DATE),
LocalDate.now().minusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesPublishedBefore(String datePublished) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.parse(datePublished, DateTimeFormatter.ISO_DATE).plusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesPublishedAfter(String datePublished) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.parse(datePublished, DateTimeFormatter.ISO_DATE).plusMonths(2),
true,
1)
);
return articles;
}
@Override
public List<Article> getArticlesPublishedBetween(String beginning, String end) {
List<Article> articles = new ArrayList<>();
articles.add(new Article(
"toi",
"azezeaea",
LocalDate.now().minusMonths(1),
LocalDate.parse(end, DateTimeFormatter.ISO_DATE),
true,
1)
);
return articles;
}
// endregion
}
Loading…
Cancel
Save