ajout Model Article

pull/1/head
Maxime POINT 1 year ago
parent cad18cf306
commit f99c586220

@ -1,41 +1,51 @@
import org.springframework.web.bind.annotation.*;
<?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>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version> <version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>SAE.ApiREST</groupId> <groupId>com.example</groupId>
<artifactId>WebService</artifactId> <artifactId>WebService</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>WebService</name> <name>tp2</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<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>
</dependencies>
<build> <dependency>
<plugins> <groupId>org.springframework.boot</groupId>
<plugin> <artifactId>spring-boot-starter-web</artifactId>
<groupId>org.springframework.boot</groupId> </dependency>
<artifactId>spring-boot-maven-plugin</artifactId> <dependency>
</plugin> <groupId>jakarta.persistence</groupId>
</plugins> <artifactId>jakarta.persistence-api</artifactId>
</build> </dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project> </project>

@ -0,0 +1,37 @@
@org.springframework.stereotype.Controller
@Controller
@RequestMapping("/CollectionWebService")
public class CollectionController {
private ArrayList<Collection> 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<Book> 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);
}
}
}

@ -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<Article> articles
@Column(name = "name")
private String name
public Collection(String name){
this.name = name;
this.articles = new ArrayList<Article>();
}
public List<Article> getAllArticles(){
return articles;
}
public void addArticle(Article article){
if(!this.articles.contains(article)){
this.articles.add(article);
}
}
public void addArticles(List<Article> articles){
for(Article article : articles){
addArticle(article);
}
}
public void removeArticle(Article article){
this.articles.remove(article);
}
public void removeArticles(List<Article> articles){
this.articles.removeAll(articles);
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
Loading…
Cancel
Save