From a8fe405acca415347870f5b7ef2abf12bac582fb Mon Sep 17 00:00:00 2001 From: "koastiebratt@gmail.com" Date: Fri, 13 Nov 2020 14:00:20 -0500 Subject: [PATCH 1/4] Can add categories and products, no assocition yet --- pom.xml | 5 ++ .../controllers/AssociationController.java | 17 ++++++ .../controllers/CategoryController.java | 44 +++++++++++++++ .../controllers/ProductController.java | 35 +++++++++++- .../productcategories/models/Association.java | 34 ++++++++++++ .../productcategories/models/Category.java | 34 ++++++++++++ .../productcategories/models/Product.java | 43 +++++++++++++++ .../repositories/AssociationRepository.java | 9 ++- .../repositories/CategoryRepository.java | 11 +++- .../repositories/ProductRepository.java | 10 +++- .../services/AppService.java | 4 -- .../services/AssociationService.java | 21 +++++++ .../services/CategoryService.java | 52 ++++++++++++++++++ .../services/ProductService.java | 55 +++++++++++++++++++ src/main/resources/application.properties | 9 +-- .../WEB-INF/categories/categoriesindex.jsp | 2 +- .../webapp/WEB-INF/categories/newcategory.jsp | 6 +- src/main/webapp/WEB-INF/products/index.jsp | 2 +- .../webapp/WEB-INF/products/newproduct.jsp | 9 +-- 19 files changed, 379 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java delete mode 100644 src/main/java/com/dtcc/projects/productcategories/services/AppService.java create mode 100644 src/main/java/com/dtcc/projects/productcategories/services/AssociationService.java create mode 100644 src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java create mode 100644 src/main/java/com/dtcc/projects/productcategories/services/ProductService.java diff --git a/pom.xml b/pom.xml index 18e6419..4d8fa55 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,11 @@ + + org.sonatype.sisu + sisu-inject-bean + 1.4.2 + diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java index 50fb383..7372df3 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java @@ -1,4 +1,21 @@ package com.dtcc.projects.productcategories.controllers; +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.repositories.CategoryRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + + public class AssociationController { + @Autowired + private CategoryRepository categoryRepository; + +// @RequestMapping(value="/categories/createcategory", method= RequestMethod.POST) +// public void addNewCategory(@ModelAttribute("newcategory") Category category) { +// Category test = new Category(category.getValue()); +// categoryRepository.save(test); +// } + } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java new file mode 100644 index 0000000..9d4861d --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java @@ -0,0 +1,44 @@ +package com.dtcc.projects.productcategories.controllers; + +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.services.CategoryService; +import com.dtcc.projects.productcategories.services.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; +@Controller +public class CategoryController { + @Autowired + private CategoryService categoryService; + + @GetMapping(value = "/categories/category") + public String getCategoryIndex(Model model){ + List categoryList = categoryService.showAll(); + model.addAttribute("categories", categoryList); + return "categories/categoriesindex"; + } + + @GetMapping(value = "/categories/newcategory") + public String newcat(Model model){ + model.addAttribute("newcategory", new Category()); + return "categories/newcategory"; + } + + @PostMapping(value = "/categories/createcategory") + public String createCategory(@ModelAttribute("newcategory")Category cat){ + categoryService.saveCategory(cat); + return "redirect:/"; + } + +//@GetMapping("/products/newproduct")//.jsp file location/name +//public String newprod(Model model) { +// model.addAttribute("newproduct", new Product()); //Creating and inserting in JSp +// return "products/newproduct";//Returns .jsp page +//} +} diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java index 608f4e1..d52010a 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java @@ -1,4 +1,37 @@ package com.dtcc.projects.productcategories.controllers; -public class ProductController { + +import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.services.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Controller +public class ProductController{ + + @Autowired + private ProductService productService; + + @GetMapping(value = "/") + public String getIndexPage(Model model){ + List prdocutList = productService.showAll(); + model.addAttribute("products", prdocutList); + return "products/index"; + } + + @GetMapping("/products/newproduct")//.jsp file location/name + public String newprod(Model model) { + model.addAttribute("newproduct", new Product()); //Creating and inserting in JSp + return "products/newproduct";//Returns .jsp page + } + + @PostMapping(value = "/products/createproduct") //the action clause + public String createProduct(@ModelAttribute("newproduct") Product item){//action attribute + productService.saveProduct(item);//Defined service for DB + return "redirect:/";//Go back to main page + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/models/Association.java b/src/main/java/com/dtcc/projects/productcategories/models/Association.java index 651200e..dc96c07 100644 --- a/src/main/java/com/dtcc/projects/productcategories/models/Association.java +++ b/src/main/java/com/dtcc/projects/productcategories/models/Association.java @@ -1,4 +1,38 @@ package com.dtcc.projects.productcategories.models; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.Date; +import java.util.List; + +@Data +@Entity +@NoArgsConstructor +@Table(name = "categories_products") public class Association { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "PRODUCT_ID") + private Product productId; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "CATEGORY_ID") + private Category categoryId; + + @Column(name = "CREATED_AT") + private Date createdAt; + + @Column(name = "UPDATED_AT") + private Date updatedAt; + + public Association(Product productId, Category categoryId) { + this.productId = productId; + this.categoryId = categoryId; + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/models/Category.java b/src/main/java/com/dtcc/projects/productcategories/models/Category.java index 537da78..5531c7d 100644 --- a/src/main/java/com/dtcc/projects/productcategories/models/Category.java +++ b/src/main/java/com/dtcc/projects/productcategories/models/Category.java @@ -1,4 +1,38 @@ package com.dtcc.projects.productcategories.models; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.util.Date; +import java.util.List; + +@Data +@Entity +@NoArgsConstructor public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "CATEGORY_ID") + private Integer categoryId; + + @Column(name = "CATEGORY_NAME") + private String value; + + @Column(name = "CREATED_AT") + private Date created_at; + + @Column(name = "UPDATED_AT") + private Date updated_at; + + @ManyToMany + @JoinTable(name = "categories_products", joinColumns = @JoinColumn(name = "category_id"), inverseJoinColumns = @JoinColumn(name = "product_id")) + private List productList; + + public Category(String value, Date created_at, Date updated_at) { + this.value = value; + this.created_at = created_at; + this.updated_at = updated_at; + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/models/Product.java b/src/main/java/com/dtcc/projects/productcategories/models/Product.java index 731a0ae..5c0a1ca 100644 --- a/src/main/java/com/dtcc/projects/productcategories/models/Product.java +++ b/src/main/java/com/dtcc/projects/productcategories/models/Product.java @@ -1,4 +1,47 @@ package com.dtcc.projects.productcategories.models; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import java.time.LocalDate; +import java.util.Date; +import java.util.List; + +@Data +@Entity +@NoArgsConstructor public class Product { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "PRODUCT_ID") + private Integer productId; + + @Column(name = "PRDOCUT_NAME") + private String productName; + + @Column(name = "DESCRIPTION") + private String description; + + @Column(name = "PRICE") + private Double price; + + @Column(name = "CREATED_AT") + private Date created_at; + + @Column(name = "UPDATED_AT") + private Date updated_at; + + @ManyToMany + @JoinTable(name = "categories_products", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "category_id")) + private List categoryList; + + public Product(String productName, String description, Double price, Date created_at, Date updated_at) { + this.productName = productName; + this.description = description; + this.price = price; + this.created_at = created_at; + this.updated_at = updated_at; + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/AssociationRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/AssociationRepository.java index 2d1e948..7c48d59 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/AssociationRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/AssociationRepository.java @@ -1,4 +1,11 @@ package com.dtcc.projects.productcategories.repositories; -public interface AssociationRepository { +import com.dtcc.projects.productcategories.models.Association; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + + +@Repository +public interface AssociationRepository extends CrudRepository { + } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java index 18c3243..7ad9732 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java @@ -1,8 +1,15 @@ package com.dtcc.projects.productcategories.repositories; +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.models.Product; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + import java.util.List; -public interface CategoryRepository { +@Repository +public interface CategoryRepository extends CrudRepository { List findAll(); - List findByProductsNotContains(Product product); + //List findByProductsNotContains(Product product); + } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java index 3fdd521..ba9de75 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java @@ -1,8 +1,14 @@ package com.dtcc.projects.productcategories.repositories; +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.models.Product; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + import java.util.List; -public interface ProductRepository { +@Repository +public interface ProductRepository extends CrudRepository { List findAll(); - List findByCategoriesNotContains(Category category); + //List findByCategoriesNotContains(Category category); } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/AppService.java b/src/main/java/com/dtcc/projects/productcategories/services/AppService.java deleted file mode 100644 index 2bf08a8..0000000 --- a/src/main/java/com/dtcc/projects/productcategories/services/AppService.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.dtcc.projects.productcategories.services; - -public class AppService { -} diff --git a/src/main/java/com/dtcc/projects/productcategories/services/AssociationService.java b/src/main/java/com/dtcc/projects/productcategories/services/AssociationService.java new file mode 100644 index 0000000..1c185b7 --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/AssociationService.java @@ -0,0 +1,21 @@ +package com.dtcc.projects.productcategories.services; + +import com.dtcc.projects.productcategories.models.Association; +import com.dtcc.projects.productcategories.repositories.AssociationRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class AssociationService { + + private AssociationRepository associationRepository; + + @Autowired + public AssociationService(AssociationRepository associationRepository){ + this.associationRepository = associationRepository; + } + + public Association saveAssociation(Association association){ + return associationRepository.save(association); + } +} diff --git a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java new file mode 100644 index 0000000..59c73e2 --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java @@ -0,0 +1,52 @@ +package com.dtcc.projects.productcategories.services; + +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.repositories.CategoryRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +@Service +public class CategoryService { + + private CategoryRepository categoryRepository; + + @Autowired + public CategoryService(CategoryRepository categoryRepository){ + this.categoryRepository = categoryRepository; + } + + public List showAll(){ + return categoryRepository.findAll(); + } + + public Category findById(Long ID){ + return categoryRepository.findById(ID).orElse(null); + } + + public Category saveCategory(Category category){ + return categoryRepository.save(category); + } + + public Category updateProduct(Long id, String Name){ + Category category= findById(id); + if (Name!= null) { + category.setValue(Name); + } else { + category.setValue(category.getValue()); + } + category.setUpdated_at(new Date()); + return categoryRepository.save(category); + } + + public void deleteById(Long ID){ + categoryRepository.deleteById(ID); + } + +// public List findByProductsNotContains(Product product){ +// return categoryRepository.findByProductsNotContains(product); +// } +} diff --git a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java new file mode 100644 index 0000000..350e900 --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -0,0 +1,55 @@ +package com.dtcc.projects.productcategories.services; +import com.dtcc.projects.productcategories.models.Category; +import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.repositories.ProductRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.util.List; + +@Service +public class ProductService { + private ProductRepository productRepository; + @Autowired + public ProductService(ProductRepository productRepository) { + this.productRepository=productRepository; + } + + public List showAll(){ + return productRepository.findAll(); + } + + public Product findById(Long id){ + return productRepository.findById(id).orElse(null); + } + + public Product saveProduct(Product product){ + return productRepository.save(product); + } + + public Product updateProduct(Long id, String Name, String description, Double price){ + Product product= findById(id); + if (Name!= null) { + product.setProductName(Name); + } else { + product.setProductName(product.getProductName()); + } + if(description!= null){ + product.setDescription(description); + } else { + product.setDescription(product.getDescription()); + } + if(price!= null){ + product.setPrice(price); + } else { + product.setPrice(product.getPrice()); + } + return productRepository.save(product); + } + public void deleteById(Long id){ + productRepository.deleteById(id); + } +// public List findByCategoriesNotContains(Category category) { +// return productRepository.findByCategoriesNotContains(category); +// } +} + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a97c83f..1462543 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,6 @@ -spring.datasource.url=jdbc:mysql://localhost:3306/products?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC -spring.datasource.username=root -spring.datasource.password=root +spring.datasource.url=jdbc:mysql://localhost:3306/store?allowPublicKeyRetrieval=true&useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC +spring.datasource.username=CedricS +spring.datasource.password=Kikflip6217! spring.jpa.hibernate.ddl-auto=update -spring.mvc.view.prefix=/WEB-INF/ \ No newline at end of file +spring.mvc.view.prefix=/WEB-INF/ +spring.mvc.view.suffix=.jsp \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/categories/categoriesindex.jsp b/src/main/webapp/WEB-INF/categories/categoriesindex.jsp index caa50ce..ee6b09b 100644 --- a/src/main/webapp/WEB-INF/categories/categoriesindex.jsp +++ b/src/main/webapp/WEB-INF/categories/categoriesindex.jsp @@ -19,7 +19,7 @@

Choose a Category

diff --git a/src/main/webapp/WEB-INF/categories/newcategory.jsp b/src/main/webapp/WEB-INF/categories/newcategory.jsp index fe95371..9417923 100644 --- a/src/main/webapp/WEB-INF/categories/newcategory.jsp +++ b/src/main/webapp/WEB-INF/categories/newcategory.jsp @@ -17,9 +17,9 @@
- Category Name: - - + Category Name: + +
diff --git a/src/main/webapp/WEB-INF/products/index.jsp b/src/main/webapp/WEB-INF/products/index.jsp index c451a6c..1531f69 100644 --- a/src/main/webapp/WEB-INF/products/index.jsp +++ b/src/main/webapp/WEB-INF/products/index.jsp @@ -19,7 +19,7 @@

All Products

Add Product

diff --git a/src/main/webapp/WEB-INF/products/newproduct.jsp b/src/main/webapp/WEB-INF/products/newproduct.jsp index 1d88527..1e43e46 100644 --- a/src/main/webapp/WEB-INF/products/newproduct.jsp +++ b/src/main/webapp/WEB-INF/products/newproduct.jsp @@ -1,4 +1,4 @@ -<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> @@ -12,11 +12,12 @@ +
- Product Name: - - + Product Name: + +
From b26bf09a07932059841ecd6e12efbc18e5448b7d Mon Sep 17 00:00:00 2001 From: "koastiebratt@gmail.com" Date: Fri, 13 Nov 2020 17:07:19 -0500 Subject: [PATCH 2/4] Items are updateable, no duplicate categories --- .../controllers/AssociationController.java | 17 +++++------------ .../controllers/CategoryController.java | 18 ++++++++++++------ .../controllers/ProductController.java | 17 +++++++++++++++-- .../productcategories/models/Product.java | 1 + .../repositories/CategoryRepository.java | 2 +- .../repositories/ProductRepository.java | 2 +- .../services/CategoryService.java | 6 +++--- .../services/ProductService.java | 12 +++++------- src/main/webapp/WEB-INF/products/index.jsp | 2 ++ 9 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java index 7372df3..fecd021 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java @@ -1,21 +1,14 @@ package com.dtcc.projects.productcategories.controllers; -import com.dtcc.projects.productcategories.models.Category; -import com.dtcc.projects.productcategories.repositories.CategoryRepository; +import com.dtcc.projects.productcategories.repositories.AssociationRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.stereotype.Controller; + +@Controller public class AssociationController { @Autowired - private CategoryRepository categoryRepository; - -// @RequestMapping(value="/categories/createcategory", method= RequestMethod.POST) -// public void addNewCategory(@ModelAttribute("newcategory") Category category) { -// Category test = new Category(category.getValue()); -// categoryRepository.save(test); -// } + private AssociationRepository associationRepository; } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java index 9d4861d..f8d650d 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; +import java.util.Date; import java.util.List; @Controller public class CategoryController { @@ -32,13 +33,18 @@ public String newcat(Model model){ @PostMapping(value = "/categories/createcategory") public String createCategory(@ModelAttribute("newcategory")Category cat){ - categoryService.saveCategory(cat); + List temp = categoryService.showAll(); + cat.setCreated_at(new Date()); + boolean exists = false; + for(Category c:temp){ + if(c.getValue().equals(cat.getValue())){ + exists = true; + } + } + if(!exists){ + categoryService.saveCategory(cat); + } return "redirect:/"; } -//@GetMapping("/products/newproduct")//.jsp file location/name -//public String newprod(Model model) { -// model.addAttribute("newproduct", new Product()); //Creating and inserting in JSp -// return "products/newproduct";//Returns .jsp page -//} } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java index d52010a..7197528 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java @@ -1,6 +1,5 @@ package com.dtcc.projects.productcategories.controllers; - import com.dtcc.projects.productcategories.models.Product; import com.dtcc.projects.productcategories.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; @@ -8,6 +7,7 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; @Controller @@ -26,12 +26,25 @@ public String getIndexPage(Model model){ @GetMapping("/products/newproduct")//.jsp file location/name public String newprod(Model model) { model.addAttribute("newproduct", new Product()); //Creating and inserting in JSp + return "products/newproduct";//Returns .jsp page } @PostMapping(value = "/products/createproduct") //the action clause public String createProduct(@ModelAttribute("newproduct") Product item){//action attribute - productService.saveProduct(item);//Defined service for DB + List temp = productService.showAll(); + item.setCreated_at(new Date()); + productService.saveProduct(item); + + for(Product p:temp) { + if(p.getProductName().equals(item.getProductName())){ + productService.updateProduct(p.getProductId(), item.getProductName(),item.getDescription(), item.getPrice()); + p.setUpdated_at(new Date()); + productService.saveProduct(p); + productService.deleteById(item.getProductId()); + } + } + return "redirect:/";//Go back to main page } } diff --git a/src/main/java/com/dtcc/projects/productcategories/models/Product.java b/src/main/java/com/dtcc/projects/productcategories/models/Product.java index 5c0a1ca..1a3893c 100644 --- a/src/main/java/com/dtcc/projects/productcategories/models/Product.java +++ b/src/main/java/com/dtcc/projects/productcategories/models/Product.java @@ -44,4 +44,5 @@ public Product(String productName, String description, Double price, Date create this.created_at = created_at; this.updated_at = updated_at; } + } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java index 7ad9732..03a9e9f 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java @@ -8,7 +8,7 @@ import java.util.List; @Repository -public interface CategoryRepository extends CrudRepository { +public interface CategoryRepository extends CrudRepository { List findAll(); //List findByProductsNotContains(Product product); diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java index ba9de75..d3b0a0c 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java @@ -8,7 +8,7 @@ import java.util.List; @Repository -public interface ProductRepository extends CrudRepository { +public interface ProductRepository extends CrudRepository { List findAll(); //List findByCategoriesNotContains(Category category); } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java index 59c73e2..663300e 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java @@ -23,7 +23,7 @@ public List showAll(){ return categoryRepository.findAll(); } - public Category findById(Long ID){ + public Category findById(Integer ID){ return categoryRepository.findById(ID).orElse(null); } @@ -31,7 +31,7 @@ public Category saveCategory(Category category){ return categoryRepository.save(category); } - public Category updateProduct(Long id, String Name){ + public Category updateProduct(Integer id, String Name){ Category category= findById(id); if (Name!= null) { category.setValue(Name); @@ -42,7 +42,7 @@ public Category updateProduct(Long id, String Name){ return categoryRepository.save(category); } - public void deleteById(Long ID){ + public void deleteById(Integer ID){ categoryRepository.deleteById(ID); } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java index 350e900..68026e8 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -1,5 +1,4 @@ package com.dtcc.projects.productcategories.services; -import com.dtcc.projects.productcategories.models.Category; import com.dtcc.projects.productcategories.models.Product; import com.dtcc.projects.productcategories.repositories.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -9,6 +8,7 @@ @Service public class ProductService { private ProductRepository productRepository; + @Autowired public ProductService(ProductRepository productRepository) { this.productRepository=productRepository; @@ -18,7 +18,7 @@ public List showAll(){ return productRepository.findAll(); } - public Product findById(Long id){ + public Product findById(Integer id){ return productRepository.findById(id).orElse(null); } @@ -26,7 +26,7 @@ public Product saveProduct(Product product){ return productRepository.save(product); } - public Product updateProduct(Long id, String Name, String description, Double price){ + public Product updateProduct(Integer id, String Name, String description, Double price){ Product product= findById(id); if (Name!= null) { product.setProductName(Name); @@ -45,11 +45,9 @@ public Product updateProduct(Long id, String Name, String description, Double pr } return productRepository.save(product); } - public void deleteById(Long id){ + + public void deleteById(Integer id){ productRepository.deleteById(id); } -// public List findByCategoriesNotContains(Category category) { -// return productRepository.findByCategoriesNotContains(category); -// } } diff --git a/src/main/webapp/WEB-INF/products/index.jsp b/src/main/webapp/WEB-INF/products/index.jsp index 1531f69..55dc8cf 100644 --- a/src/main/webapp/WEB-INF/products/index.jsp +++ b/src/main/webapp/WEB-INF/products/index.jsp @@ -24,6 +24,8 @@

Add Product

Add Category

+

To update a product, please match item name with updated values.

+ Duplicate Categories now allowed!
\ No newline at end of file From 6a6c5387792978add702d9ec94e40f9d5bf78c29 Mon Sep 17 00:00:00 2001 From: "koastiebratt@gmail.com" Date: Fri, 13 Nov 2020 20:48:50 -0500 Subject: [PATCH 3/4] Problems with 'findByCategoriesNotContains' --- .../controllers/AssociationController.java | 37 ++++++++++++++++--- .../controllers/CategoryController.java | 17 +++++++-- .../controllers/ProductController.java | 17 +++++++++ .../repositories/CategoryRepository.java | 2 +- .../repositories/ProductRepository.java | 2 +- .../services/CategoryService.java | 6 +-- .../services/ProductService.java | 5 +++ src/main/webapp/WEB-INF/products/show.jsp | 6 +-- 8 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java index fecd021..38ccf9e 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/AssociationController.java @@ -1,14 +1,41 @@ -package com.dtcc.projects.productcategories.controllers; - -import com.dtcc.projects.productcategories.repositories.AssociationRepository; +import com.dtcc.projects.productcategories.models.Association; +import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.services.AssociationService; +import com.dtcc.projects.productcategories.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; - +import java.util.List; @Controller public class AssociationController { + + @Autowired - private AssociationRepository associationRepository; + private AssociationService associationService; + @Autowired + private ProductService productService; + + + @RequestMapping(value="/associations/products",method=RequestMethod.POST) + public String setCategoriesProducts(Model model, @ModelAttribute("association") Association association){ + ListproductList = productService.showAll(); + model.addAttribute("products",productList); + + //save data into categories_products table. + associationService.saveAssociation(association); + return "/products/index"; + } +// @RequestMapping(value="/associations/categories",method=RequestMethod.POST) +// public String getCategory(Model model, @ModelAttribute("association1") Association associationObj){ +// List productList = productService.showAll(); +// model.addAttribute("products",productList); +// associationService.saveAssociation(associationObj); +// return "/products/index"; +// } } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java index f8d650d..ccf79d2 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java @@ -1,5 +1,6 @@ package com.dtcc.projects.productcategories.controllers; +import com.dtcc.projects.productcategories.models.Association; import com.dtcc.projects.productcategories.models.Category; import com.dtcc.projects.productcategories.models.Product; import com.dtcc.projects.productcategories.services.CategoryService; @@ -7,9 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; @@ -17,6 +16,7 @@ public class CategoryController { @Autowired private CategoryService categoryService; + @Autowired ProductService productService; @GetMapping(value = "/categories/category") public String getCategoryIndex(Model model){ @@ -47,4 +47,15 @@ public String createCategory(@ModelAttribute("newcategory")Category cat){ return "redirect:/"; } + @RequestMapping(value="categories/{id}",method= RequestMethod.GET) + public String showById(Model model, @PathVariable Integer id){ + Category category=categoryService.findById(id); + List productList= productService.findByCategoriesNotContains(category); + + Association association =new Association(); + model.addAttribute("category",category); + model.addAttribute("association",association); + model.addAttribute("notInProducts",productList); + return "categories/show"; + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java index 7197528..0de38db 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java @@ -1,6 +1,9 @@ package com.dtcc.projects.productcategories.controllers; +import com.dtcc.projects.productcategories.models.Association; +import com.dtcc.projects.productcategories.models.Category; import com.dtcc.projects.productcategories.models.Product; +import com.dtcc.projects.productcategories.services.CategoryService; import com.dtcc.projects.productcategories.services.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -15,6 +18,8 @@ public class ProductController{ @Autowired private ProductService productService; + @Autowired + private CategoryService categoryService; @GetMapping(value = "/") public String getIndexPage(Model model){ @@ -47,4 +52,16 @@ public String createProduct(@ModelAttribute("newproduct") Product item){//action return "redirect:/";//Go back to main page } + + @RequestMapping(value="products/{id}",method=RequestMethod.GET) + public String showById(Model model,@PathVariable Integer id){ + Product product = productService.findById(id); + List categoryList = categoryService.findByProductsNotContains(product); + Association association = new Association(); + + model.addAttribute("product",product); + model.addAttribute("association",association); + model.addAttribute("notInCategories",categoryList); + return "products/show"; + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java index 03a9e9f..54f8236 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java @@ -10,6 +10,6 @@ @Repository public interface CategoryRepository extends CrudRepository { List findAll(); - //List findByProductsNotContains(Product product); + List findByProductsNotContains(Product product); } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java index d3b0a0c..b5b44f4 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java @@ -10,5 +10,5 @@ @Repository public interface ProductRepository extends CrudRepository { List findAll(); - //List findByCategoriesNotContains(Category category); + List findByCategoriesNotContains(Category category); } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java index 663300e..8a42484 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java @@ -46,7 +46,7 @@ public void deleteById(Integer ID){ categoryRepository.deleteById(ID); } -// public List findByProductsNotContains(Product product){ -// return categoryRepository.findByProductsNotContains(product); -// } + public List findByProductsNotContains(Product product){ + return categoryRepository.findByProductsNotContains(product); + } } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java index 68026e8..a77cc83 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -1,4 +1,5 @@ package com.dtcc.projects.productcategories.services; +import com.dtcc.projects.productcategories.models.Category; import com.dtcc.projects.productcategories.models.Product; import com.dtcc.projects.productcategories.repositories.ProductRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -49,5 +50,9 @@ public Product updateProduct(Integer id, String Name, String description, Double public void deleteById(Integer id){ productRepository.deleteById(id); } + + public List findByCategoriesNotContains(Category category){ + return productRepository.findByCategoriesNotContains(category); + } } diff --git a/src/main/webapp/WEB-INF/products/show.jsp b/src/main/webapp/WEB-INF/products/show.jsp index dd37ab2..e37a448 100644 --- a/src/main/webapp/WEB-INF/products/show.jsp +++ b/src/main/webapp/WEB-INF/products/show.jsp @@ -17,17 +17,17 @@ -

${ product.name }

+

${ product.productName }

${ product.description }

Category:

    -
  • ${ cat.name }
  • +
  • ${ cat.value }

Add Category

- +
Category From 6c407fd143cb4e01d8654271bbdf2389769114e0 Mon Sep 17 00:00:00 2001 From: "koastiebratt@gmail.com" Date: Fri, 13 Nov 2020 20:52:51 -0500 Subject: [PATCH 4/4] Association is killing me --- .../controllers/CategoryController.java | 22 +++++++++---------- .../controllers/ProductController.java | 22 +++++++++---------- .../repositories/CategoryRepository.java | 2 +- .../repositories/ProductRepository.java | 2 +- .../services/CategoryService.java | 6 ++--- .../services/ProductService.java | 6 ++--- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java index ccf79d2..b378cff 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java @@ -47,15 +47,15 @@ public String createCategory(@ModelAttribute("newcategory")Category cat){ return "redirect:/"; } - @RequestMapping(value="categories/{id}",method= RequestMethod.GET) - public String showById(Model model, @PathVariable Integer id){ - Category category=categoryService.findById(id); - List productList= productService.findByCategoriesNotContains(category); - - Association association =new Association(); - model.addAttribute("category",category); - model.addAttribute("association",association); - model.addAttribute("notInProducts",productList); - return "categories/show"; - } +// @RequestMapping(value="categories/{id}",method= RequestMethod.GET) +// public String showById(Model model, @PathVariable Integer id){ +// Category category=categoryService.findById(id); +// List productList= productService.findByCategoriesNotContains(category); +// +// Association association =new Association(); +// model.addAttribute("category",category); +// model.addAttribute("association",association); +// model.addAttribute("notInProducts",productList); +// return "categories/show"; +// } } diff --git a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java index 0de38db..2cc3870 100644 --- a/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/ProductController.java @@ -53,15 +53,15 @@ public String createProduct(@ModelAttribute("newproduct") Product item){//action return "redirect:/";//Go back to main page } - @RequestMapping(value="products/{id}",method=RequestMethod.GET) - public String showById(Model model,@PathVariable Integer id){ - Product product = productService.findById(id); - List categoryList = categoryService.findByProductsNotContains(product); - Association association = new Association(); - - model.addAttribute("product",product); - model.addAttribute("association",association); - model.addAttribute("notInCategories",categoryList); - return "products/show"; - } +// @RequestMapping(value="products/{id}",method=RequestMethod.GET) +// public String showById(Model model,@PathVariable Integer id){ +// Product product = productService.findById(id); +// List categoryList = categoryService.findByProductsNotContains(product); +// Association association = new Association(); +// +// model.addAttribute("product",product); +// model.addAttribute("association",association); +// model.addAttribute("notInCategories",categoryList); +// return "products/show"; +// } } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java index 54f8236..4f6826e 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/CategoryRepository.java @@ -10,6 +10,6 @@ @Repository public interface CategoryRepository extends CrudRepository { List findAll(); - List findByProductsNotContains(Product product); + // List findByProductsNotContains(Product product); } diff --git a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java index b5b44f4..d3b0a0c 100644 --- a/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java +++ b/src/main/java/com/dtcc/projects/productcategories/repositories/ProductRepository.java @@ -10,5 +10,5 @@ @Repository public interface ProductRepository extends CrudRepository { List findAll(); - List findByCategoriesNotContains(Category category); + //List findByCategoriesNotContains(Category category); } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java index 8a42484..663300e 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.java @@ -46,7 +46,7 @@ public void deleteById(Integer ID){ categoryRepository.deleteById(ID); } - public List findByProductsNotContains(Product product){ - return categoryRepository.findByProductsNotContains(product); - } +// public List findByProductsNotContains(Product product){ +// return categoryRepository.findByProductsNotContains(product); +// } } diff --git a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java index a77cc83..c65ea79 100644 --- a/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -51,8 +51,8 @@ public void deleteById(Integer id){ productRepository.deleteById(id); } - public List findByCategoriesNotContains(Category category){ - return productRepository.findByCategoriesNotContains(category); - } +// public List findByCategoriesNotContains(Category category){ +// return productRepository.findByCategoriesNotContains(category); +// } }