ajout HATOES

Maxime
Maxime POINT 1 year ago
parent 68a9aad682
commit 194eff0552

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
@ -22,13 +21,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -38,14 +39,12 @@
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -6,12 +6,17 @@ 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.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
@Controller
@RequestMapping("/CollectWebService")
public class CollectController {
@ -34,84 +39,167 @@ public class CollectController {
return results;
}
@GetMapping(value = "/getCollectionById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Collect getCollectionById(@PathVariable(value = "id") Integer id){
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 results;
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 List<Collect> getAllCollectionsByName(@PathVariable(value = "name") String name){
public @ResponseBody EntityModel<List<Collect>> getAllCollectionsByName(@PathVariable(value = "name") String name){
List<Collect> results = collectionService.getAllCollectionsByName(name);
if(results.isEmpty()) {
throw new CollectException("No collections available");
}
return results;
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollectionsByName(name)).withSelfRel(),
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
// endregion
// region DELETE
@DeleteMapping(value = "/deleteColletionById/{id}")
public @ResponseBody void deleteColletionById(@RequestParam("id") Integer id){
collectionService.deleteColletionById(id);
public @ResponseBody EntityModel<Link> deleteColletionById(@RequestParam("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 void deleteColletionByName(@RequestParam("name") String name){
collectionService.deleteColletionByName(name);
public @ResponseBody EntityModel<Link> deleteColletionByName(@RequestParam("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 void deleteAllColletionByName(@RequestParam("name") String name){
collectionService.deleteAllColletionByName(name);
public @ResponseBody EntityModel<Link> deleteAllColletionByName(@RequestParam("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 void addCollection(@RequestParam("collection") Collect collection){
collectionService.addCollection(collection);
public @ResponseBody EntityModel<Collect> addCollection(@RequestParam("collection") 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 void addCollections(@RequestParam("collections") List<Collect> collections){
collectionService.addCollections(collections);
public @ResponseBody EntityModel<List<Collect>> addCollections(@RequestParam("collections") List<Collect> collections){
List<Collect> results = collectionService.addCollections(collections);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
// endregion
// region POST
@PostMapping(value="/modifyCollectionName", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void modifyCollectionName(@RequestParam("collection") Collect collection, @RequestParam("name") String name){
collectionService.modifyCollectionName(collection,name);
public @ResponseBody EntityModel<Collect> modifyCollectionName(@RequestParam("collection") Collect collection, @RequestParam("name") String name){
Collect results = collectionService.modifyCollectionName(collection,name);
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 void modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){
collectionService.modifyCollectionNameById(id,name);
public @ResponseBody EntityModel<Collect> modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") 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 List<Article> getAllArticlesById(@PathVariable(value = "id") Integer id){
public @ResponseBody EntityModel<List<Article>> getAllArticlesById(@PathVariable(value = "id") Integer id){
List<Article> results = collectionService.getAllArticlesById(id);
if(results == null) {
throw new ArticleException("No articles available");
}
return results;
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
}
@PutMapping(value = "/addArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
collectionService.addArticle(collection,article);
public @ResponseBody EntityModel<Collect> addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
Collect results = collectionService.addArticle(collection,article);
Article art = results.getAllArticles().get(results.getAllArticles().indexOf(article));
return EntityModel.of(results,
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 void deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
collectionService.deleteArticle(collection,article);
public @ResponseBody EntityModel<Collect> deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
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
}

@ -73,4 +73,10 @@ public class Collect {
this.name = name;
}
// endregion
// region teacher
public Teacher getTeacher(){
return teacher;
}
// endregion
}

@ -8,14 +8,15 @@ public interface ICollectionService{
public List<Collect> getAllCollections();
public Collect getCollectionById(Integer id);
public List<Collect> getAllCollectionsByName(String name);
public void deleteColletionById(Integer id);
public void deleteColletionByName(String name);
public void deleteAllColletionByName(String name);
public void addCollection(Collect collection);
public void addCollections(List<Collect> collection);
public void modifyCollectionName(Collect collection, String name);
public void modifyCollectionNameById(Integer id, String name);
public Collect getCollection(Collect collect);
public boolean deleteColletionById(Integer id);
public boolean deleteColletionByName(String name);
public boolean deleteAllColletionByName(String name);
public Collect addCollection(Collect collection);
public List<Collect> addCollections(List<Collect> collection);
public Collect modifyCollectionName(Collect collection, String name);
public Collect modifyCollectionNameById(Integer id, String name);
public List<Article> getAllArticlesById(Integer id);
public void addArticle(Collect collection, Article article);
public void deleteArticle(Collect collection, Article article);
public Collect addArticle(Collect collection, Article article);
public Collect deleteArticle(Collect collection, Article article);
}

@ -9,7 +9,6 @@ import java.util.Objects;
import SAE.ApiREST.WebService.model.Article;
import org.springframework.stereotype.Service;
import SAE.ApiREST.WebService.model.Collect;
@Service
public class StubCollectionService implements ICollectionService {
@ -55,50 +54,72 @@ public class StubCollectionService implements ICollectionService {
}
return repCollections;
}
@Override
public Collect getCollection(Collect collect){
return getCollectionById(collect.getId());
}
// endregion
// region DELETE
@Override
public void deleteColletionById(Integer id){
public boolean deleteColletionById(Integer id){
Collect collection = getCollectionById(id);
collects.remove(collection);
if (collection != null) {
collects.remove(collection);
return true;
}
return false;
}
@Override
public void deleteColletionByName(String name){
public boolean deleteColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name);
collects.remove(collectionsByName.get(0));
if (!collectionsByName.isEmpty()) {
collects.remove(collectionsByName.get(0));
return true;
}
return false;
}
@Override
public void deleteAllColletionByName(String name){
public boolean deleteAllColletionByName(String name){
List<Collect> collectionsByName = getAllCollectionsByName(name);
collects.removeAll(collectionsByName);
if (!collectionsByName.isEmpty()) {
collects.removeAll(collectionsByName);
return true;
}
return false;
}
@Override
public void addCollection(Collect collection) {
public Collect addCollection(Collect collection) {
collects.add(collection);
return collects.get(collects.lastIndexOf(collection));
}
// endregion
// region PUT
@Override
public void addCollections(List<Collect> collections){
public List<Collect> addCollections(List<Collect> collections){
collects.addAll(collections);
return collects;
}
// endregion
// region POST
@Override
public void modifyCollectionName(Collect collection, String name){
collection.setName(name);
public Collect modifyCollectionName(Collect collection, String name){
Collect collect = getCollectionById(collection.getId());
collect.setName(name);
return collect;
}
@Override
public void modifyCollectionNameById(Integer id, String name){
Collect collection = getCollectionById(id);
modifyCollectionName(collection,name);
public Collect modifyCollectionNameById(Integer id, String name){
Collect collect = getCollectionById(id);
collect.setName(name);
return collect;
}
// endregion
// endregion
@ -114,12 +135,16 @@ public class StubCollectionService implements ICollectionService {
return null;
}
@Override
public void addArticle(Collect collect, Article article){
collect.addArticle(article);
public Collect addArticle(Collect collect, Article article){
Collect collection = getCollectionById(collect.getId());
collection.addArticle(article);
return collection;
}
@Override
public void deleteArticle(Collect collect, Article article){
collect.removeArticle(article);
public Collect deleteArticle(Collect collect, Article article){
Collect collection = getCollectionById(collect.getId());
collection.removeArticle(article);
return collection;
}
// endregion
}
Loading…
Cancel
Save