From f99c5862204a527754412fed3d8a4232ee3849e0 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 26 Feb 2024 11:40:12 +0100 Subject: [PATCH] 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