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..294f471 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,5 @@ package com.dtcc.projects.productcategories.controllers; public class AssociationController { + } 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..6ad85c3 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,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 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:/"; + } } 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..289b4b4 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,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; + } } 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..c532b9b 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,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 productList; + + + public Category(String value, Date created_at, Date updated_at) { + this.categoryName = value; + this.createdAt = created_at; + this.updatedAt = updated_at; + } +} \ No newline at end of file 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..b0b0d93 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 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 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; + } } 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..4e7e32d 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,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 { + } 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..5998162 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,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 { 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..c7e5ae6 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..6187b3a --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/AssociationService.java @@ -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); + } +} 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..8aa03ea --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/CategoryService.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.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 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 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..509c764 --- /dev/null +++ b/src/main/java/com/dtcc/projects/productcategories/services/ProductService.java @@ -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 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 findByCategoriesNotContains(Category category) { +// return productRepository.findByCategoriesNotContains(category); +// } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a97c83f..c65010c 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.password=JanaReemRoot 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/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

Add Product

diff --git a/src/main/webapp/WEB-INF/products/newproduct.jsp b/src/main/webapp/WEB-INF/products/newproduct.jsp index 1d88527..6247fb5 100644 --- a/src/main/webapp/WEB-INF/products/newproduct.jsp +++ b/src/main/webapp/WEB-INF/products/newproduct.jsp @@ -14,18 +14,18 @@
- Product Name: - - + Product Name: + +
- Description: + description:
- Price: + price: