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
- 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
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:
+
+