package com.alatus.mall.cart.web;
import com.alatus.mall.cart.service.CartService;
import com.alatus.mall.cart.vo.Cart;
import com.alatus.mall.cart.vo.CartItem;
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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.concurrent.ExecutionException;
@Controller
public class CartWebController {
@Autowired
private CartService cartService;
@GetMapping("/cart.html")
public String cartListPage(Model model) throws ExecutionException, InterruptedException {
Cart cart = cartService.getCart();
model.addAttribute("cart",cart);
return "cartList";
}
@GetMapping("/deleteItem")
public String deleteItem(@RequestParam("skuId")Long skuId){
cartService.deleteItem(skuId);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/countItem")
public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){
cartService.changeItemCount(skuId,num);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/addToCart")
public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes model) throws ExecutionException, InterruptedException {
if(num > 0){
cartService.addToCart(skuId,num);
// RedirectAttributes的addAttribute会自动拼串,自动以参数的形式携带数据
// RedirectAttributes的addFlashAttribute()会保存在session里面,但是仅可以取一次值
model.addAttribute("skuId",skuId);
return "redirect:http://cart.alatusmall.com/addToCartSuccess.html";
}
return "redirect:http://cart.alatusmall.com/addToCartSuccess.html";
}
@GetMapping("/checkItem")
public String checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check")Integer check){
cartService.checkItem(skuId,check);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/addToCartSuccess.html")
public String addToCartSuccessPage(@RequestParam("skuId")Long skuId,Model model){
CartItem cartItem = cartService.getCartItem(skuId);
model.addAttribute("item",cartItem);
return "success";
}
}
package com.alatus.mall.cart.web;
import com.alatus.mall.cart.service.CartService;
import com.alatus.mall.cart.vo.Cart;
import com.alatus.mall.cart.vo.CartItem;
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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.concurrent.ExecutionException;
@Controller
public class CartWebController {
@Autowired
private CartService cartService;
@GetMapping("/cart.html")
public String cartListPage(Model model) throws ExecutionException, InterruptedException {
Cart cart = cartService.getCart();
model.addAttribute("cart",cart);
return "cartList";
}
@GetMapping("/deleteItem")
public String deleteItem(@RequestParam("skuId")Long skuId){
cartService.deleteItem(skuId);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/countItem")
public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){
cartService.changeItemCount(skuId,num);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/addToCart")
public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes model) throws ExecutionException, InterruptedException {
if(num > 0){
cartService.addToCart(skuId,num);
// RedirectAttributes的addAttribute会自动拼串,自动以参数的形式携带数据
// RedirectAttributes的addFlashAttribute()会保存在session里面,但是仅可以取一次值
model.addAttribute("skuId",skuId);
return "redirect:http://cart.alatusmall.com/addToCartSuccess.html";
}
return "redirect:http://cart.alatusmall.com/addToCartSuccess.html";
}
@GetMapping("/checkItem")
public String checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check")Integer check){
cartService.checkItem(skuId,check);
return "redirect:http://cart.alatusmall.com/cart.html";
}
@GetMapping("/addToCartSuccess.html")
public String addToCartSuccessPage(@RequestParam("skuId")Long skuId,Model model){
CartItem cartItem = cartService.getCartItem(skuId);
model.addAttribute("item",cartItem);
return "success";
}
}
package com.alatus.mall.cart.app;
import com.alatus.mall.cart.service.CartService;
import com.alatus.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CartController {
@Autowired
private CartService cartService;
@GetMapping("/currentUserCartItem")
public List<CartItem> getCurrentCartItem(){
return cartService.getUserCartItems();
}
}
package com.alatus.mall.cart.app;
import com.alatus.mall.cart.service.CartService;
import com.alatus.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CartController {
@Autowired
private CartService cartService;
@GetMapping("/currentUserCartItem")
public List<CartItem> getCurrentCartItem(){
return cartService.getUserCartItems();
}
}
package com.alatus.mall.product.web;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.SubCatalogVo;
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.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class IndexController {
@Autowired
private CategoryService categoryService;
@GetMapping({"/","/index.html"})
public String indexPage(Model model){
List<CategoryEntity> categoryEntities = categoryService.getLevelCategories(1);
// 视图解析器会自动拼串,不需要写具体位置和html后缀
model.addAttribute("categories",categoryEntities);
return "index";
}
}
package com.alatus.mall.product.web;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.SubCatalogVo;
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.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class IndexController {
@Autowired
private CategoryService categoryService;
@GetMapping({"/","/index.html"})
public String indexPage(Model model){
List<CategoryEntity> categoryEntities = categoryService.getLevelCategories(1);
// 视图解析器会自动拼串,不需要写具体位置和html后缀
model.addAttribute("categories",categoryEntities);
return "index";
}
}
package com.alatus.mall.product.web;
import com.alatus.mall.product.service.SkuInfoService;
import com.alatus.mall.product.vo.SkuItemVo;
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.PathVariable;
import java.util.concurrent.ExecutionException;
@Controller
public class ItemController {
@Autowired
private SkuInfoService skuInfoService;
@GetMapping("/{skuId}.html")
public String skuItem(@PathVariable("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
SkuItemVo skuItemVo = skuInfoService.item(skuId);
model.addAttribute("item",skuItemVo);
return "item";
}
}
package com.alatus.mall.product.web;
import com.alatus.mall.product.service.SkuInfoService;
import com.alatus.mall.product.vo.SkuItemVo;
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.PathVariable;
import java.util.concurrent.ExecutionException;
@Controller
public class ItemController {
@Autowired
private SkuInfoService skuInfoService;
@GetMapping("/{skuId}.html")
public String skuItem(@PathVariable("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
SkuItemVo skuItemVo = skuInfoService.item(skuId);
model.addAttribute("item",skuItemVo);
return "item";
}
}
package com.alatus.search.web;
import com.alatus.search.vo.SearchParam;
import com.alatus.search.vo.SearchResult;
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 com.alatus.search.service.MallSearchService;
import javax.servlet.http.HttpServletRequest;
@Controller
public class SearchController {
@Autowired
private MallSearchService mallSearchService;
@GetMapping("/list.html")
public String listPage(SearchParam searchParam, Model model, HttpServletRequest request){
searchParam.set_queryString(request.getQueryString());
SearchResult result = mallSearchService.search(searchParam);
model.addAttribute("result",result);
return "list";
}
}
package com.alatus.search.web;
import com.alatus.search.vo.SearchParam;
import com.alatus.search.vo.SearchResult;
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 com.alatus.search.service.MallSearchService;
import javax.servlet.http.HttpServletRequest;
@Controller
public class SearchController {
@Autowired
private MallSearchService mallSearchService;
@GetMapping("/list.html")
public String listPage(SearchParam searchParam, Model model, HttpServletRequest request){
searchParam.set_queryString(request.getQueryString());
SearchResult result = mallSearchService.search(searchParam);
model.addAttribute("result",result);
return "list";
}
}
package com.alatus.mall.product.app;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 商品三级分类
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:05:30
*/
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/index/catalog.json")
public Map<String, List<SubCatalogVo>> getCatalogJson(){
return categoryService.getCatalogJson();
}
/**
* 查出所有分类以及子分类,并以树形结构组装
*/
@RequestMapping("/list/tree")
public R listWithTree(){
List<CategoryEntity> entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = categoryService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@Cacheable(value = "category",key = "#root.getMethodName()",sync = true)
@RequestMapping("/info/{catId}")
public R catInfo(@PathVariable("catId") Long catId){
CategoryEntity category = categoryService.getById(catId);
return R.ok().put("category", category);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody CategoryEntity category){
categoryService.save(category);
return R.ok();
}
/**
* 批量修改
*/
@RequestMapping("/update/sort")
public R updateSort(@RequestBody CategoryEntity[] categories){
categoryService.updateBatchById(Arrays.asList(categories));
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateCascade(category);
return R.ok();
}
/**
* 删除
*/
// @RequestBody注解是为了获取请求体的内容,因为只有Post采有,所以必须是Post请求
// SpringMvc自动将我们请求体的数据Json转为对象
@RequestMapping("/delete")
public R delete(@RequestBody Long[] catIds){
// 检查当前删除的菜单是否被其他地方引用
categoryService.removeMenuByIds(Arrays.asList(catIds));
return R.ok();
}
}
package com.alatus.mall.product.app;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.alatus.mall.product.vo.SubCatalogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 商品三级分类
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:05:30
*/
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@GetMapping("/index/catalog.json")
public Map<String, List<SubCatalogVo>> getCatalogJson(){
return categoryService.getCatalogJson();
}
/**
* 查出所有分类以及子分类,并以树形结构组装
*/
@RequestMapping("/list/tree")
public R listWithTree(){
List<CategoryEntity> entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = categoryService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@Cacheable(value = "category",key = "#root.getMethodName()",sync = true)
@RequestMapping("/info/{catId}")
public R catInfo(@PathVariable("catId") Long catId){
CategoryEntity category = categoryService.getById(catId);
return R.ok().put("category", category);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody CategoryEntity category){
categoryService.save(category);
return R.ok();
}
/**
* 批量修改
*/
@RequestMapping("/update/sort")
public R updateSort(@RequestBody CategoryEntity[] categories){
categoryService.updateBatchById(Arrays.asList(categories));
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateCascade(category);
return R.ok();
}
/**
* 删除
*/
// @RequestBody注解是为了获取请求体的内容,因为只有Post采有,所以必须是Post请求
// SpringMvc自动将我们请求体的数据Json转为对象
@RequestMapping("/delete")
public R delete(@RequestBody Long[] catIds){
// 检查当前删除的菜单是否被其他地方引用
categoryService.removeMenuByIds(Arrays.asList(catIds));
return R.ok();
}
}