Skip to content

Commit

Permalink
添加,查询购物车
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-1515 committed Jul 24, 2022
1 parent a341ec4 commit 5360e35
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class JwtTokenUserInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("拦截到了请求:{}", request.getRequestURI());

if (handler instanceof HandlerMethod) {
if (!(handler instanceof HandlerMethod)) {
//如果不是映射到controller某个方法的请求,则直接放行,例如请求的是/doc.html
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions reggie_server/src/main/java/com/reggie/ReggieApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
@EnableCaching //开启注解缓存
@Slf4j
public class ReggieApplication {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
Expand All @@ -24,6 +26,19 @@ public class DishController {
@Autowired
private DishService dishService;

@Autowired
private RedisTemplate redisTemplate;

/**
* 清理缓存
*
* @param pattern redis key值
*/
private void cleanCache(String pattern) {
Set keys = redisTemplate.keys(pattern);
redisTemplate.delete(keys);
}


/**
* 添加菜品
Expand All @@ -36,6 +51,8 @@ public class DishController {
public R<String> save(@RequestBody DishDTO dishDTO) {
log.info("添加菜品:{}", dishDTO);
dishService.save(dishDTO);

cleanCache("dish_"+dishDTO.getCategoryId());
return R.success("菜品添加成功");
}

Expand Down Expand Up @@ -65,6 +82,8 @@ public R<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
public R<String> delete(@RequestParam List<Long> ids) {
log.info("批量删除菜品:{}", ids);
dishService.delete(ids);

cleanCache("dish_*");
return R.success("批量删除菜品成功");
}

Expand All @@ -79,6 +98,7 @@ public R<String> delete(@RequestParam List<Long> ids) {
public R<DishVO> select(@PathVariable Long id) {
log.info("根据id查询菜品和关联的口味:{}", id);
DishVO dishVO = dishService.getByIdWithFlavor(id);

return R.success(dishVO);
}

Expand All @@ -93,6 +113,8 @@ public R<DishVO> select(@PathVariable Long id) {
public R<String> update(@RequestBody DishDTO dishDTO) {
log.info("修改菜品:{}", dishDTO);
dishService.updateWithFlavor(dishDTO);

cleanCache("dish_*");
return R.success("修改成功");
}

Expand All @@ -108,6 +130,7 @@ public R<String> update(@RequestBody DishDTO dishDTO) {
public R<String> startOrStop(@PathVariable Integer status, Long id) {
log.info("菜品起售、停售---status:{},id:{}", status, id);
dishService.startOrStop(status, id);
cleanCache("dish_*");
return R.success("状态修改成功");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -30,6 +31,7 @@ public class SetmealController {
*/
@PostMapping
@ApiOperation("新增套餐")
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public R<String> save(@RequestBody SetmealDTO setmealDTO) {
log.info("新增套餐:{}", setmealDTO);
setmealService.saveWithDish(setmealDTO);
Expand Down Expand Up @@ -58,6 +60,7 @@ public R<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
*/
@DeleteMapping
@ApiOperation("批量删除的套餐")
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public R<String> delete(@RequestParam List<Long> ids) {
log.info("批量删除:{}", ids);
setmealService.deleteBatch(ids);
Expand Down Expand Up @@ -86,6 +89,7 @@ public R<SetmealVO> getById(@PathVariable Long id) {
*/
@PutMapping
@ApiOperation("修改套餐")
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public R<String> update(@RequestBody SetmealDTO setmealDTO) {
log.info("修改套餐:{}", setmealDTO);
setmealService.update(setmealDTO);
Expand All @@ -100,9 +104,10 @@ public R<String> update(@RequestBody SetmealDTO setmealDTO) {
* @return success
*/
@PostMapping("/status/{status}")
@ApiOperation("修改套餐")
@ApiOperation("套餐 启用禁用")
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public R<String> startOrStop(@PathVariable Integer status, Long id) {
log.info("修改套餐:{}", status);
log.info("套餐 启用禁用:{}", status);
setmealService.startOrStop(status, id);
return R.success("状态修改成功");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reggie.controller.admin;

import com.reggie.constant.StatusConstant;
import com.reggie.result.R;
import com.reggie.service.ShopService;
import io.swagger.annotations.Api;
Expand Down Expand Up @@ -43,10 +44,10 @@ public R<Integer> getShopOrStatus() {
try {
shopStatus = shopService.getShopStatus();
} catch (Exception e) {
e.printStackTrace();
shopStatus = 1;
log.error(e.getMessage());
shopStatus = StatusConstant.ENABLE;
}
log.info("当前营业状态为{}",shopStatus);
log.info("当前营业状态为{}", shopStatus);
return R.success(shopStatus);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userCategoryController")
Expand All @@ -21,8 +22,9 @@ public class CategoryController {

/**
* 查询分类
* @param type
* @return
*
* @param type 分类类型
* @return 所有 菜品/套餐
*/
@GetMapping("/list")
@ApiOperation("查询分类")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
Expand All @@ -21,21 +23,39 @@ public class DishController {
@Autowired
private DishService dishService;

@Autowired
private RedisTemplate redisTemplate;

/**
* 根据分类id查询菜品
*
* @param categoryId
* @return
* @param categoryId 分类id
* @return 菜品和口味
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
public R<List<DishVO>> list(Long categoryId) {
//创建redisKey
String key = "dish_" + categoryId;

//从redis中获取缓存
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get("key");
if (list != null) {
//查询到缓存数据,直接返回
return R.success(list);
}

//redis没有就从数据库中拿出
//设置要查询的分类id和启用的状态
Dish dish = new Dish();
dish.setCategoryId(categoryId);
dish.setStatus(Dish.ON);

List<DishVO> list = dishService.listWithFlavor(dish);
//从数据库根据分类id查询菜品
list = dishService.listWithFlavor(dish);

//存入缓存中
redisTemplate.opsForValue().set(key, list);
return R.success(list);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userSetmealController")
Expand All @@ -28,6 +27,7 @@ public class SetmealController {
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询套餐")
@Cacheable(cacheNames = "setmealCache", key = "#categoryId", unless = "#result == null || #result.data.size() == 0")
public R<List<Setmeal>> list(Long categoryId) {
Setmeal setmeal = new Setmeal();
setmeal.setCategoryId(categoryId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reggie.controller.user;

import com.reggie.constant.StatusConstant;
import com.reggie.result.R;
import com.reggie.service.ShopService;
import io.swagger.annotations.Api;
Expand Down Expand Up @@ -33,7 +34,7 @@ public R<Integer> getShopOrStatus() {
shopStatus = shopService.getShopStatus();
} catch (Exception e) {
e.printStackTrace();
shopStatus = 1;
shopStatus = StatusConstant.ENABLE;
}
log.info("当前营业状态为{}", shopStatus);
return R.success(shopStatus);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.reggie.controller.user;

import com.reggie.dto.ShoppingCartDTO;
import com.reggie.entity.ShoppingCart;
import com.reggie.result.R;
import com.reggie.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* 购物车
*/
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {

@Autowired
private ShoppingCartService shoppingCartService;

/**
* 添加购物车
*
* @param shoppingCartDTO 购物车(口味,菜品id,套餐id)
* @return success
*/
@PostMapping("/add")
@ApiOperation("添加购物车接口")
public R<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
log.info("添加购物车,商品:{}", shoppingCartDTO);
shoppingCartService.add(shoppingCartDTO);
return R.success("购物车添加成功");
}


/**
* 查询购物车
*
* @return 购物车信息
*/
@GetMapping("/list")
public R<List<ShoppingCart>> list() {
log.info("查询购物车");
List<ShoppingCart> shoppingCarts = shoppingCartService.list();
return R.success(shoppingCarts);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.reggie.mapper;

import com.reggie.entity.ShoppingCart;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface ShoppingCartMapper {
/**
* 条件查询
*
* @param shoppingCart
* @return
*/
public List<ShoppingCart> list(ShoppingCart shoppingCart);

/**
* 更新商品数量
*
* @param shoppingCart
*/
public void updateNumberById(ShoppingCart shoppingCart);

/**
* 插入购物车数据
*
* @param shoppingCart
*/
public void insert(ShoppingCart shoppingCart);

/**
* 根据id删除购物车数据
*
* @param id
*/
public void deleteById(Long id);

/**
* 根据用户id删除购物车数据
*
* @param userId
*/
public void deleteByUserId(Long userId);

/**
* 批量插入购物车数据
*
* @param shoppingCartList
*/
public void insertBatch(List<ShoppingCart> shoppingCartList);
}
Loading

0 comments on commit 5360e35

Please sign in to comment.