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..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,4 +1,41 @@ -package com.dtcc.projects.productcategories.controllers; +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 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 new file mode 100644 index 0000000..b378cff --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/controllers/CategoryController.java @@ -0,0 +1,61 @@ +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; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; +@Controller +public class CategoryController { + @Autowired + private CategoryService categoryService; + @Autowired ProductService productService; + + @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){ + 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:/"; + } + +// @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 608f4e1..2cc3870 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,67 @@ package com.dtcc.projects.productcategories.controllers; -public class ProductController { +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; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; + +@Controller +public class ProductController{ + + @Autowired + private ProductService productService; + @Autowired + private CategoryService categoryService; + + @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 + 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 + } + +// @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/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..1a3893c 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,48 @@ 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..4f6826e 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..d3b0a0c 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..663300e --- /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(Integer ID){ + return categoryRepository.findById(ID).orElse(null); + } + + public Category saveCategory(Category category){ + return categoryRepository.save(category); + } + + public Category updateProduct(Integer 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(Integer 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..c65ea79 --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -0,0 +1,58 @@ +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(Integer id){ + return productRepository.findById(id).orElse(null); + } + + public Product saveProduct(Product product){ + return productRepository.save(product); + } + + public Product updateProduct(Integer 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(Integer 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..55dc8cf 100644 --- a/src/main/webapp/WEB-INF/products/index.jsp +++ b/src/main/webapp/WEB-INF/products/index.jsp @@ -19,11 +19,13 @@

All Products

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