From f99c5862204a527754412fed3d8a4232ee3849e0 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 Feb 2024 11:40:12 +0100 Subject: [PATCH 1/2] ajout Model Article --- WebService/pom.xml | 82 +++++++++++-------- .../WebService/CollectionControlleur.java | 37 +++++++++ .../SAE/ApiREST/WebService/Data/Article.java | 2 + .../ApiREST/WebService/Data/Collection.java | 44 ++++++++++ 4 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java diff --git a/WebService/pom.xml b/WebService/pom.xml index 9f379f5..0ed24c0 100644 --- a/WebService/pom.xml +++ b/WebService/pom.xml @@ -1,41 +1,51 @@ +import org.springframework.web.bind.annotation.*; - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.2.3 - - - SAE.ApiREST - WebService - 0.0.1-SNAPSHOT - WebService - Demo project for Spring Boot - - 17 - - - - org.springframework.boot - spring-boot-starter - + 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 + spring-boot-starter-parent + 3.2.2 + + + com.example + WebService + 0.0.1-SNAPSHOT + tp2 + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter + - - org.springframework.boot - spring-boot-starter-test - test - - + + org.springframework.boot + spring-boot-starter-test + test + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + org.springframework.boot + spring-boot-starter-web + + + jakarta.persistence + jakarta.persistence-api + + - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java b/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java new file mode 100644 index 0000000..2e17cad --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java @@ -0,0 +1,37 @@ +@org.springframework.stereotype.Controller + +@Controller +@RequestMapping("/CollectionWebService") +public class CollectionController { + private ArrayList books = new ArrayList<>(); + + public Controller() { + books.add(new Book("titre1", "moi")); + books.add(new Book("titre2", "toi")); + books.add(new Book("titre3", "eux")); + books.add(new Book("titre4", "tout ceux qui le veule")); + } + + @GetMapping(value = "/getAllBooks", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody List getAllBooks() { + return books; + } + + @GetMapping(value = "/getBookById/{isbn}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody Book getBookById(@PathVariable(value = "isbn") int isbn) { + for(Book book : books) { + if(book.isbn == isbn) { + return book; + } + } + throw new BookException("Undefined id"); + } + + @PostMapping("/logBooks") + public @ResponseBody void logBooks(@RequestParam("title") String title, @RequestParam("author") String author) { + books.add(new Book(title, author)); + for(Book book : books) { + System.out.println(book); + } + } +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java b/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java new file mode 100644 index 0000000..58a2c90 --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java @@ -0,0 +1,2 @@ +@Entity +public class Article \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java b/WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java new file mode 100644 index 0000000..3f85887 --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java @@ -0,0 +1,44 @@ +package SAE.ApiREST.WebService.Data; +@Entity +public class Collection{ + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private final long isbn + @Column(name = "articles") + private ArrayList
articles + @Column(name = "name") + private String name + + public Collection(String name){ + this.name = name; + this.articles = new ArrayList
(); + } + + public List
getAllArticles(){ + return articles; + } + + public void addArticle(Article article){ + if(!this.articles.contains(article)){ + this.articles.add(article); + } + } + public void addArticles(List
articles){ + for(Article article : articles){ + addArticle(article); + } + } + public void removeArticle(Article article){ + this.articles.remove(article); + } + public void removeArticles(List
articles){ + this.articles.removeAll(articles); + } + + public String getName(){ + return name; + } + public void setName(String name){ + this.name = name; + } +} \ No newline at end of file From c23c0aa7c0b83eb4e27fa340a328e7e3ed2cae51 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 Feb 2024 15:59:17 +0100 Subject: [PATCH 2/2] Ajout service Collection --- .../WebService/CollectionControlleur.java | 37 ------ .../SAE/ApiREST/WebService/Data/Article.java | 2 - .../controller/CollectionControlleur.java | 107 ++++++++++++++++++ .../{Data => model}/Collection.java | 20 +++- .../service/ICollectionService.java | 15 +++ .../service/StubCollectionService.java | 87 ++++++++++++++ 6 files changed, 228 insertions(+), 40 deletions(-) delete mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java delete mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectionControlleur.java rename WebService/src/main/java/SAE/ApiREST/WebService/{Data => model}/Collection.java (72%) create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java create mode 100644 WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java b/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java deleted file mode 100644 index 2e17cad..0000000 --- a/WebService/src/main/java/SAE/ApiREST/WebService/CollectionControlleur.java +++ /dev/null @@ -1,37 +0,0 @@ -@org.springframework.stereotype.Controller - -@Controller -@RequestMapping("/CollectionWebService") -public class CollectionController { - private ArrayList books = new ArrayList<>(); - - public Controller() { - books.add(new Book("titre1", "moi")); - books.add(new Book("titre2", "toi")); - books.add(new Book("titre3", "eux")); - books.add(new Book("titre4", "tout ceux qui le veule")); - } - - @GetMapping(value = "/getAllBooks", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody List getAllBooks() { - return books; - } - - @GetMapping(value = "/getBookById/{isbn}", produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody Book getBookById(@PathVariable(value = "isbn") int isbn) { - for(Book book : books) { - if(book.isbn == isbn) { - return book; - } - } - throw new BookException("Undefined id"); - } - - @PostMapping("/logBooks") - public @ResponseBody void logBooks(@RequestParam("title") String title, @RequestParam("author") String author) { - books.add(new Book(title, author)); - for(Book book : books) { - System.out.println(book); - } - } -} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java b/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java deleted file mode 100644 index 58a2c90..0000000 --- a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Article.java +++ /dev/null @@ -1,2 +0,0 @@ -@Entity -public class Article \ No newline at end of file diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectionControlleur.java b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectionControlleur.java new file mode 100644 index 0000000..6060b3f --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/controller/CollectionControlleur.java @@ -0,0 +1,107 @@ +@org.springframework.stereotype.Controller + +@Controller +@RequestMapping("/CollectionWebService") +public class CollectionController { + private ArrayList collections = new ArrayList<>(); + + public Controller() { + + } + + @GetMapping(value = "/getAllCollection", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody List getAllCollection() { + return collections; + } + + @GetMapping(value = "/getCollectionById/{isbn}", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody Book getCollectionById(@PathVariable(value = "isbn") int isbn) { + for(Book book : books) { + if(book.isbn == isbn) { + return book; + } + } + throw new BookException("Undefined id"); + } + + @PostMapping("/logBooks") + public @ResponseBody void logBooks(@RequestParam("title") String title, @RequestParam("author") String author) { + books.add(new Book(title, author)); + for(Book book : books) { + System.out.println(book); + } + } + // region Collection + + // region GET + @GetMapping(value = "/getCollectionById", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody Collection getCollectionById(long isbn){ + for(Collection collection : this.collections){ + if(collection.getId === isbn) { + return collection; + } + } + return null; + } + @GetMapping(value = "/getAllCollectionsByName", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody List getAllCollectionsByName(String name){ + private ArrayList repCollections = new ArrayList(); + for(Collection collection : this.collections){ + if(collection.getName === name) { + repCollections.add(collection); + } + } + return repCollections; + } + // endregion + + // region DELETE + @DeleteMaping(value = "/deleteColletionById") + public @ResponseBody void deleteColletionById(long isbn){ + Collection collection = getCollectionById(isbn); + this.collections.remove(collection); + } + + public @ResponseBody void deleteColletionByName(String name){ + ArrayList collectionsByName = getAllCollectionsByName(name); + this.collections.remove(collectionsByName[0]); + } + public @ResponseBody void deleteAllColletionByName(String name){ + ArrayList collectionsByName = getAllCollectionsByName(name); + this.collections.removeAll(collectionsByName); + } + // endregion + + // region PUT + public @ResponseBody void addCollection(Collection collection){ + this.collections.add(collection); + } + public @ResponseBody void addCollections(List collections){ + this.collections.addAll(collections); + } + // endregion + + // region POST + public @ResponseBody void modifyCollectionName(Collection collection, String name){ + collection.setName(name); + } + public @ResponseBody void modifyCollectionNameById(long isbn, String name){ + Collection collection = getCollectionById(isbn); + modifyCollectionName(collection,name); + } + // endregion + // endregion + + // region Article + @GetMapping(value = "/getAllArticles", produces = MediaType.APPLICATION_JSON_VALUE) + public @ResponseBody List
getAllArticles(Collection collection){ + return collection.getAllArticles; + } + public @ResponseBody void addArticle(Collection collection, Article article){ + collection.addArticle + } + public @ResponseBody void deleteArticle(Collection collection, Article article){ + collection.deleteArticle(article); + } + // endregion +} diff --git a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collection.java similarity index 72% rename from WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java rename to WebService/src/main/java/SAE/ApiREST/WebService/model/Collection.java index 3f85887..bf1f6dc 100644 --- a/WebService/src/main/java/SAE/ApiREST/WebService/Data/Collection.java +++ b/WebService/src/main/java/SAE/ApiREST/WebService/model/Collection.java @@ -8,16 +8,27 @@ public class Collection{ private ArrayList
articles @Column(name = "name") private String name + @Column(name = "teacher") + private Teacher teacher - public Collection(String name){ + public Collection(String name, Teacher teacher){ this.name = name; + this.teacher = teacher; this.articles = new ArrayList
(); } + // region Article + public long getId(){ + return isbn; + } + // endregion + + // region Article public List
getAllArticles(){ return articles; } + // region addArticle public void addArticle(Article article){ if(!this.articles.contains(article)){ this.articles.add(article); @@ -28,17 +39,24 @@ public class Collection{ addArticle(article); } } + // endregion + + // region removeArticle public void removeArticle(Article article){ this.articles.remove(article); } public void removeArticles(List
articles){ this.articles.removeAll(articles); } + // endregion + // endregion + // region name public String getName(){ return name; } public void setName(String name){ this.name = name; } + // 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 new file mode 100644 index 0000000..6b82362 --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/ICollectionService.java @@ -0,0 +1,15 @@ +public interface ICollectionService{ + public List getAllCollections(); + public Collection getCollectionById(long isbn); + public List getAllCollectionsByName(String name); + public void deleteColletionById(long isbn); + public void deleteColletionByName(String name); + public void deleteAllColletionByName(String name); + public void addCollection(Collection collection); + public void addCollections(List collection); + public void modifyCollectionName(Collection collection, String name); + public void modifyCollectionNameById(long isbn, String name); + public List
getAllArticles(Collection collection); + public void addArticle(Collection collection, Article article); + public void deleteArticle(Collection 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 new file mode 100644 index 0000000..a335f5f --- /dev/null +++ b/WebService/src/main/java/SAE/ApiREST/WebService/service/StubCollectionService.java @@ -0,0 +1,87 @@ +package SAE.ApiREST.WebService.service; + +import java.sql.Date; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import SAE.ApiREST.WebService.model.Article; + +@Service +public class CollectionService implements ICollectionService { + private ArrayList collections; + public List getAllCollections() { + return this.collections; + } + + // region Collection + + // region GET + public Collection getCollectionById(long isbn){ + for(Collection collection : this.collections){ + if(collection.getId === isbn) { + return collection; + } + } + return null; + } + public List getAllCollectionsByName(String name){ + private ArrayList repCollections = new ArrayList(); + for(Collection collection : this.collections){ + if(collection.getName === name) { + repCollections.add(collection); + } + } + return repCollections; + } + // endregion + + // region DELETE + public void deleteColletionById(long isbn){ + Collection collection = getCollectionById(isbn); + this.collections.remove(collection); + } + + public void deleteColletionByName(String name){ + ArrayList collectionsByName = getAllCollectionsByName(name); + this.collections.remove(collectionsByName[0]); + } + public void deleteAllColletionByName(String name){ + ArrayList collectionsByName = getAllCollectionsByName(name); + this.collections.removeAll(collectionsByName); + } + // endregion + + // region PUT + public void addCollection(Collection collection){ + this.collections.add(collection); + } + public void addCollections(List collections){ + this.collections.addAll(collections); + } + // endregion + + // region POST + public void modifyCollectionName(Collection collection, String name){ + collection.setName(name); + } + public void modifyCollectionNameById(long isbn, String name){ + Collection collection = getCollectionById(isbn); + modifyCollectionName(collection,name); + } + // endregion + // endregion + + // region Article + public List
getAllArticles(Collection collection){ + return collection.getAllArticles; + } + public void addArticle(Collection collection, Article article){ + collection.addArticle + } + public void deleteArticle(Collection collection, Article article){ + collection.deleteArticle(article); + } + // endregion +} \ No newline at end of file