Compare commits

..

16 Commits

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="WebService" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="WebService" options="-parameters" />
</option>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/WebService/src/main/java" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/WebService/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="liberica-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Api_Rest_VWeb.iml" filepath="$PROJECT_DIR$/.idea/Api_Rest_VWeb.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -26,10 +26,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId> <artifactId>spring-boot-starter-hateoas</artifactId>
@ -42,22 +38,6 @@
<groupId>jakarta.persistence</groupId> <groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId> <artifactId>jakarta.persistence-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

@ -0,0 +1,26 @@
package SAE.ApiREST.WebService.Wrapper;
import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.model.Collect;
public class ArticleCollect {
private Collect collection;
private Article article;
public ArticleCollect() {}
public ArticleCollect(Collect collection, Article article) {
this.collection = collection;
this.article = article;
}
public Collect getCollection() {
return collection;
}
public Article getNewArticle() {
return article;
}
// Setters if necessary
}

@ -0,0 +1,25 @@
package SAE.ApiREST.WebService.Wrapper;
import SAE.ApiREST.WebService.model.Collect;
public class CollectionName {
private Collect collection;
private String newName;
public CollectionName() {}
public CollectionName(Collect collection, String newName) {
this.collection = collection;
this.newName = newName;
}
public Collect getCollection() {
return collection;
}
public String getNewName() {
return newName;
}
// Setters if necessary
}

