Merge remote-tracking branch 'origin/server_side_template' into server_side_template
This commit is contained in:
@@ -6,8 +6,7 @@ import edu.neu.neumall.entity.User;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
@@ -26,4 +25,13 @@ public class ShoppingCartController {
|
||||
|
||||
return "cart.html";
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
@ResponseBody
|
||||
public String appendShoppingCart(@RequestParam long productID, @RequestParam int count,
|
||||
@AuthenticationPrincipal User user) {
|
||||
ShoppingCart item = new ShoppingCart();
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,4 +125,8 @@ public class OrderService {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void save(Order order) {
|
||||
orderRepository.save(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,70 @@
|
||||
package edu.neu.neumall.service;
|
||||
|
||||
import edu.neu.neumall.entity.Order;
|
||||
import edu.neu.neumall.entity.Product;
|
||||
import edu.neu.neumall.entity.ShoppingCart;
|
||||
import edu.neu.neumall.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class SettleService {
|
||||
public void Purchase(User user, List<ShoppingCart> shoppingCart) {
|
||||
|
||||
private ProductService productService;
|
||||
private OrderService orderService;
|
||||
|
||||
@Autowired
|
||||
public SettleService(ProductService productService,
|
||||
OrderService orderService) {
|
||||
this.productService = productService;
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
public boolean Purchase(User user, List<ShoppingCart> shoppingCart) {
|
||||
if (!canPurchase(shoppingCart)) {
|
||||
return false;
|
||||
}
|
||||
ArrayList<Product> products = new ArrayList<>();
|
||||
for (var item : shoppingCart) {
|
||||
var targetProductID = item.getProduct().getID();
|
||||
var productOptional = productService.getProductByID(targetProductID);
|
||||
if (productOptional.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// reset product's count
|
||||
var product = productOptional.get();
|
||||
product.setCount(product.getCount() - item.getCount());
|
||||
productService.save(product);
|
||||
|
||||
// add a order record
|
||||
Order record = new Order();
|
||||
record.setCount(item.getCount());
|
||||
record.setOwner(item.getOwner());
|
||||
record.setProduct(item.getProduct());
|
||||
record.setPrice(product.getPrice());
|
||||
record.setType(Order.OrderType.PURCHASE);
|
||||
orderService.save(record);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isShoppingCartItemValid(ShoppingCart shoppingCart) {
|
||||
var product = shoppingCart.getProduct();
|
||||
if (product.getCount() < shoppingCart.getCount()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean canPurchase(List<ShoppingCart> shoppingCarts) {
|
||||
return false;
|
||||
for (var item : shoppingCarts) {
|
||||
if (!isShoppingCartItemValid(item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package edu.neu.neumall.service;
|
||||
|
||||
import edu.neu.neumall.entity.Order;
|
||||
import edu.neu.neumall.entity.Product;
|
||||
import edu.neu.neumall.entity.ShoppingCart;
|
||||
import edu.neu.neumall.entity.User;
|
||||
import edu.neu.neumall.repository.ProductRepository;
|
||||
import edu.neu.neumall.repository.ShoppingCartRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ShoppingCartService {
|
||||
private ShoppingCartRepository shoppingCartRepository;
|
||||
private ProductRepository productRepository;
|
||||
|
||||
public ShoppingCartService(ShoppingCartRepository shoppingCartRepository,
|
||||
ProductRepository productRepository) {
|
||||
this.shoppingCartRepository = shoppingCartRepository;
|
||||
this.productRepository = productRepository;
|
||||
}
|
||||
|
||||
public boolean addShoppingCartItem(long productID, int count, User user) {
|
||||
var productOptional = productRepository.findById(productID);
|
||||
if (productOptional.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
var product = productOptional.get();
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
shoppingCart.setCount(count);
|
||||
shoppingCart.setProduct(product);
|
||||
shoppingCart.setOwner(user);
|
||||
shoppingCartRepository.save(shoppingCart);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user