Ajout service Collection

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

@ -1,37 +0,0 @@
@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,107 @@
@org.springframework.stereotype.Controller
@Controller
@RequestMapping("/CollectionWebService")
public class CollectionController {
private ArrayList<Collection> collections = new ArrayList<>();
public Controller() {
}
@GetMapping(value = "/getAllCollection", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Collection> 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<Collection> getAllCollectionsByName(String name){
private ArrayList<Collection> repCollections = new ArrayList<Collection>();
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<Collection> collectionsByName = getAllCollectionsByName(name);
this.collections.remove(collectionsByName[0]);
}
public @ResponseBody void deleteAllColletionByName(String name){
ArrayList<Collection> 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<Collection> 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<Article> 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
}

@ -8,16 +8,27 @@ public class Collection{
private ArrayList<Article> 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<Article>();
}
// region Article
public long getId(){
return isbn;
}
// endregion
// region Article
public List<Article> 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<Article> articles){
this.articles.removeAll(articles);
}
// endregion
// endregion
// region name
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
// endregion
}

@ -0,0 +1,15 @@
public interface ICollectionService{
public List<Collection> getAllCollections();
public Collection getCollectionById(long isbn);
public List<Collection> 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> collection);
public void modifyCollectionName(Collection collection, String name);
public void modifyCollectionNameById(long isbn, String name);
public List<Article> getAllArticles(Collection collection);
public void addArticle(Collection collection, Article article);
public void deleteArticle(Collection collection, Article article);
}

@ -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<Collection> collections;
public List<Collection> 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<Collection> getAllCollectionsByName(String name){
private ArrayList<Collection> repCollections = new ArrayList<Collection>();
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<Collection> collectionsByName = getAllCollectionsByName(name);
this.collections.remove(collectionsByName[0]);
}
public void deleteAllColletionByName(String name){
ArrayList<Collection> collectionsByName = getAllCollectionsByName(name);
this.collections.removeAll(collectionsByName);
}
// endregion
// region PUT
public void addCollection(Collection collection){
this.collections.add(collection);
}
public void addCollections(List<Collection> 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<Article> 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
}
Loading…
Cancel
Save