添加商品收藏

This commit is contained in:
xiaofeng 2019-07-03 00:01:24 +08:00
parent 5c2c4da6fa
commit ab04382852
5 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package cn.iocoder.mall.product.application.controller.users;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.product.api.ProductSpuCollectionService;
import cn.iocoder.mall.user.sdk.annotation.RequiresLogin;
import cn.iocoder.mall.user.sdk.context.UserSecurityContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* 商品收藏接口
* @author xiaofeng
* @date 2019/07/01 23:21
* @version 1.0
*/
@RestController
@RequestMapping("users/spu")
@Api("商品收藏")
public class UsersProductSpuCollectionController {
@Reference(validation = "true", version = "${dubbo.provider.ProductSpuCollectionService.version}")
private ProductSpuCollectionService productSpuCollectionService;
@PostMapping("/collection/{spuId}/{hasCollectionType}")
@ApiOperation("商品收藏")
// @RequiresLogin
public CommonResult<Boolean> productSpuCollection(@PathVariable("spuId") Integer spuId,
@PathVariable("hasCollectionType") Integer hasCollectionType) {
// final Integer userId = UserSecurityContextHolder.getContext().getUserId();
return success(productSpuCollectionService.productSpuCollection(spuId, hasCollectionType,140));
}
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.mall.product.api;
/**
* 商品收藏
* @author xiaofeng
* @date 2019/07/01 23:13
* @version 1.0
*/
public interface ProductSpuCollectionService {
/**
* 商品收藏
* @param spuId
* @param hasCollectionType 1 商品收藏 2 取消收藏
* @param userId
* @return
*/
boolean productSpuCollection(Integer spuId,Integer hasCollectionType,Integer userId);
}

View File

@ -0,0 +1,46 @@
package cn.iocoder.mall.product.api.message;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 商品收藏或取消收藏时发送消息
* @author xiaofeng
* @date 2019/07/01 22:33
* @version 1.0
*/
@Data
@Accessors(chain = true)
public class ProductSpuCollectionMessage {
public static final String TOPIC = "ProductSpuCollection";
/**
* SPU 编号
*/
private Integer spuId;
/**
* 用户ID
*/
private Integer userId;
// ========== 基本信息 =========
/**
* SPU 名字
*/
private String spuName;
/**
* 商品图片
*/
private String spuImage;
/**
* 1 收藏 2 取消
*/
private Integer hasCollectionType;
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.mall.product.service;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.mall.product.api.ProductSpuCollectionService;
import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum;
import cn.iocoder.mall.product.api.message.ProductSpuCollectionMessage;
import cn.iocoder.mall.product.dao.ProductSpuMapper;
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* ProductSpuCollectionServiceImpl
* @author xiaofeng
* @date 2019/07/01 23:14
* @version 1.0
*/
@Service // 实际上不用添加添加的原因是必须 Spring 报错提示
@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.ProductSpuCollectionService.version}")
public class ProductSpuCollectionServiceImpl implements ProductSpuCollectionService {
@Autowired
private ProductSpuMapper productSpuMapper;
@Resource
private RocketMQTemplate rocketMQTemplate;
@Override
public boolean productSpuCollection(Integer spuId, Integer hasCollectionType, Integer userId) {
ProductSpuDO productSpuDO = this.productSpuMapper.selectById(spuId);
// 校验 Spu 是否存在
if (productSpuDO == null) {
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_NOT_EXISTS.getCode());
}
this.sendProductSpuCollectionMessage(productSpuDO, hasCollectionType, userId);
return Boolean.TRUE;
}
/**
* 发送商品收藏或取消消息
* @param productSpuDO
* @param hasCollectionType
*/
private void sendProductSpuCollectionMessage(final ProductSpuDO productSpuDO, final Integer hasCollectionType,
final Integer userId) {
ProductSpuCollectionMessage productSpuCollectionMessage = new ProductSpuCollectionMessage()
.setSpuId(productSpuDO.getId()).setSpuName(productSpuDO.getName())
.setSpuImage(productSpuDO.getPicUrls()).setHasCollectionType(hasCollectionType)
.setUserId(userId);
rocketMQTemplate.convertAndSend(ProductSpuCollectionMessage.TOPIC, productSpuCollectionMessage);
}
}

View File

@ -35,6 +35,8 @@ dubbo:
version: 1.0.0 version: 1.0.0
OAuth2Service: OAuth2Service:
version: 1.0.0 version: 1.0.0
ProductSpuCollectionService:
version: 1.0.0
# rocketmq # rocketmq
rocketmq: rocketmq: