ajout HATEOS et manque 2 tests

Maxime
Maxime POINT 1 year ago
parent 194eff0552
commit e2cf019437

@ -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,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<Link> deleteColletionById(@RequestParam("id") Integer id){
public @ResponseBody EntityModel<Link> 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<Link> deleteColletionByName(@RequestParam("name") String name){
public @ResponseBody EntityModel<Link> 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<Link> deleteAllColletionByName(@RequestParam("name") String name){
public @ResponseBody EntityModel<Link> 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<Collect> addCollection(@RequestParam("collection") Collect collection){
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(),
@ -114,32 +118,40 @@ public class CollectController {
}
@PutMapping(value = "/addCollections")
public @ResponseBody EntityModel<List<Collect>> addCollections(@RequestParam("collections") List<Collect> collections){
public @ResponseBody CollectionModel<EntityModel<Collect>> addCollections(@RequestBody List<Collect> collections){
List<Collect> results = collectionService.addCollections(collections);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
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(@RequestParam("collection") Collect collection, @RequestParam("name") String name){
Collect results = collectionService.modifyCollectionName(collection,name);
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"));
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(@RequestParam("id") Integer id, @RequestParam("name") String name){
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(),
@ -157,26 +169,37 @@ public class CollectController {
// region Article
@GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody EntityModel<List<Article>> getAllArticlesById(@PathVariable(value = "id") Integer id){
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");
}
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
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<Collect> addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
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(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<Collect> deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
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(),

Loading…
Cancel
Save