diff --git a/WebService/pom.xml b/WebService/pom.xml index 56b8716..472a887 100644 --- a/WebService/pom.xml +++ b/WebService/pom.xml @@ -1,7 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot @@ -22,13 +21,15 @@ org.springframework.boot spring-boot-starter - org.springframework.boot spring-boot-starter-test test - + + org.springframework.boot + spring-boot-starter-hateoas + org.springframework.boot spring-boot-starter-web @@ -38,14 +39,12 @@ jakarta.persistence-api - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java index 6ffeac5..f0e2da2 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java @@ -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 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 getAllCollectionsByName(@PathVariable(value = "name") String name){ + public @ResponseBody EntityModel> getAllCollectionsByName(@PathVariable(value = "name") String name){ List 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 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 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 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 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 collections){ - collectionService.addCollections(collections); + public @ResponseBody EntityModel> addCollections(@RequestParam("collections") List collections){ + List 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 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 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
getAllArticlesById(@PathVariable(value = "id") Integer id){ + public @ResponseBody EntityModel> getAllArticlesById(@PathVariable(value = "id") Integer id){ List
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 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 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 } diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java index 161a4c2..88669d4 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collect.java @@ -73,4 +73,10 @@ public class Collect { this.name = name; } // endregion + + // region teacher + public Teacher getTeacher(){ + return teacher; + } + // endregion } \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java index e2a0813..b6decc7 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java @@ -8,14 +8,15 @@ public interface ICollectionService{ public List getAllCollections(); public Collect getCollectionById(Integer id); public List 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 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 addCollections(List collection); + public Collect modifyCollectionName(Collect collection, String name); + public Collect modifyCollectionNameById(Integer id, String name); public List
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); } \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java index c2bcff3..c4bc093 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java @@ -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 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 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 collections){ + public List addCollections(List 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 } \ No newline at end of file