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