Skip to content

Commit

Permalink
update productdetail function
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantaphocpython committed Mar 13, 2024
1 parent 43d0eab commit 19dac09
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class SecurityConfig {

private final static String[] paths = {
"/**",
"/js/**",
"/error",
"/shop/**",
"/login",
"/registration/**",
"/img/**",
Expand All @@ -35,7 +37,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
author -> author.requestMatchers(paths).permitAll()
author -> author
.requestMatchers("/shop/order").authenticated()
.requestMatchers(paths).permitAll()
.anyRequest().authenticated()
)
.formLogin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import jakarta.servlet.http.HttpServletRequest;
import org.ecommerce.spring.boot.vegetable.project.entity.Category;
import org.ecommerce.spring.boot.vegetable.project.entity.OrderItem;
import org.ecommerce.spring.boot.vegetable.project.entity.Product;
import org.ecommerce.spring.boot.vegetable.project.service.CategoryService;
import org.ecommerce.spring.boot.vegetable.project.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

Expand Down Expand Up @@ -34,9 +36,20 @@ public ModelAndView shop(HttpServletRequest request) {
@GetMapping("/product/{id}")
public ModelAndView productDetail(@PathVariable Long id, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("productDetail");
List<Category> categories = categoryService.getCategoryList();
modelAndView.addObject("categories", categories);
modelAndView.addObject("request", request);
return modelAndView;
}

@GetMapping("/shoppingCart")
public ModelAndView shoppingCart(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("shoppingCart");
List<Category> categories = categoryService.getCategoryList();
modelAndView.addObject("categories", categories);
modelAndView.addObject("request", request);
return modelAndView;

}

@GetMapping("/getProductSaleOffList/{categoryName}")
Expand Down Expand Up @@ -90,4 +103,11 @@ public List<Product> getProductByCategory(@RequestParam Long categoryId,
return products;
}

@GetMapping("/order")
public String order(@RequestParam Long productId,
@RequestParam Integer quantity,
@RequestParam Long userId) {
return productService.order(productId, quantity, userId);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.ecommerce.spring.boot.vegetable.project.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.aspectj.weaver.ast.Or;

import java.util.List;

@Entity
@Data
Expand All @@ -20,4 +20,21 @@ public class Order {
private Long id;
private String status;

}
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(
name = "order_id",
referencedColumnName = "id"
)
private List<OrderItem> orderItems;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(
name = "user_id",
referencedColumnName = "id"
)
private User user;

public void addOrderItem(OrderItem orderItem) {
orderItems.add(orderItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ecommerce.spring.boot.vegetable.project.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long productId;
private int quantity;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.ecommerce.spring.boot.vegetable.project.repository;

import org.ecommerce.spring.boot.vegetable.project.entity.OrderItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderItemRepository extends JpaRepository<OrderItem, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.ecommerce.spring.boot.vegetable.project.repository;

import org.ecommerce.spring.boot.vegetable.project.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {

@Query(
value = "SELECT * FROM Order where status = 'not yet' AND user_id = :userId",
nativeQuery = true
)
Order findOrderByUserIdAndStatusNotYet(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.ecommerce.spring.boot.vegetable.project.service;

public interface OrderItemService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.ecommerce.spring.boot.vegetable.project.service;

import org.ecommerce.spring.boot.vegetable.project.entity.Order;

public interface OrderService {
Order findOderNotYetByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public interface ProductService {
Product findProductById(Long id);

List<Product> getProductByCategory(Long categoryId, Long productId);

String order(Long productId, Integer quantity, Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.ecommerce.spring.boot.vegetable.project.service.implement;

import org.ecommerce.spring.boot.vegetable.project.service.OrderItemService;
import org.springframework.stereotype.Service;

@Service
public class OrderItemServiceImp implements OrderItemService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ecommerce.spring.boot.vegetable.project.service.implement;

import org.ecommerce.spring.boot.vegetable.project.entity.Order;
import org.ecommerce.spring.boot.vegetable.project.repository.OrderRepository;
import org.ecommerce.spring.boot.vegetable.project.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImp implements OrderService {

@Autowired
private OrderRepository orderRepository;

@Override
public Order findOderNotYetByUserId(Long userId) {
Order order = orderRepository.findOrderByUserIdAndStatusNotYet(userId);
return order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

import org.ecommerce.spring.boot.vegetable.project.dto.ProductDto;
import org.ecommerce.spring.boot.vegetable.project.entity.Category;
import org.ecommerce.spring.boot.vegetable.project.entity.Order;
import org.ecommerce.spring.boot.vegetable.project.entity.OrderItem;
import org.ecommerce.spring.boot.vegetable.project.entity.Product;
import org.ecommerce.spring.boot.vegetable.project.repository.CategoryRepository;
import org.ecommerce.spring.boot.vegetable.project.repository.OrderRepository;
import org.ecommerce.spring.boot.vegetable.project.repository.ProductRepository;
import org.ecommerce.spring.boot.vegetable.project.repository.UserRepository;
import org.ecommerce.spring.boot.vegetable.project.service.OrderItemService;
import org.ecommerce.spring.boot.vegetable.project.service.OrderService;
import org.ecommerce.spring.boot.vegetable.project.service.ProductService;
import org.ecommerce.spring.boot.vegetable.project.service.UserService;
import org.ecommerce.spring.boot.vegetable.project.utility.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.swing.plaf.BorderUIResource;
import java.io.IOException;
import java.util.List;

Expand All @@ -21,12 +29,16 @@ public class ProductServiceImp implements ProductService {

@Autowired
private ProductRepository productRepository;

@Autowired
private ImageUtils imageUtils;

@Autowired
private CategoryRepository categoryRepository;
@Autowired
private OrderItemService orderItemService;
@Autowired
private OrderRepository orderRepository;
@Autowired
private UserRepository userRepository;

@Override
public Product addProduct(ProductDto productDto) throws IOException {
Expand Down Expand Up @@ -123,4 +135,25 @@ public List<Product> getProductByCategory(Long categoryId, Long productId) {
List<Product> products = productRepository.findByCategoryAndIdNot(category, productId, page).getContent();
return products;
}

@Override
public String order(Long productId, Integer quantity, Long userId) {
OrderItem orderItem = OrderItem.builder()
.productId(productId)
.quantity(quantity)
.build();
Order order = orderRepository.findOrderByUserIdAndStatusNotYet(userId);
if(order == null) {
Order userOrder = Order.builder()
.status("not yet")
.user(userRepository.findById(userId).get())
.orderItems(List.of(orderItem))
.build();
orderRepository.save(userOrder);
}
else {
order.addOrderItem(orderItem);
}
return "success";
}
}
18 changes: 17 additions & 1 deletion src/main/resources/static/js/productDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function getProductDetail(id) {
"<input class='product-detail_number-item text-center' type='number' value='0'/>" +
"<span class='product-detail_number-item' onclick='incrementProductQuantity()'>+</span>" +
"</div>" +
"<button class=\"btn btn-outline-success search-bar-submit product-detail_btn\" type=\"submit\">ADD TO CART</button>" +
"<button class=\"btn btn-outline-success search-bar-submit product-detail_btn\" type=\"submit\" " +
"onclick='order("+ product.id + "," + $('.product-detail_number-item').value +")'>ADD TO CART</button>" +
"</div>" +
"<hr class='mt-5' />" +
"<div class='mt-5 product-detail_inform'>" +
Expand Down Expand Up @@ -73,6 +74,21 @@ function getProductDetail(id) {
});
}

function order(productId, quantity, userId) {
$.ajax({
type: "GET",
url: "/shop/order",
data: {
productId: productId,
quantity: quantity,
userId: userId
},
success: function (data) {
console.log(data);
}
});
}

function incrementProductQuantity() {
let quantity = document.querySelectorAll(".product-detail_number-item")[1];
quantity.value = parseInt(quantity.value) + 1;
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/templates/fragments/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@
<div class="page-title-wrapper" th:fragment="page-title-image">
<img th:src="@{/img/breadcrumb.jpg.webp}" class="page-title-img">
<h2 class="page-title"
th:text="${request.getRequestURL().toString().contains('shop')} ? 'OGANI SHOP':''">
th:text="${request.getRequestURL().toString().contains('shop') &&
!request.getRequestURL().toString().contains('shoppingCart')} ? 'OGANI SHOP':''">
</h2>
<h2 class="page-title"
th:text="${request.getRequestURL().toString().contains('shoppingCart')} ? 'Shopping Cart':''">
</h2>
<h2 class="page-title"
th:text="${request.getRequestURL().toString().contains('blog')} ? 'BLOG':''">
Expand Down
29 changes: 29 additions & 0 deletions src/main/resources/templates/shoppingCart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en" th:lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script th:src="@{/js/home.js}"></script>
<link rel="stylesheet" th:href="@{/css/header.css}">
<link rel="stylesheet" th:href="@{/css/home.css}">
<link rel="stylesheet" th:href="@{/css/page_title_img.css}">
<link rel="stylesheet" th:href="@{/css/footer.css}">
<link rel="stylesheet" th:href="@{/css/shop.css}">
<link rel="stylesheet" th:href="@{/css/productDetail.css}">
<body>
<div th:insert="fragments/fragments :: header"></div>
<div class="container col-9 search-content">
<div th:insert="home :: header-search-bar"></div>
</div>
<div th:insert="fragments/fragments :: page-title-image"></div>

</body>
</html>

0 comments on commit 19dac09

Please sign in to comment.