diff --git a/backend/src/main/java/com/bookstore/backend/BackendApplication.java b/backend/src/main/java/com/bookstore/backend/BackendApplication.java index dde31e4a..0af2e8b2 100644 --- a/backend/src/main/java/com/bookstore/backend/BackendApplication.java +++ b/backend/src/main/java/com/bookstore/backend/BackendApplication.java @@ -264,7 +264,7 @@ public void run(String... args) throws Exception { for(AdminModel admin: adminList) { System.out.println(admin.toString()); } - System.out.println("enter the admin ID"); + System.out.println("enter the admin ID (s to exit)"); String option = input.nextLine(); clearConsole(); @@ -292,12 +292,12 @@ public void run(String... args) throws Exception { } }else if(op.equals("3")){ - Listadmins = adminRepositoryService.getInstance().findAll(); - for(AdminModel admin : admins) { - System.out.println(admin.toString()); - } - while(true){ + Listadmins = adminRepositoryService.getInstance().findAll(); + for(AdminModel admin : admins) { + System.out.println(admin.toString()); + } + System.out.println("Type the ID admin(Type s for exit): "); String id = input.nextLine(); if(id.equals("s")){ @@ -346,7 +346,7 @@ else if(op.equals("s")){ for(AuthorModel author : authors) { System.out.println(author.toString()); } - System.out.println("enter the author ID"); + System.out.println("enter the author ID (s to exit)"); String option = input.nextLine(); clearConsole(); @@ -416,13 +416,13 @@ else if(op.equals("s")){ clearConsole(); }else if(op.equals("3")){ - System.out.println("List of Categories"); - List categoryList = categoryRepositoryService.getInstance().findAll(); - for(CategoryModel category : categoryList) { - System.out.println(category.toString()); - } - while(true){ + System.out.println("List of Categories"); + List categoryList = categoryRepositoryService.getInstance().findAll(); + for(CategoryModel category : categoryList) { + System.out.println(category.toString()); + } + System.out.println("Type the ID Category(Type s for exit): "); String id = input.nextLine(); clearConsole(); @@ -439,7 +439,7 @@ else if(op.equals("s")){ for(CategoryModel category : categorys) { System.out.println(category.toString()); } - System.out.println("enter the category ID"); + System.out.println("enter the category ID (s to exit)"); String option = input.nextLine(); clearConsole(); @@ -492,7 +492,7 @@ else if(op.equals("s")){ for(PublishingCompanyModel company : companys) { System.out.println(company.toString()); } - System.out.println("enter the company ID"); + System.out.println("enter the company ID (s to exit)"); String option = input.nextLine(); clearConsole(); @@ -512,12 +512,12 @@ else if(op.equals("s")){ } }else if(op.equals("4")){ - Listcompanys = companyRepositoryService.getInstance().findAll(); - for(PublishingCompanyModel company : companys) { - System.out.println(company.toString()); - } - while(true){ + Listcompanys = companyRepositoryService.getInstance().findAll(); + for(PublishingCompanyModel company : companys) { + System.out.println(company.toString()); + } + System.out.println("Type the ID company(Type s for exit): "); String id = input.nextLine(); clearConsole(); @@ -536,36 +536,48 @@ else if(op.equals("s")){ } }else if(op.equals("6")){ - shoppingCartModel shoppingCart = new shoppingCartModel(0l, null, userModel); + shoppingCartModel shoppingCart = userModel.getShoppingCart(); + + if(shoppingCart == null) { + shoppingCart = new shoppingCartModel(0l, null, userModel); + } + // Carrinho carrinho = ; while(true){ System.out.println("ShoppingCart options:"); System.out.println("What option do you desire?"); - System.out.println("1 - remove\n2 - add\n3 - exit: "); + System.out.println("1 - remove\n2 - add\ns - exit: "); op = input.nextLine(); clearConsole(); //opção para remover livro do carrinho if(op.equals("1")){ - shoppingCart = shoppingCartRepositoryService.getInstance().findById(userModel.getShoppingCart().getId()).get(); - while(true){ - System.out.println("Items Oders:"); - for(ItemOrderModel item: shoppingCart.getItemList()){ - System.out.println(item.getProduct().toString() + - " Amount: " + item.getAmount()+ - " Price: " + item.getTotalPrice()); - } - - System.out.print("Id Book for remove(Type s for exit): "); - String id = input.nextLine(); - clearConsole(); - - if(id.equals("s")){ + if(userModel.getShoppingCart() != null) { + shoppingCart = shoppingCartRepositoryService.getInstance().findById(userModel.getShoppingCart().getId()).get(); + while(true){ + System.out.println("Items Oders:"); + for(ItemOrderModel item: shoppingCart.getItemList()){ + System.out.println(item.getProduct().toString() + + " Amount: " + item.getAmount()+ + " Price: " + item.getTotalPrice()); + } + + System.out.print("Id Book for remove(Type s for exit): "); + String id = input.nextLine(); clearConsole(); - break; + + if(id.equals("s")){ + shoppingCartRepositoryService.getInstance().deleteById(shoppingCart.getId()); + clearConsole(); + break; + } + shoppingCart.removeItemOrderFromItemListByProductId(Long.parseLong(id)); } - shoppingCart.removeItemOrderFromItemListByProductId(Long.parseLong(id)); + }else { + System.out.println("Carrinho vazio"); + input.nextLine(); + clearConsole(); } //opção para adionar livro no carrinho @@ -598,7 +610,9 @@ else if(op.equals("s")){ } if(list.size() > 0) { shoppingCart.setItemList(list); - shoppingCartRepositoryService.getInstance().save(shoppingCart); + shoppingCart = shoppingCartRepositoryService.getInstance().save(shoppingCart); + userModel.setShoppingCart(shoppingCart); + userRepositoryService.getInstance().save(userModel); } clearConsole(); break; @@ -638,12 +652,12 @@ else if(op.equals("s")){ clearConsole(); System.out.println("Escolha o autor:"); // percorre a lista de autores. - List listAuthors = authorRepositoryService.getInstance().findAll(); - for(AuthorModel author : listAuthors) { - System.out.println(author.toString()); - } List authorList = new ArrayList<>(); while(true){ + List listAuthors = authorRepositoryService.getInstance().findAll(); + for(AuthorModel author : listAuthors) { + System.out.println(author.toString()); + } System.out.println("Type the ID Author(Type s for exit): "); String id = input.nextLine(); clearConsole(); @@ -662,12 +676,13 @@ else if(op.equals("s")){ System.out.println("pages: "); int pages = Integer.parseInt(input.nextLine()); clearConsole(); - List categoryList = categoryRepositoryService.getInstance().findAll(); - for(CategoryModel category : categoryList) { - System.out.println(category.toString()); - } List cateList = new ArrayList<>(); while(true){ + List categoryList = categoryRepositoryService.getInstance().findAll(); + for(CategoryModel category : categoryList) { + System.out.println(category.toString()); + } + System.out.println("Type the ID Category(Type s for exit): "); String id = input.nextLine(); clearConsole(); @@ -691,7 +706,7 @@ else if(op.equals("s")){ PublishingCompanyModel companyModel = companyRepositoryService.getInstance().findById(companyId).get(); System.out.println("Inventory: "); int qtd = Integer.parseInt(input.nextLine()); - BookModel book = new BookModel(0l, title, description, yearLaunch, pages, price, null, null, categoryList, userModel, companyModel, authorList, null); + BookModel book = new BookModel(0l, title, description, yearLaunch, pages, price, null, null, cateList, userModel, companyModel, authorList, null); book = bookRepositoryService.getInstance().save(book); InventoryModel inventory = new InventoryModel(0l, qtd, book, null); inventoryRepositoryService.getInstance().save(inventory); @@ -974,7 +989,7 @@ public UserModel login() { String password = input.nextLine(); clearConsole(); - UserModel user = new UserModel(0l, username, email, password, null, null, null, null, null); + UserModel user = new UserModel(0l, username, email, password, null, null, null, new shoppingCartModel(), null); userModel = userRepositoryService.getInstance().save(user); clearConsole(); diff --git a/backend/src/main/java/com/bookstore/backend/domain/model/product/BookModel.java b/backend/src/main/java/com/bookstore/backend/domain/model/product/BookModel.java index 4feea4d9..d0390dc4 100644 --- a/backend/src/main/java/com/bookstore/backend/domain/model/product/BookModel.java +++ b/backend/src/main/java/com/bookstore/backend/domain/model/product/BookModel.java @@ -30,6 +30,6 @@ public BookModel() { @Override public String toString() { - return String.format("BOOK [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: %s]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage())); + return String.format("BOOK [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: [AVERAGE: %s - AVALUATE NUMBER: %s]]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage()), String.valueOf(getEvaluateList().size())); } } diff --git a/backend/src/main/java/com/bookstore/backend/domain/model/product/HqModel.java b/backend/src/main/java/com/bookstore/backend/domain/model/product/HqModel.java index 8488cdfa..115f59a3 100644 --- a/backend/src/main/java/com/bookstore/backend/domain/model/product/HqModel.java +++ b/backend/src/main/java/com/bookstore/backend/domain/model/product/HqModel.java @@ -30,6 +30,6 @@ public HqModel() { @Override public String toString() { - return String.format("HQ [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: %s]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage())); + return String.format("HQ [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: [AVERAGE: %s - AVALUATE NUMBER: %s]]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage()), String.valueOf(getEvaluateList().size())); } } diff --git a/backend/src/main/java/com/bookstore/backend/domain/model/product/MagazineModel.java b/backend/src/main/java/com/bookstore/backend/domain/model/product/MagazineModel.java index e085ebef..13b98bcb 100644 --- a/backend/src/main/java/com/bookstore/backend/domain/model/product/MagazineModel.java +++ b/backend/src/main/java/com/bookstore/backend/domain/model/product/MagazineModel.java @@ -30,6 +30,6 @@ public MagazineModel() { @Override public String toString() { - return String.format("MAGAZINE [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: %s]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage())); + return String.format("MAGAZINE [ID: %s - TITLE: %s - DESCRIPTION: %s - YEAR LAUNCH: %s - PAGES: %s - PRICE: %s - COMPANY: %s - EVALUATE: [AVERAGE: %s - AVALUATE NUMBER: %s]]", getId(), getTitle(), getDescription(), getYearLaunch(), getPages(), getPrice().toString(), getCompany().getName(), String.valueOf(calculateStarAverage()), String.valueOf(getEvaluateList().size())); } } diff --git a/backend/src/main/java/com/bookstore/backend/domain/model/sale/shoppingCartModel.java b/backend/src/main/java/com/bookstore/backend/domain/model/sale/shoppingCartModel.java index a4860ccf..0c3e7cfd 100644 --- a/backend/src/main/java/com/bookstore/backend/domain/model/sale/shoppingCartModel.java +++ b/backend/src/main/java/com/bookstore/backend/domain/model/sale/shoppingCartModel.java @@ -5,6 +5,7 @@ import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -15,6 +16,9 @@ import com.bookstore.backend.domain.model.user.UserModel; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -32,8 +36,9 @@ public class shoppingCartModel { @Column(name = "ID") private Long Id; - @OneToMany + @OneToMany(fetch = FetchType.EAGER) @JoinColumn(name = "ITEMLIST_FK") + @Fetch(FetchMode.SUBSELECT) private List itemList; @OneToOne