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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.sonatype.sisu</groupId>
<artifactId>sisu-inject-bean</artifactId>
<version>1.4.2</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -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){
List<Product>productList = 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<Product> productList = productService.showAll();
// model.addAttribute("products",productList);
// associationService.saveAssociation(associationObj);
// return "/products/index";
// }
}
Original file line number Diff line number Diff line change
@@ -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<Category> 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<Category> 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<Product> productList= productService.findByCategoriesNotContains(category);
//
// Association association =new Association();
// model.addAttribute("category",category);
// model.addAttribute("association",association);
// model.addAttribute("notInProducts",productList);
// return "categories/show";
// }
}
Original file line number Diff line number Diff line change
@@ -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<Product> 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<Product> 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<Category> categoryList = categoryService.findByProductsNotContains(product);
// Association association = new Association();
//
// model.addAttribute("product",product);
// model.addAttribute("association",association);
// model.addAttribute("notInCategories",categoryList);
// return "products/show";
// }
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Product> productList;

public Category(String value, Date created_at, Date updated_at) {
this.value = value;
this.created_at = created_at;
this.updated_at = updated_at;
}
}
Original file line number Diff line number Diff line change
@@ -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<Category> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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<Association,Long> {

}
Original file line number Diff line number Diff line change
@@ -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<Category, Integer> {
List<Category> findAll();
List<Category> findByProductsNotContains(Product product);
// List<Category> findByProductsNotContains(Product product);

}
Original file line number Diff line number Diff line change
@@ -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<Product, Integer> {
List<Product> findAll();
List<Product> findByCategoriesNotContains(Category category);
//List<Product> findByCategoriesNotContains(Category category);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading