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,4 +1,5 @@
package com.dtcc.projects.productcategories.controllers;

public class AssociationController {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
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;
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 ProductController {

@Autowired
private ProductService productService;

@GetMapping(value = "/")
public String getIndexPage(Model model) {

List<Product> productList=productService.showAll();

model.addAttribute("products",productList);
return "products/index";
}

@GetMapping(value = "/products/newproduct")
public String newprod(Model model) {
model.addAttribute("newproduct", new Product());
return "products/newproduct";
}

@PostMapping(value = "/products/createproduct")
public String CreateProduct(@ModelAttribute("newproduct") Product item) {
productService.saveProduct(item);
return "redirect:/";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
package com.dtcc.projects.productcategories.models;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Date;

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

@Id
@GeneratedValue
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,37 @@
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
@Column(name = "CATEGORY_ID")
private Integer categoryId;

@Column(name = "CATEGORY_NAME")
private String categoryName;

@Column(name = "CREATED_AT")
private Date createdAt;

@Column(name = "UPDATED_AT")
private Date updatedAt;

@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.categoryName = value;
this.createdAt = created_at;
this.updatedAt = 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 Long productId;

@Column(name ="PRODUCT_NAME")
private String productName;

@Column(name ="DESCRIPTION")
private String description;

@Column(name ="PRICE")
private Double price;

@Column(name ="CREATED_AT")
private Date createdAt;

@Column(name = "UPDATED_AT")
private Date updatedAt;


@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 createdAt, Date updatedAt) {
this.productName = productName;
this.description = description;
this.price = price;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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,16 @@
package com.dtcc.projects.productcategories.repositories;

import com.dtcc.projects.productcategories.models.Category;
import com.dtcc.projects.productcategories.models.Product;
import com.dtcc.projects.productcategories.services.CategoryService;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

public interface CategoryRepository {
@Repository
public interface CategoryRepository extends CrudRepository<Category, Long> {
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, Long> {
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,22 @@
package com.dtcc.projects.productcategories.services;

import com.dtcc.projects.productcategories.models.Association;
import com.dtcc.projects.productcategories.repositories.AssociationRepository;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
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);
}
}
Original file line number Diff line number Diff line change
@@ -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.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<Category> 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 updateCategory (Long id, String name){
Category category=findByID(id);
if( name!= null) {
category.setCategoryName(name);
} else{
category.setCategoryName(category.getCategoryName());
}

category.setUpdatedAt(new Date());

return categoryRepository.save(category);
}

public void deletById (Long id) {
categoryRepository.deleteById(id);
}

// public List <Category> findByProductsNotContains(Product product) {
// return categoryRepository.findByProductsNotContains(product);
// }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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.Date;
import java.util.List;

@Service

public class ProductService {
private ProductRepository productRepository;

@Autowired
public ProductService(ProductRepository productRepository) {
this.productRepository=productRepository;
}

public List<Product> 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());
}

product.setUpdatedAt(new Date());
return productRepository.save(product);
}

public void deleteById(Long id){
productRepository.deleteById(id);
}

// public List<Product> findByCategoriesNotContains(Category category) {
// return productRepository.findByCategoriesNotContains(category);
// }
}
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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.password=JanaReemRoot
spring.jpa.hibernate.ddl-auto=update
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/products/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<h2>All Products</h2>
<ul>
<c:forEach items="${ products }" var="prod">
<li><a href="/${ prod.id }">${ prod.name }</a></li>
<li><a href="/${ prod.productId }">${ prod.productName }</a></li>
</c:forEach>
</ul>
<h4> <a href="/products/newproduct">Add Product</a></h4>
Expand Down
Loading