Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
package com.dtcc.projects.productcategories;

import com.dtcc.projects.productcategories.models.Product;
import com.dtcc.projects.productcategories.repositories.CategoryRepository;
import com.dtcc.projects.productcategories.repositories.ProductRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.util.Date;
import java.util.Optional;

@SpringBootApplication
public class ProductCategoriesApplication {

private static final Logger log = LoggerFactory.getLogger(ProductCategoriesApplication.class);

public static void main(String[] args) {
SpringApplication.run(ProductCategoriesApplication.class, args);
}

/* @Bean
public CommandLineRunner demo(ProductRepository productRepository, CategoryRepository categoryRepository){
return (args) -> {
//save few products
productRepository.save(new Product("Butter","Lite Olive Butter spread",4.99,new Date(),new Date()));
productRepository.save(new Product("Oil","Wesson brand",7.50,new Date(),new Date()));
productRepository.save(new Product("Tablet","Ipad-mini",250.99,new Date(),new Date()));

//fetch all products.
log.info("Products found with findAll(): ");
log.info("---------------------------------");
for(Product product : productRepository.findAll()){
log.info(product.toString());
}
log.info(" ");

//fetch an individual product by ID;
Optional<Product> product=productRepository.findById(25L);
log.info("Product found with findById(25L):");
log.info("--------------------------------");
log.info(product.toString());
log.info("");
};
}*/
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
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.PathVariable;
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;

private List<Product> productList;
@RequestMapping(value="/associations/products",method=RequestMethod.POST)
public String insertDataIntoCategoriesProducts(Model model, @ModelAttribute("association") Association associationObj){
productList= (List<Product>) productService.showAll();
model.addAttribute("products",productList);

//save data into categories_products table.
associationService.save(associationObj);
return "/products/index";
}

@RequestMapping(value="/associations/categories",method=RequestMethod.POST)
public String insertDataFromCategoryPage(Model model, @ModelAttribute("association1") Association associationObj){
productList= (List<Product>) productService.showAll();
model.addAttribute("products",productList);

//save data into categories_products table.
associationService.save(associationObj);
return "/products/index";
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Controller
public class CategoryController {

@Autowired
private ProductService productService;

@Autowired
private CategoryService categoryService;

@RequestMapping(value="categories/{id}",method=RequestMethod.GET)
public String showById(Model model,@PathVariable Long id){
Category category=categoryService.showById(id);
List<Product> productList= (List<Product>) productService.findByCategoriesNotContains(category);
Association associationObj=new Association();
model.addAttribute("category",category);
model.addAttribute("association",associationObj);
model.addAttribute("notInProducts",productList);
return "categories/show";
}

@RequestMapping(value="/categories/category", method=RequestMethod.GET)
public String getAllCategories(Model model){
List<Category> categories= (List<Category>) categoryService.showAll();
model.addAttribute("categories",categories);
return "/categories/categoriesindex";
}
@RequestMapping(value="categories/newcategory", method=RequestMethod.GET)
public String showNewCategory(Model model){
Category category=new Category();
model.addAttribute("newcategory",category);
return "/categories/newcategory";
}

@RequestMapping(value="categories/createcategory", method=RequestMethod.POST)
public String addNewCategory(@Valid @ModelAttribute("newcategory") Category category, BindingResult bindingResult){
if (bindingResult.hasErrors()) { return "/categories/newcategory";}
else {
category.setCreatedate(new Date());
category.setUpdatedate(new Date());
categoryService.create(category);
return "redirect:/viewcategories";
}
}

@RequestMapping("/viewcategories")
public String viewCategories(Model model){
List<Category> categoryList= (List<Category>) categoryService.showAll();
model.addAttribute("categories",categoryList);
return "categories/categoriesindex";
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,80 @@
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.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ProductController {

@Autowired
private ProductService productService;

@Autowired
private CategoryService categoryService;

@RequestMapping(value="/",method=RequestMethod.GET)
public String showIndexPage(Model model){

List<Product> productList= (List<Product>) productService.showAll();
model.addAttribute("products",productList);
return "products/index";
}

@RequestMapping(value="products/{id}",method=RequestMethod.GET)
public String showById(Model model,@PathVariable Long id){
Product product=productService.showById(id);
System.out.println("product: "+product);
List<Category> categoryList= (List<Category>) categoryService.findByProductNotContains(product);
Association associationObject = new Association();
model.addAttribute("product",product);
model.addAttribute("association",associationObject);
model.addAttribute("notInCategories",categoryList);
return "products/show";
}

@RequestMapping(value="products/newproduct", method=RequestMethod.GET)
public String showNewProduct(Model model){
Product product=new Product();
model.addAttribute("newproduct",product);
return "/products/newproduct";
}

//@ModelAttribute to read the data from the submitted form.
//to bind form data to java object.
@RequestMapping(value="products/createproduct", method=RequestMethod.POST)
public String addNewProduct(@Valid @ModelAttribute("newproduct") Product product, BindingResult bindingResult){

if(bindingResult.hasErrors()){return "products/newproduct";}
else {
product.setCreatedate(new Date());
product.setUpdatedate(new Date());
productService.create(product);
//return "redirect:/";
return "redirect:/viewproducts";
}
}

@RequestMapping("/viewproducts")
public String viewProducts(Model model){
System.out.println("in view products");
List<Product> productList= (List<Product>) productService.showAll();
model.addAttribute("products",productList);
return "products/index";
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
package com.dtcc.projects.productcategories.models;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity
@Table(name="categories_products")
public class Association {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="products_id")
private Product product;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="categories_id")
private Category category;

public Association(){}

public Association(Product product, Category category) {
this.product = product;
this.category = category;
}

public Association(Long id, Product product, Category category) {
this.id = id;
this.product = product;
this.category = category;
}

@Override
public String toString() {
return "Association{" +
"id=" + id +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
package com.dtcc.projects.productcategories.models;

import lombok.Data;
import lombok.ToString;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

@Data
@Entity
@Table(name="categories")
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull(message="is required")
@NotBlank(message="can not be empty")
private String name;

@Column(name="created_at")
private Date createdate;

@Column(name="updated_at")
private Date updatedate;
// @ManyToMany(targetEntity = Product.class, cascade = CascadeType.ALL)
// private List<Product> products;
@ManyToMany
@JoinTable(
name="categories_products",
joinColumns = @JoinColumn(name="categories_id"),
inverseJoinColumns = @JoinColumn(name="products_id")
)
private List<Product> products;

public Category(){}

public Category(@NotNull(message = "is required") @NotBlank(message = "can not be empty") String name, Date createdate, Date updatedate) {
this.name = name;
this.createdate = createdate;
this.updatedate = updatedate;
}

public Category(Long id, @NotNull(message = "is required") @NotBlank(message = "can not be empty") String name, Date createdate, Date updatedate) {
this.id = id;
this.name = name;
this.createdate = createdate;
this.updatedate = updatedate;
}

@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Loading