Merge branch 'server_side_template' of https://github.com/c-my/mall-website into server_side_template

This commit is contained in:
lihang19980418
2019-09-04 15:41:36 +08:00
4 changed files with 68 additions and 3 deletions
@@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.file.Path;
@@ -21,6 +22,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
registry.addResourceHandler("/img/**").addResourceLocations("file:/" + location.toString() + "\\");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/product");
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
@@ -3,6 +3,8 @@ package edu.neu.neumall.controller;
import edu.neu.neumall.entity.Product;
import edu.neu.neumall.entity.ShoppingCart;
import edu.neu.neumall.entity.User;
import edu.neu.neumall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -14,12 +16,20 @@ import java.util.HashSet;
@Controller
@RequestMapping("/cart")
public class ShoppingCartController {
private ShoppingCartService shoppingCartService;
@Autowired
public ShoppingCartController(ShoppingCartService shoppingCartService) {
this.shoppingCartService = shoppingCartService;
}
@GetMapping
public String shoppingCartPage(@AuthenticationPrincipal User user, Model model) {
ArrayList<Product> products = new ArrayList<>();
var shoppingCartItems = user.getShoppingCart();
if (shoppingCartItems == null) {
shoppingCartItems = new HashSet<ShoppingCart>();
shoppingCartItems = new HashSet<>();
}
model.addAttribute("productlist", shoppingCartItems);
@@ -30,8 +40,32 @@ public class ShoppingCartController {
@ResponseBody
public String appendShoppingCart(@RequestParam long productID, @RequestParam int count,
@AuthenticationPrincipal User user) {
ShoppingCart item = new ShoppingCart();
var result = shoppingCartService.addShoppingCartItem(productID, count, user);
if (result) {
return "success";
}
return "false";
}
return "";
@PostMapping("/delete")
@ResponseBody
public void removeShoppingCart(@RequestParam("shoppingCartID") long cartID, @AuthenticationPrincipal User user) {
shoppingCartService.deleteCartByID(cartID);
}
@PostMapping("/update")
@ResponseBody
public String updateShoppingCart(@RequestParam("shoppingCartID") long cartID,
@RequestParam("count") int count,
@AuthenticationPrincipal User user) {
var shoppingCartOptional = shoppingCartService.getByID(cartID);
if (shoppingCartOptional.isEmpty() || !shoppingCartOptional.get().getOwner().equals(user)
|| count < 0) {
return "false";
}
var item = shoppingCartOptional.get();
item.setCount(count);
shoppingCartService.save(item);
return "true";
}
}
@@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long> {
@@ -12,4 +13,6 @@ public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long
public List<ShoppingCart> findAllByOwner_ID(long id);
public List<ShoppingCart> removeByOwner_ID(long id);
public Optional<ShoppingCart> findByOwnerIDAndProductID(long ownerID, long productID);
}
@@ -8,6 +8,8 @@ import edu.neu.neumall.repository.ProductRepository;
import edu.neu.neumall.repository.ShoppingCartRepository;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class ShoppingCartService {
private ShoppingCartRepository shoppingCartRepository;
@@ -24,6 +26,15 @@ public class ShoppingCartService {
if (productOptional.isEmpty()) {
return false;
}
// cart item already exists
var cartExist = shoppingCartRepository.findByOwnerIDAndProductID(user.getID(), productID);
if (cartExist.isPresent()) {
var cart = cartExist.get();
cart.setCount(cart.getCount() + 1);
shoppingCartRepository.save(cart);
return true;
}
// item not exist, create a new one
var product = productOptional.get();
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setCount(count);
@@ -33,4 +44,15 @@ public class ShoppingCartService {
return true;
}
public void deleteCartByID(long cartID) {
shoppingCartRepository.deleteById(cartID);
}
public Optional<ShoppingCart> getByID(long cartID) {
return shoppingCartRepository.findById(cartID);
}
public void save(ShoppingCart cart) {
shoppingCartRepository.save(cart);
}
}