修改商品数量
程序员文章站
2022-05-22 23:51:09
...
修改商品数量
页面发起请求
后台实现
Controller
@PutMapping
public ResponseEntity<Void> updateNum(@RequestBody Cart cart){
this.cartService.updateCarts(cart);
return ResponseEntity.noContent().build();
}
Service
public void updateCarts(Cart cart) {
// 获取登陆信息
UserInfo userInfo = LoginInterceptor.getUserInfo();
String key = LEARN_CART_KEY + userInfo.getId();
// 获取hash操作对象
BoundHashOperations<String, Object, Object> hashOperations =
this.redisTemplate.boundHashOps(key);
// 获取购物车信息
String cartJson = hashOperations.get(cart.getSkuId().toString()).toString();
Cart cart1 = JsonUtils.parse(cartJson, Cart.class);
// 更新数量
cart1.setNum(cart.getNum());
// 写入购物车
hashOperations.put(cart.getSkuId().toString(), JsonUtils.serialize(cart1));
}
删除购物车商品
页面发起请求
注意:后台成功响应后,要把页面的购物车中数据也删除
后台实现
Controller
@DeleteMapping("{skuId}")
public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
this.cartService.deleteCart(skuId);
return ResponseEntity.ok().build();
}
Service
public void deleteCart(String skuId) {
// 获取登录用户
UserInfo user = LoginInterceptor.getLoginUser();
String key = KEY_PREFIX + user.getId();
BoundHashOperations<String, Object, Object> hashOps =
this.redisTemplate.boundHashOps(key);
hashOps.delete(skuId);
}
上一篇: 微软Band2进军车联网 语音遥控沃尔沃