ajout HATOES

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

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -22,13 +21,15 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@ -38,14 +39,12 @@
<artifactId>jakarta.persistence-api</artifactId> <artifactId>jakarta.persistence-api</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<build> <plugins>
<plugins> <plugin>
<plugin> <groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
<artifactId>spring-boot-maven-plugin</artifactId> </plugin>
</plugin> </plugins>
</plugins> </build>
</build>
</project> </project>

@ -6,12 +6,17 @@ import SAE.ApiREST.WebService.model.Article;
import SAE.ApiREST.WebService.model.Collect; import SAE.ApiREST.WebService.model.Collect;
import SAE.ApiREST.WebService.service.ICollectionService; import SAE.ApiREST.WebService.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired; 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.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 java.util.List; 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("/CollectWebService") @RequestMapping("/CollectWebService")
public class CollectController { public class CollectController {
@ -34,84 +39,167 @@ public class CollectController {
return results; return results;
} }
@GetMapping(value = "/getCollectionById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @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); Collect results = collectionService.getCollectionById(id);
if(results == null) { if(results == null) {
throw new CollectException("No collections available"); 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) @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); List<Collect> results = collectionService.getAllCollectionsByName(name);
if(results.isEmpty()) { if(results.isEmpty()) {
throw new CollectException("No collections available"); 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 // endregion
// region DELETE // region DELETE
@DeleteMapping(value = "/deleteColletionById/{id}") @DeleteMapping(value = "/deleteColletionById/{id}")
public @ResponseBody void deleteColletionById(@RequestParam("id") Integer id){ public @ResponseBody EntityModel<Link> deleteColletionById(@RequestParam("id") Integer id){
collectionService.deleteColletionById(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}") @DeleteMapping(value = "/deleteColletionByName/{name}")
public @ResponseBody void deleteColletionByName(@RequestParam("name") String name){ public @ResponseBody EntityModel<Link> deleteColletionByName(@RequestParam("name") String name){
collectionService.deleteColletionByName(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}") @DeleteMapping(value = "/deleteAllColletionByName/{name}")
public @ResponseBody void deleteAllColletionByName(@RequestParam("name") String name){ public @ResponseBody EntityModel<Link> deleteAllColletionByName(@RequestParam("name") String name){
collectionService.deleteAllColletionByName(name); if(!collectionService.deleteAllColletionByName(name)) {
throw new CollectException("No collections available");
}
return EntityModel.of(linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
} }
// endregion // endregion
// region PUT // region PUT
@PutMapping(value = "/addCollection") @PutMapping(value = "/addCollection")
public @ResponseBody void addCollection(@RequestParam("collection") Collect collection){ public @ResponseBody EntityModel<Collect> addCollection(@RequestParam("collection") Collect collection){
collectionService.addCollection(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") @PutMapping(value = "/addCollections")
public @ResponseBody void addCollections(@RequestParam("collections") List<Collect> collections){ public @ResponseBody EntityModel<List<Collect>> addCollections(@RequestParam("collections") List<Collect> collections){
collectionService.addCollections(collections); List<Collect> results = collectionService.addCollections(collections);
return EntityModel.of(results,
linkTo(methodOn(CollectController.class).getAllCollection()
).withRel("getAllCollection"));
} }
// endregion // endregion
// region POST // region POST
@PostMapping(value="/modifyCollectionName", produces = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value="/modifyCollectionName", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void modifyCollectionName(@RequestParam("collection") Collect collection, @RequestParam("name") String name){ public @ResponseBody EntityModel<Collect> modifyCollectionName(@RequestParam("collection") Collect collection, @RequestParam("name") String name){
collectionService.modifyCollectionName(collection,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) @PostMapping(value="/modifyCollectionNameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){ public @ResponseBody EntityModel<Collect> modifyCollectionNameById(@RequestParam("id") Integer id, @RequestParam("name") String name){
collectionService.modifyCollectionNameById(id,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
// endregion // endregion
// region Article // region Article
@GetMapping(value = "/getAllArticlesById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @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); List<Article> results = collectionService.getAllArticlesById(id);
if(results == null) { if(results == null) {
throw new ArticleException("No articles available"); 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) @PutMapping(value = "/addArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){ public @ResponseBody EntityModel<Collect> addArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
collectionService.addArticle(collection,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) @DeleteMapping(value = "/deleteArticle", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){ public @ResponseBody EntityModel<Collect> deleteArticle(@RequestParam("collection") Collect collection, @RequestParam("article") Article article){
collectionService.deleteArticle(collection,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 // endregion
} }

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

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