@ -1,16 +1,21 @@
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.CollectionModel;
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.*;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.exception.ArticleException; import SAE.ApiREST.WebService.exception.ArticleException;
import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.service.IArticleService; import SAE.ApiREST.WebService.service.IArticleService;
import java.util.ArrayList; 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 @Controller
@RequestMapping("/ArticleWebService") @RequestMapping("/ArticleWebService")
@ -19,147 +24,561 @@ public class ArticleControler {
IArticleService articleService; IArticleService articleService;
// region POST // region POST
@PostMapping(
value = "/addArticle",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Response> addArticle(
@RequestBody Article article
) {
Response results = articleService.addArticle(article);
return EntityModel.of(
results,
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
// region PUT // region PUT
@PutMapping(
value = "/updateTitle/{title}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> updateTitle(
@PathVariable("title") String title,
@RequestBody Article article
) {
Article results = articleService.updateTitle(article, title);
return EntityModel.of(
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"),
linkTo(methodOn(ArticleControler.class).getArticlesByTitle(article.getTitle())).withRel("getArticlesByTitle"),
linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle")
);
}
@PutMapping(
value = "/updateUrl/{url}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> updateUrl(
@PathVariable("url") String url,
@RequestBody Article article
) {
Article results = articleService.updateUrl(article, url);
return EntityModel.of(
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"),
linkTo(methodOn(ArticleControler.class).getArticlesByUrl(article.getUrl())).withRel("getArticlesByUrl"),
linkTo(methodOn(ArticleControler.class).deleteArticleFromId(article.getId())).withRel("deleteArticle")
);
}
@PutMapping(
value = "/updateDatePublished/{datePublished}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> updateDatePublished(
@PathVariable("datePublished") String datePublished,
@RequestBody Article article
) {
Article results = articleService.updateDatePublished(article, datePublished);
return EntityModel.of(
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"),
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(
value = "/updateDateAdded/{dateAdded}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> updateDateAdded(
@PathVariable("dateAdded") String dateAdded,
@RequestBody Article article
) {
Article results = articleService.updateDateAdded(article, dateAdded);
return EntityModel.of(
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"),
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(
value = "/changeVisibility",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> changeVisibility(
@RequestBody Article article
) {
Article results = articleService.changeVisibility(article);
return EntityModel.of(
results,
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(
value = "/updateType/{type}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Article> updateType(
@PathVariable("type") Integer type,
@RequestBody Article article
) {
Article results = articleService.updateType(article, type);
return EntityModel.of(
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"),
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(
@GetMapping(value = "/getAllArticle", produces = MediaType.APPLICATION_JSON_VALUE) value = "/getAllArticle",
public @ResponseBody List<Article> getAllArticles() { produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.of(
results,
linkTo(methodOn(ArticleControler.class).getAllArticles()).withSelfRel()
);
} }
@GetMapping(value = "/getArticleById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(
public @ResponseBody Article getArticlesById(@PathVariable(value = "id") Integer id) { value = "/getArticleById/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
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(
public @ResponseBody List<Article> getArticlesByTitle(@PathVariable(value = "title") String title) { value = "/getArticlesByTitle/{title}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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 CollectionModel<Article> getArticlesByUrl(@PathVariable(value = "url") String url) {
ArrayList<Article> results = (ArrayList<Article>) articleService.getArticlesByUrl(url);
if(results.isEmpty()) {
throw new ArticleException("Undefined title");
}
return CollectionModel.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(
public @ResponseBody List<Article> getArticlesByType(@PathVariable(value = "type") Integer type) { value = "/getArticlesByType/{type}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getVisibleArticles() { value = "/getVisibleArticles",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.of(
results,
linkTo(methodOn(ArticleControler.class).getVisibleArticles()).withSelfRel()
);
} }
@GetMapping(value = "/getInvisibleArticles", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(
public @ResponseBody List<Article> getInvisibleArticles() { value = "/getInvisibleArticles",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.of(
results,
linkTo(methodOn(ArticleControler.class).getInvisibleArticles()).withSelfRel()
);
} }
@GetMapping(value = "/getArticlesAddedBefore/{dateAdded}", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(
public @ResponseBody List<Article> getArticlesAddedBefore(@PathVariable(value = "dateAdded") String dateAdded) { value = "/getArticlesAddedBefore/{dateAdded}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getArticlesAddedAfter(@PathVariable(value = "dateAdded") String dateAdded) { value = "/getArticlesAddedAfter/{dateAdded}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getArticlesAddedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { value = "/getArticlesAddedBetween/{beginning}/{end}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getArticlesPublishedBefore(@PathVariable(value = "datePublished") String datePublished) { value = "/getArticlesPublishedBefore/{datePublished}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getArticlesPublishedAfter(@PathVariable(value = "datePublished") String datePublished) { value = "/getArticlesPublishedAfter/{datePublished}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.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(
public @ResponseBody List<Article> getArticlesPublishedAfter(@PathVariable(value = "beginning") String beginning, @PathVariable(value = "end") String end) { value = "/getArticlesPublishedBetween/{beginning}/{end}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody CollectionModel<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 CollectionModel.of(
results,
linkTo(methodOn(ArticleControler.class).getArticlesPublishedBetween(beginning, end)).withSelfRel(),
linkTo(methodOn(ArticleControler.class).deleteArticlePublishedBetween(beginning, end)).withRel("getArticlesPublishedBetween")
);
} }
// endregion // endregion
// region DELETE // region DELETE
@DeleteMapping(
value = "/deleteArticleFromId/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Response> deleteArticleFromId(
@PathVariable("id") Integer id
) {
Response results = articleService.deleteArticleFromId(id);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticleFromTitle(
@PathVariable("title") String title
) {
Response results = articleService.deleteArticleFromTitle(title);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticleFromUrl(
@PathVariable("url") String url
) {
Response results = articleService.deleteArticleFromTitle(url);
return EntityModel.of(
results,
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 results = articleService.deleteArticleFromType(type);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticleAddedBefore(
@PathVariable("date") String date
) {
Response results = articleService.deleteArticleAddedBefore(date);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticleAddedAfter(
@PathVariable("date") String date
) {
Response results = articleService.deleteArticleAddedAfter(date);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticleAddedBetween(
@PathVariable("beginning") String beginning,
@PathVariable("end") String end
) {
Response results = articleService.deleteArticleAddedBetween(beginning, end);
return EntityModel.of(
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")
);
}
@DeleteMapping(
value = "/deleteArticlePublishedBefore/{date}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public @ResponseBody EntityModel<Response> deleteArticlePublishedBefore(
@PathVariable("date") String date
) {
Response results = articleService.deleteArticlePublishedBefore(date);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticlePublishedAfter(
@PathVariable("date") String date
) {
Response results = articleService.deleteArticlePublishedBefore(date);
return EntityModel.of(
results,
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 EntityModel<Response> deleteArticlePublishedBetween(
@PathVariable("beginning") String beginning,
@PathVariable("end") String end
) {
Response results = articleService.deleteArticlePublishedBetween(beginning, end);
return EntityModel.of(
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")
);
}
// endregion // endregion
} }

@ -1,16 +1,240 @@
package SAE.ApiREST.WebService.controller; package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.Wrapper.ArticleCollect;
import SAE.ApiREST.WebService.Wrapper.CollectionName;
import SAE.ApiREST.WebService.exception.ArticleException;
import SAE.ApiREST.WebService.exception.CollectException;
import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.model.Collect;
import SAE.ApiREST.WebService.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller @Controller
@RequestMapping("/CollectWebService") @RequestMapping("/CollectWebService")
public class CollectController { public class CollectController {
@Autowired
ICollectionService collectionService;
public CollectController() {
}
// region Collection
// region GET
@GetMapping(value = "/getAllCollection", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Collect> getAllCollection(){
List<Collect> results = collectionService.getAllCollections();
if(results.isEmpty()) {
throw new CollectException("No collections available");
}
return results;
}
@GetMapping(value = "/getCollectionById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<Collect> getCollectionById(@PathVariable(value = "id") Integer id){
Collect results = collectionService.getCollectionById(id);
if(results == null) {
throw new CollectException("No collections available");
}
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getCollectionById(id)).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(id)
).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(id)
).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())
).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@GetMapping(value = "/getAllCollectionsByName/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CollectionModel<EntityModel<Collect>> getAllCollectionsByName(@PathVariable(value = "name") String name){
List<Collect> results = collectionService.getAllCollectionsByName(name);
if(results.isEmpty()) {
throw new CollectException("No collections available");
}
List<EntityModel<Collect>> collectModels = results.stream()
.map(collect -> EntityModel.of(collect,
linkTo(methodOn(CollectController.class).getAllCollectionsByName(name)).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection")))
.collect(Collectors.toList());
return CollectionModel.of(collectModels);
} }
// endregion
// region DELETE
@DeleteMapping(value = "/deleteColletionById/{id}")
public @ResponseBody EntityModel<Link> deleteColletionById(@PathVariable("id") Integer id){
if(!collectionService.deleteColletionById(id)) {
throw new CollectException("No collections available");
}
return EntityModel.of(linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@DeleteMapping(value = "/deleteColletionByName/{name}")
public @ResponseBody EntityModel<Link> deleteColletionByName(@PathVariable("name") String name){
if(!collectionService.deleteColletionByName(name)) {
throw new CollectException("No collections available");
}
return EntityModel.of(linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@DeleteMapping(value = "/deleteAllColletionByName/{name}")
public @ResponseBody EntityModel<Link> deleteAllColletionByName(@PathVariable("name") String name){
if(!collectionService.deleteAllColletionByName(name)) {
throw new CollectException("No collections available");
}
return EntityModel.of(linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
// endregion
// region PUT
@PutMapping(value = "/addCollection")
public @ResponseBody EntityModel<Collect> addCollection(@RequestBody Collect collection){
Collect results = collectionService.addCollection(collection);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getCollectionById(results.getId())).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(results.getId())
).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(results.getId())
).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())
).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@PutMapping(value = "/addCollections")
public @ResponseBody CollectionModel<EntityModel<Collect>> addCollections(@RequestBody List<Collect> collections){
List<Collect> results = collectionService.addCollections(collections);
List<EntityModel<Collect>> collectModels = results.stream()
.map(collect -> EntityModel.of(collect,
linkTo(methodOn(CollectController.class).getAllCollection()).withRel("getAllCollection")))
.collect(Collectors.toList());
return CollectionModel.of(collectModels);
}
// endregion
// region POST
@PostMapping(value="/modifyCollectionName", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<Collect> modifyCollectionName(@RequestBody CollectionName request) {
String newName = request.getNewName();
Collect collection = request.getCollection();
if(collection == null) {
throw new ArticleException("No articles available");
}
Collect results = collectionService.modifyCollectionName(collection, newName);
// Assuming your modification logic here
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getCollectionById(results.getId())).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(results.getId())).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(results.getId())).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()).withRel("getAllCollection"));
}
@PostMapping(value="/modifyCollectionNameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<Collect> modifyCollectionNameById(@PathVariable("id") Integer id, @RequestBody String name){
Collect results = collectionService.modifyCollectionNameById(id,name);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getCollectionById(id)).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(id)
).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(id)
).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())
).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
// endregion
// endregion
// region Article
@GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CollectionModel<EntityModel<Article>> getAllArticlesById(@PathVariable(value = "id") Integer id){
List<Article> results = collectionService.getAllArticlesById(id);
if(results == null) {
throw new ArticleException("No articles available");
}
List<EntityModel<Article>> collectModels = results.stream()
.map(collect -> EntityModel.of(collect,
linkTo(methodOn(CollectController.class).getCollectionById(id)).withRel("getCollection"),
linkTo(methodOn(CollectController.class).getAllCollection()).withRel("getAllCollection")))
.collect(Collectors.toList());
return CollectionModel.of(collectModels);
}
@PutMapping(value = "/addArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<Article> addArticle(@RequestBody ArticleCollect request) {
Article article = request.getNewArticle();
Collect collection = request.getCollection();
if(article == null) {
throw new ArticleException("Invalid article");
}
if(collection == null) {
throw new CollectException("No articles available");
}
Collect results = collectionService.addArticle(collection,article);
Article art = results.getAllArticles().get(results.getAllArticles().indexOf(article));
return EntityModel.of(art,
linkTo(methodOn(CollectController.class).getCollectionById(results.getId())).withSelfRel(),
linkTo(methodOn(TeacherController.class).getTeachById(results.getTeacher().getId())).withSelfRel(),
linkTo(methodOn(ArticleControler.class).getArticleById(art.getId())).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(results.getId())
).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(results.getId())
).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())
).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@DeleteMapping(value = "/deleteArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<Collect> deleteArticle(@RequestBody ArticleCollect request) {
Article article = request.getNewArticle();
Collect collection = request.getCollection();
if(article == null) {
throw new ArticleException("Invalid article");
}
if(collection == null) {
throw new CollectException("No articles available");
}
Collect results = collectionService.deleteArticle(collection,article);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getCollectionById(results.getId())).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllArticlesById(results.getId())
).withRel("getAllArticlesById"),
linkTo(methodOn(CollectController.class).deleteColletionById(results.getId())
).withRel("deleteColletionById"),
linkTo(methodOn(CollectController.class).deleteAllColletionByName(results.getName())
).withRel("deleteAllColletionByName"),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
// endregion
}

@ -2,9 +2,8 @@ package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.exception.TeacherException; import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher; import SAE.ApiREST.WebService.model.Teacher;
import SAE.ApiREST.WebService.service.TeacherService; import SAE.ApiREST.WebService.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -12,6 +11,8 @@ 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.*;
import javax.print.attribute.standard.Media;
import java.awt.*;
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.linkTo;
@ -20,21 +21,19 @@ import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller @Controller
@RequestMapping("/ProfWebService") @RequestMapping("/ProfWebService")
public class TeacherController { public class TeacherController {
@Autowired @Autowired
private TeacherService teacherServ; private ITeacherService iTeacherServ;
/*public void setiTeacherServ(TeacherService iTeacherS) {
this.teacherServ = iTeacherS;
}*/
public TeacherController(ITeacherService iserv) {
this.iTeacherServ = iserv;
}
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public @ResponseBody CollectionModel<Teacher> getAllTeacher(){ public @ResponseBody CollectionModel<Teacher> getAllTeacher(){
return CollectionModel.of( return CollectionModel.of(
teacherServ.getAllTeacher(), iTeacherServ.getAllTeacher(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel()); linkTo(methodOn(TeacherController.class).getAllTeacher()).withSelfRel());
} }
@PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ -47,7 +46,7 @@ public class TeacherController {
} }
@GetMapping(value = "/getid/{id}") @GetMapping(value = "/getid/{id}")
public @ResponseBody EntityModel<Teacher> getTeachById(@PathVariable("id") Integer id){ public @ResponseBody EntityModel<Teacher> getTeachById(@PathVariable("id") Integer id){
Teacher tt = teacherServ.getTeacherById(id); Teacher tt = iTeacherServ.getTeacherById(id);
if( tt == null ){ if( tt == null ){
throw new TeacherException("No teacher found for this id !"); throw new TeacherException("No teacher found for this id !");
} }
@ -57,7 +56,7 @@ public class TeacherController {
} }
@GetMapping(value = "/getusername/{username}") @GetMapping(value = "/getusername/{username}")
public @ResponseBody EntityModel<Teacher> getTeachByUsername(@PathVariable("username") String username) { public @ResponseBody EntityModel<Teacher> getTeachByUsername(@PathVariable("username") String username) {
Teacher tt = teacherServ.getTeacherByUsername(username); Teacher tt = iTeacherServ.getTeacherByUsername(username);
if (tt == null) { if (tt == null) {
throw new TeacherException("No teacher found for this username"); throw new TeacherException("No teacher found for this username");
} }
@ -67,7 +66,7 @@ public class TeacherController {
} }
@GetMapping( value = "/getmail/{mail}" ) @GetMapping( value = "/getmail/{mail}" )
public @ResponseBody EntityModel<Teacher> getTeachByMail(@PathVariable("mail") String mail) { public @ResponseBody EntityModel<Teacher> getTeachByMail(@PathVariable("mail") String mail) {
Teacher tt = teacherServ.getTeacherByMail(mail); Teacher tt = iTeacherServ.getTeacherByMail(mail);
if( tt == null ) { if( tt == null ) {
throw new TeacherException("No teacher found for this mail"); throw new TeacherException("No teacher found for this mail");
} }
@ -77,7 +76,7 @@ public class TeacherController {
} }
@GetMapping( value = "/getdate/{date}" ) @GetMapping( value = "/getdate/{date}" )
public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) { public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) {
Teacher tt = teacherServ.getTeacherByMail(date); Teacher tt = iTeacherServ.getTeacherByMail(date);
if( tt == null ) { if( tt == null ) {
throw new TeacherException("No teacher found for this mail"); throw new TeacherException("No teacher found for this mail");
} }
@ -88,14 +87,14 @@ public class TeacherController {
if( username == "" ){ if( username == "" ){
throw new TeacherException("Username provided for modification is empty"); throw new TeacherException("Username provided for modification is empty");
} }
teacherServ.modifyUsername(tt,username); iTeacherServ.modifyUsername(tt,username);
return EntityModel.of(tt, return EntityModel.of(tt,
linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(), linkTo(methodOn(TeacherController.class).modifyTeachUsername(username,tt)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
} }
@DeleteMapping( value = "delete") @DeleteMapping( value = "delete")
public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){ public @ResponseBody EntityModel<List<Teacher>> deleteTeacher(Integer id){
return EntityModel.of(teacherServ.deleteTeacher(id), return EntityModel.of(iTeacherServ.deleteTeacher(id),
linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(), linkTo(methodOn(TeacherController.class).deleteTeacher(id)).withSelfRel(),
linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all")); linkTo(methodOn(TeacherController.class).getAllTeacher()).withRel("all"));
} }

@ -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 CollectAdvice {
@ResponseBody
@ExceptionHandler(CollectException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
String collectHandler(CollectException ex) {
return ex.getMessage();
}
}

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

@ -2,27 +2,109 @@ package SAE.ApiREST.WebService.model;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import jakarta.persistence.*; import jakarta.persistence.Entity;
import lombok.*; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Article { public class Article {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
Integer id; Integer id;
String title; String title;
String URL; String url;
LocalDate dateAdded; LocalDate dateAdded;
LocalDate datePublished; LocalDate datePublished;
Boolean isVisible; Boolean visible;
Integer type; Integer type;
// ArrayList<Keyword> keywords = new ArrayList<>(); // ArrayList<Keyword> keywords = new ArrayList<>();
public Article() {}
public Article(String title, String url, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) {
this.id = 1;
this.title = title;
this.url = url;
this.dateAdded = dateAdded;
this.datePublished = datePublished;
this.visible = visibility;
this.type = type;
}
public Article(String title, String url, String dateAdded, String datePublished, Boolean visibility, Integer type) {
this.id = 1;
this.title = title;
this.url = url;
this.dateAdded = LocalDate.parse(dateAdded, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.datePublished = LocalDate.parse(datePublished, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.visible = visibility;
this.type = type;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public LocalDate getDateAdded() {
return this.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(String datePublished) {
this.datePublished = LocalDate.parse(datePublished, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
public Boolean getVisible() {
return this.visible;
}
public void setVisibility(Boolean visible) {
this.visible = visible;
}
public Integer getType() {
return this.type;
}
public void setType(Integer type) {
this.type = type;
}
/*
public List<Keyword> getKeywords() {
return this.keywords;
}
public void setKeywords(List<Keyword> keywords) {
this.keywords = keywords;
}
*/
} }

@ -1,26 +1,82 @@
package SAE.ApiREST.WebService.model; package SAE.ApiREST.WebService.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Cascade;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Collect { public class Collect {
@Id @Id
@GeneratedValue(strategy=GenerationType.AUTO) @GeneratedValue(strategy=GenerationType.AUTO)
private final long isbn = 0; private Integer id;
@Column(name = "articles")
@JoinColumn(name = "articles") @OneToMany(cascade = CascadeType.DETACH)
private ArrayList<Article> articles; private ArrayList<Article> articles;
@Column(name = "name") @Column(name = "name")
private String name; private String name;
@JoinColumn(name = "teacher") @ManyToOne @Column(name = "teacher")
private Teacher teacher; private Teacher teacher;
public Collect() {
}
public Collect(String name, Teacher teacher){
this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
}
public Collect(String name, Teacher teacher, Integer id){
this.name = name;
this.teacher = teacher;
this.articles = new ArrayList<Article>();
this.id = id;
}
// region Article
public Integer getId(){
return id;
}
// endregion
// region Article
public List<Article> getAllArticles(){
return articles;
}
// region addArticle
public void addArticle(Article article){
if(!this.articles.contains(article)){
this.articles.add(article);
}
}
public void addArticles(List<Article> articles){
for(Article article : articles){
addArticle(article);
}
}
// endregion
// region removeArticle
public void removeArticle(Article article){
this.articles.remove(article);
}
public void removeArticles(List<Article> articles){
this.articles.removeAll(articles);
}
// endregion
// endregion
// region name
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
// endregion
// region teacher
public Teacher getTeacher(){
return teacher;
}
// endregion
} }

@ -1,23 +1,59 @@
package SAE.ApiREST.WebService.model; package SAE.ApiREST.WebService.model;
import jakarta.persistence.*; import jakarta.persistence.Entity;
import lombok.*; import jakarta.persistence.Id;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date;
@Entity @Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Teacher { public class Teacher {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id; private Integer id;
private LocalDate date; private LocalDate date;
private String mail; private String mail;
private String username; private String username;
public Teacher() {
}
public Teacher(Integer id, String date, String mail, String username) {
this.id = id;
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.mail = mail;
this.username = username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalDate getDate() {
return date;
}
public void setDate(String date) {
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
} }

@ -1,4 +0,0 @@
package SAE.ApiREST.WebService.repository;
public interface IArticleRepository {
}

@ -1,4 +0,0 @@
package SAE.ApiREST.WebService.repository;
public interface ICollectRepository {
}

@ -1,19 +0,0 @@
package SAE.ApiREST.WebService.repository;
import SAE.ApiREST.WebService.model.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.util.List;
@Repository
public interface ITeacherRepository extends JpaRepository< Teacher, Integer> {
Teacher findByusername(String username);
Teacher findBymail(String mail);
}

@ -2,23 +2,50 @@ package SAE.ApiREST.WebService.service;
import java.util.List; import java.util.List;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.model.Article;
public interface IArticleService { 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 // region GET
List<Article> getAllArticles(); public List<Article> getAllArticles();
Article getArticlesById(Integer id); Article getArticlesById(Integer id);
List<Article> getArticlesByTitle(String title); public List<Article> getArticlesByTitle(String title);
List<Article> getArticlesByType(Integer type); public List<Article> getArticlesByUrl(String url);
List<Article> getVisibleArticles(); public List<Article> getArticlesByType(Integer type);
List<Article> getInvisibleArticles(); public List<Article> getVisibleArticles();
List<Article> getArticlesAddedBefore(String dateAdded); public List<Article> getInvisibleArticles();
List<Article> getArticlesAddedAfter(String dateAdded); public List<Article> getArticlesAddedBefore(String dateAdded);
List<Article> getArticlesAddedBetween(String beginning, String end); public List<Article> getArticlesAddedAfter(String dateAdded);
List<Article> getArticlesPublishedBefore(String datePublished); public List<Article> getArticlesAddedBetween(String beginning, String end);
List<Article> getArticlesPublishedAfter(String datePublished); public List<Article> getArticlesPublishedBefore(String datePublished);
List<Article> getArticlesPublishedBetween(String beginning, String end); public List<Article> getArticlesPublishedAfter(String datePublished);
public List<Article> getArticlesPublishedBetween(String beginning, String end);
// endregion // endregion
// region DELETE
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);
public Response deleteArticlePublishedBefore(String date);
public Response deleteArticlePublishedAfter(String date);
public Response deleteArticlePublishedBetween(String beginning, String end);
// endregion
} }

@ -5,17 +5,18 @@ import SAE.ApiREST.WebService.model.Article;
import java.util.List; import java.util.List;
public interface ICollectionService{ public interface ICollectionService{
List<Collect> getAllCollections(); public List<Collect> getAllCollections();
Collect getCollectionById(long isbn); public Collect getCollectionById(Integer id);
List<Collect> getAllCollectionsByName(String name); public List<Collect> getAllCollectionsByName(String name);
void deleteColletionById(long isbn); public Collect getCollection(Collect collect);
void deleteColletionByName(String name); public boolean deleteColletionById(Integer id);
void deleteAllColletionByName(String name); public boolean deleteColletionByName(String name);
void addCollection(Collect collection); public boolean deleteAllColletionByName(String name);
void addCollections(List<Collect> collection); public Collect addCollection(Collect collection);
void modifyCollectionName(Collect collection, String name); public List<Collect> addCollections(List<Collect> collection);
void modifyCollectionNameById(long isbn, String name); public Collect modifyCollectionName(Collect collection, String name);
List<Article> getAllArticles(Collect collection); public Collect modifyCollectionNameById(Integer id, String name);
void addArticle(Collect collection, Article article); public List<Article> getAllArticlesById(Integer id);
void deleteArticle(Collect collection, Article article); public Collect addArticle(Collect collection, Article article);
public Collect deleteArticle(Collect collection, Article article);
} }

@ -0,0 +1,17 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Teacher;
import java.util.List;
public interface ITeacherService {
public List<Teacher> getAllTeacher();
public Teacher getTeacherById(Integer id);
public Teacher getTeacherByUsername(String username);
public Teacher getTeacherByMail(String mail);
public Teacher getTeacherByDate(String date);
public List<Teacher> addTeacher(Teacher t);
public List<Teacher> deleteTeacher(Integer id);
public Response modifyUsername(Teacher t, String newUsername);
}

@ -7,17 +7,72 @@ import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.model.Article;
@Service @Service
public class StubArticleService implements IArticleService { 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 // region GET
@Override @Override
public List<Article> getAllArticles() { public List<Article> getAllArticles() {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -25,7 +80,7 @@ public class StubArticleService implements IArticleService {
true, true,
1) 1)
); );
articles.add(new Article(null, articles.add(new Article(
"moi", "moi",
"zaeaeaeazeza", "zaeaeaeazeza",
LocalDate.now().minusMonths(2), LocalDate.now().minusMonths(2),
@ -33,7 +88,7 @@ public class StubArticleService implements IArticleService {
false, false,
1) 1)
); );
articles.add(new Article(null, articles.add(new Article(
"eux", "eux",
"erfdhdh", "erfdhdh",
LocalDate.now().minusMonths(3), LocalDate.now().minusMonths(3),
@ -41,7 +96,7 @@ public class StubArticleService implements IArticleService {
true, true,
1) 1)
); );
articles.add(new Article(null, articles.add(new Article(
"tout ceux qui le veulent", "tout ceux qui le veulent",
"azersdfgg", "azersdfgg",
LocalDate.now().minusMonths(4), LocalDate.now().minusMonths(4),
@ -55,7 +110,7 @@ public class StubArticleService implements IArticleService {
@Override @Override
public Article getArticlesById(Integer id) { public Article getArticlesById(Integer id) {
return new Article(1, return new Article(
"azeaeaze", "azeaeaze",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -68,7 +123,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesByTitle(String title) { public List<Article> getArticlesByTitle(String title) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(1, articles.add(new Article(
title, title,
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -80,11 +135,27 @@ 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<>();
articles.add(new Article(1, articles.add(new Article(
"aeazeazeaz", "aeazeazeaz",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -100,7 +171,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getVisibleArticles() { public List<Article> getVisibleArticles() {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(1, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -116,7 +187,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getInvisibleArticles() { public List<Article> getInvisibleArticles() {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(1, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -132,7 +203,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesAddedBefore(String dateAdded) { public List<Article> getArticlesAddedBefore(String dateAdded) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).minusMonths(1), LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).minusMonths(1),
@ -148,7 +219,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesAddedAfter(String dateAdded) { public List<Article> getArticlesAddedAfter(String dateAdded) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).plusMonths(1), LocalDate.parse(dateAdded, DateTimeFormatter.ISO_DATE).plusMonths(1),
@ -164,7 +235,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesAddedBetween(String beginning, String end) { public List<Article> getArticlesAddedBetween(String beginning, String end) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.parse(beginning, DateTimeFormatter.ISO_DATE), LocalDate.parse(beginning, DateTimeFormatter.ISO_DATE),
@ -180,7 +251,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesPublishedBefore(String datePublished) { public List<Article> getArticlesPublishedBefore(String datePublished) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -196,7 +267,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesPublishedAfter(String datePublished) { public List<Article> getArticlesPublishedAfter(String datePublished) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -212,7 +283,7 @@ public class StubArticleService implements IArticleService {
public List<Article> getArticlesPublishedBetween(String beginning, String end) { public List<Article> getArticlesPublishedBetween(String beginning, String end) {
List<Article> articles = new ArrayList<>(); List<Article> articles = new ArrayList<>();
articles.add(new Article(null, articles.add(new Article(
"toi", "toi",
"azezeaea", "azezeaea",
LocalDate.now().minusMonths(1), LocalDate.now().minusMonths(1),
@ -224,4 +295,86 @@ public class StubArticleService implements IArticleService {
return articles; return articles;
} }
// endregion // 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 deleteArticleFromType(Integer type) {
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
} }

@ -1,92 +1,150 @@
package SAE.ApiREST.WebService.service; package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.controller.ArticleControler;
import SAE.ApiREST.WebService.model.Collect; import SAE.ApiREST.WebService.model.Collect;
import SAE.ApiREST.WebService.model.Teacher;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import SAE.ApiREST.WebService.model.Article; import SAE.ApiREST.WebService.model.Article;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class StubCollectionService implements ICollectionService { public class StubCollectionService implements ICollectionService {
private ArrayList<Collect> collections; private List<Collect> collects = new ArrayList<>();
public StubCollectionService(){
this.collects.add(new Collect("collect1", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),0));
Collect collection2 = new Collect("collect2", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),1);
collection2.addArticle(new Article("toi","azezeaea", LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
this.collects.add(collection2);
Collect collection3 = new Collect("collect3", new Teacher(1, "12-03-2023", "aline.alipres@gmail.com", "MsGarconManque"),3);
collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
collection3.addArticle(new Article("toi","azezeaea",LocalDate.now().minusMonths(1),LocalDate.now().minusMonths(2),true,1));
this.collects.add(collection3);
}
@Override
public List<Collect> getAllCollections() { public List<Collect> getAllCollections() {
return this.collections; return collects;
} }
// region Collection // region Collection
// region GET // region GET
public Collect getCollectionById(long isbn){ @Override
for(Collect collection : this.collections){ public Collect getCollectionById(Integer id){
if(collection.getIsbn() == isbn) { try {
return collection; return collects.get(id);
} }catch (Exception e){
}
return null; return null;
} }
}
@Override
public List<Collect> getAllCollectionsByName(String name){ public List<Collect> getAllCollectionsByName(String name){
ArrayList<Collect> repCollections = new ArrayList<Collect>(); ArrayList<Collect> repCollections = new ArrayList<Collect>();
for(Collect collection : this.collections){ for(Collect collection : collects){
if(collection.getName() == name) { if(Objects.equals(collection.getName(), name)) {
repCollections.add(collection); repCollections.add(collection);
} }
} }
return repCollections; return repCollections;
} }
@Override
public Collect getCollection(Collect collect){
return getCollectionById(collect.getId());
}
// endregion // endregion
// region DELETE // region DELETE
public void deleteColletionById(long isbn){ @Override
Collect collection = getCollectionById(isbn); public boolean deleteColletionById(Integer id){
this.collections.remove(collection); Collect collection = getCollectionById(id);
if (collection != null) {
collects.remove(collection);
return true;
}
return false;
} }
public void deleteColletionByName(String name){ @Override
public boolean deleteColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name); List<Collect> collectionsByName = getAllCollectionsByName(name);
this.collections.remove(collectionsByName.get(0)); if (!collectionsByName.isEmpty()) {
collects.remove(collectionsByName.get(0));
return true;
}
return false;
} }
public void deleteAllColletionByName(String name){
@Override
public boolean deleteAllColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name); List<Collect> collectionsByName = getAllCollectionsByName(name);
this.collections.removeAll(collectionsByName); if (!collectionsByName.isEmpty()) {
collects.removeAll(collectionsByName);
return true;
}
return false;
} }
@Override @Override
public void addCollection(Collect collection) { public Collect addCollection(Collect collection) {
this.collections.add(collection); collects.add(collection);
return collects.get(collects.lastIndexOf(collection));
} }
// endregion // endregion
// region PUT // region PUT
public void addCollections(List<Collect> collections){ @Override
this.collections.addAll(collections); public List<Collect> addCollections(List<Collect> collections){
collects.addAll(collections);
return collects;
} }
// endregion // endregion
// region POST // region POST
public void modifyCollectionName(Collect collection, String name){ @Override
collection.setName(name); public Collect modifyCollectionName(Collect collection, String name){
} Collect collect = getCollectionById(collection.getId());
public void modifyCollectionNameById(long isbn, String name){ collect.setName(name);
Collect collection = getCollectionById(isbn); return collect;
modifyCollectionName(collection,name);
} }
@Override
public Collect modifyCollectionNameById(Integer id, String name){
Collect collect = getCollectionById(id);
collect.setName(name);
return collect;
}
// endregion // endregion
// endregion // endregion
// region Article // region Article
public List<Article> getAllArticles(Collect collect) @Override
{ public List<Article> getAllArticlesById(Integer id){
return collect.getArticles(); Collect collect = getCollectionById(id);
List<Article> result = collect.getAllArticles();
if(!result.isEmpty()){
return result;
} }
public void addArticle(Collect collect, Article article){ return null;
List<Article> lA = collect.getArticles();
lA.add(article);
collect.setArticles((ArrayList<Article>) lA);
} }
public void deleteArticle(Collect collect, Article article){ @Override
collect.getArticles().remove(article); public Collect addArticle(Collect collect, Article article){
Collect collection = getCollectionById(collect.getId());
collection.addArticle(article);
return collection;
} }
@Override
public Collect deleteArticle(Collect collect, Article article){
Collect collection = getCollectionById(collect.getId());
collection.removeArticle(article);
return collection;
}
// endregion
} }

@ -1,77 +0,0 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Teacher;
import SAE.ApiREST.WebService.repository.ITeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class TeacherService {
private final ITeacherRepository teachRep;
public TeacherService(ITeacherRepository teachRep) {
this.teachRep = teachRep;
}
public List<Teacher> getAllTeacher() {
return teachRep.findAll();
}
public Teacher getTeacherById(Integer id) {
return teachRep.getReferenceById(id);
}
public Teacher getTeacherByUsername(String username) {
return teachRep.findByusername(username);
}
public Teacher getTeacherByMail(String mail) {
return teachRep.findBymail(mail);
}
/*
public Teacher getTeacherByDate(String date) {
LocalDate d = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
return teachRep.findByDate(d);
}*/
public List<Teacher> addTeacher(Teacher t) {
List<Teacher> lT = getAllTeacher();
lT.add(t);
return lT;
}
public List<Teacher> deleteTeacher(Integer id) {
List<Teacher> lT = getAllTeacher();
lT.remove(getTeacherById(id));
return lT;
}
public Response modifyUsername(Teacher t, String newUsername) {
t.setUsername(newUsername);
return new Response(t.getId(), String.format("Username changed for {id}", t.getId()));
}
}

@ -0,0 +1,69 @@
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;
import java.util.ArrayList;
import java.util.List;
@Service
public class TeacherServiceStub implements ITeacherService {
@Override
public List<Teacher> getAllTeacher() {
List<Teacher> allTeacher = new ArrayList<Teacher>();
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"));
return allTeacher;
}
@Override
public Teacher getTeacherById(Integer id) {
return new Teacher(id, "10-01-2021", "exemple.gmail.com", "testest");
}
@Override
public Teacher getTeacherByUsername(String username) { return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username); }
@Override
public Teacher getTeacherByMail(String mail) {
return new Teacher(20, "24-12-2021", mail, "tructruc");
}
@Override
public Teacher getTeacherByDate(String date) {
return new Teacher(5, date, "doudouda@gmail.com", "username");
}
@Override
public List<Teacher> addTeacher(Teacher t) {
List<Teacher> lteach = new ArrayList<Teacher>();
lteach.add(t);
return lteach;
}
@Override
public List<Teacher> deleteTeacher(Integer id) {
List<Teacher> allTeacher = new ArrayList<Teacher>();
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"));
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()));
}
}

@ -1,24 +1 @@
/*
#Todo adapter a une de nos BD choisir le langage bd
spring.datasource.url=nomBD:mysql://localhost:8889/XXXXXX?createDatabaseIfNotExist=true
spring.datasource.username=test
spring.datasource.password=test
spring.jpa.open-in-view=false
# Hibernate properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
# Logging SQL statements (optional, but useful for debugging)
spring.jpa.show-sql=true
*/
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Loading…
Cancel
Save