diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/ArticleCollect.java b/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/ArticleCollect.java new file mode 100644 index 0000000..935453e --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/ArticleCollect.java @@ -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 +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/CollectionName.java b/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/CollectionName.java new file mode 100644 index 0000000..be043af --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/Wrapper/CollectionName.java @@ -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 +} 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 f0e2da2..0b09a24 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectController.java @@ -1,11 +1,14 @@ 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; @@ -13,6 +16,7 @@ import org.springframework.stereotype.Controller; 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; @@ -70,7 +74,7 @@ public class CollectController { // region DELETE @DeleteMapping(value = "/deleteColletionById/{id}") - public @ResponseBody EntityModel deleteColletionById(@RequestParam("id") Integer id){ + public @ResponseBody EntityModel deleteColletionById(@PathVariable("id") Integer id){ if(!collectionService.deleteColletionById(id)) { throw new CollectException("No collections available"); } @@ -79,7 +83,7 @@ public class CollectController { } @DeleteMapping(value = "/deleteColletionByName/{name}") - public @ResponseBody EntityModel deleteColletionByName(@RequestParam("name") String name){ + public @ResponseBody EntityModel deleteColletionByName(@PathVariable("name") String name){ if(!collectionService.deleteColletionByName(name)) { throw new CollectException("No collections available"); } @@ -88,7 +92,7 @@ public class CollectController { } @DeleteMapping(value = "/deleteAllColletionByName/{name}") - public @ResponseBody EntityModel deleteAllColletionByName(@RequestParam("name") String name){ + public @ResponseBody EntityModel deleteAllColletionByName(@PathVariable("name") String name){ if(!collectionService.deleteAllColletionByName(name)) { throw new CollectException("No collections available"); } @@ -99,7 +103,7 @@ public class CollectController { // region PUT @PutMapping(value = "/addCollection") - public @ResponseBody EntityModel addCollection(@RequestParam("collection") Collect collection){ + public @ResponseBody EntityModel addCollection(@RequestBody Collect collection){ Collect results = collectionService.addCollection(collection); return EntityModel.of(results, linkTo(methodOn(CollectController.class).getCollectionById(results.getId())).withSelfRel(), @@ -114,32 +118,40 @@ public class CollectController { } @PutMapping(value = "/addCollections") - public @ResponseBody EntityModel> addCollections(@RequestParam("collections") List collections){ + public @ResponseBody CollectionModel> addCollections(@RequestBody List collections){ List results = collectionService.addCollections(collections); - return EntityModel.of(results, - linkTo(methodOn(CollectController.class).getAllCollection() - ).withRel("getAllCollection")); + List> 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 modifyCollectionName(@RequestParam("collection") Collect collection, @RequestParam("name") String name){ - Collect results = collectionService.modifyCollectionName(collection,name); + public @ResponseBody EntityModel 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")); + 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 modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){ + public @ResponseBody EntityModel 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(), @@ -157,26 +169,37 @@ public class CollectController { // region Article @GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody EntityModel> getAllArticlesById(@PathVariable(value = "id") Integer id){ + public @ResponseBody CollectionModel> getAllArticlesById(@PathVariable(value = "id") Integer id){ List
results = collectionService.getAllArticlesById(id); if(results == null) { throw new ArticleException("No articles available"); } - - return EntityModel.of(results, - linkTo(methodOn(CollectController.class).getAllCollection() - ).withRel("getAllCollection")); + List> 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 addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){ + public @ResponseBody EntityModel
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(results, + 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(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()) @@ -188,7 +211,16 @@ public class CollectController { } @DeleteMapping(value = "/deleteArticle", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody EntityModel deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){ + public @ResponseBody EntityModel 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(),