Update UserCollectionController.java

cyj
pbvfus8to 8 months ago
parent a6fdb601be
commit b383062b10

@ -8,13 +8,13 @@
*
*/
// 该类所属的包名表明其位于商城API的控制器包下主要用于处理与用户收藏相关的各种接口请求及对应的业务逻辑比如获取收藏数据、判断是否收藏、添加或取消收藏等操作。
package com.yami.shop.api.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.app.dto.UserCollectionDto;
import com.yaami.shop.bean.app.dto.UserCollectionDto;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.UserCollection;
import com.yami.shop.common.exception.YamiShopBindException;
@ -31,51 +31,87 @@ import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Objects;
/**
* UserCollectionControllerSpring RESTful
* UserCollectionServiceProductServiceSwagger便使
*
* @author lanhai
*/
@RestController
// 定义该控制器类的基础请求路径,所有该类中的接口请求路径都将以此为前缀,表明是与用户收藏相关的操作接口。
@RequestMapping("/p/user/collection")
// 使用Swagger的@Tag注解对该控制器类进行标记用于在API文档中生成对应的分类标签方便接口文档的分类展示和阅读这里表示该类下的接口都属于“收藏接口”这一分类。
@Tag(name = "收藏接口")
// 使用lombok的@AllArgsConstructor注解自动生成包含所有final字段的构造函数方便依赖注入这里会为UserCollectionService和ProductService生成对应的构造函数参数以便在类中使用这两个服务类的实例。
@AllArgsConstructor
public class UserCollectionController {
// 通过构造函数注入UserCollectionService实例用于调用与用户收藏记录相关的业务逻辑方法比如查询收藏记录、添加或删除收藏记录等操作。
private final UserCollectionService userCollectionService;
// 通过构造函数注入ProductService实例用于调用与商品相关的业务逻辑方法例如判断商品是否存在、获取收藏商品列表等操作通常会结合用户收藏记录来实现相关业务功能。
private final ProductService productService;
/**
*
* pageUserCollectionServicegetUserCollectionDtoPageByUserIdIDSecurityUtils
* ServerResponseEntity
*
* @param page
* @return ServerResponseEntityIPage<UserCollectionDto>
*/
@GetMapping("/page")
@Operation(summary = "分页返回收藏数据" , description = "根据用户id获取")
@Operation(summary = "分页返回收藏数据", description = "根据用户id获取")
public ServerResponseEntity<IPage<UserCollectionDto>> getUserCollectionDtoPageByUserId(PageParam page) {
return ServerResponseEntity.success(userCollectionService.getUserCollectionDtoPageByUserId(page, SecurityUtils.getUser().getUserId()));
}
/**
* ID
* IDprodIdProductServicecountLambdaQueryWrapper1
* UserCollectionServicecountLambdaQueryWrapperIDIDSecurityUtils
* 0truefalseServerResponseEntity
*
* @param prodId
* @return ServerResponseEntityBooleantruefalse
*/
@GetMapping("isCollection")
@Operation(summary = "根据商品id获取该商品是否在收藏夹中" , description = "传入收藏商品id")
@Operation(summary = "根据商品id获取该商品是否在收藏夹中", description = "传入收藏商品id")
public ServerResponseEntity<Boolean> isCollection(Long prodId) {
if (productService.count(new LambdaQueryWrapper<Product>()
.eq(Product::getProdId, prodId)) < 1) {
.eq(Product::getProdId, prodId)) < 1) {
throw new YamiShopBindException("该商品不存在");
}
return ServerResponseEntity.success(userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, SecurityUtils.getUser().getUserId())) > 0);
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, SecurityUtils.getUser().getUserId())) > 0);
}
/**
* /
* IDprodIdProductServicegetProductByProdIdIDSecurityUtils
* UserCollectionServicecountLambdaQueryWrapper0removeLambdaQueryWrapper
* 0UserCollectionnew Date()IDIDsave
* ServerResponseEntityVoid
*
* @param prodId
* @return ServerResponseEntityVoid
*/
@PostMapping("/addOrCancel")
@Operation(summary = "添加/取消收藏" , description = "传入收藏商品id,如果商品未收藏则收藏商品,已收藏则取消收藏")
@Parameter(name = "prodId", description = "商品id" , required = true)
@Operation(summary = "添加/取消收藏", description = "传入收藏商品id,如果商品未收藏则收藏商品,已收藏则取消收藏")
@Parameter(name = "prodId", description = "商品id", required = true)
public ServerResponseEntity<Void> addOrCancel(@RequestBody Long prodId) {
if (Objects.isNull(productService.getProductByProdId(prodId))) {
throw new YamiShopBindException("该商品不存在");
}
String userId = SecurityUtils.getUser().getUserId();
if (userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, userId)) > 0) {
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, userId)) > 0) {
userCollectionService.remove(new LambdaQueryWrapper<UserCollection>()
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, userId));
.eq(UserCollection::getProdId, prodId)
.eq(UserCollection::getUserId, userId));
} else {
UserCollection userCollection = new UserCollection();
userCollection.setCreateTime(new Date());
@ -86,22 +122,36 @@ public class UserCollectionController {
return ServerResponseEntity.success();
}
/**
*
* SecurityUtilsIDUserCollectionServicecountLambdaQueryWrapperID
* ServerResponseEntity
*
* @return ServerResponseEntityLong
*/
/**
*
*/
@GetMapping("count")
@Operation(summary = "查询用户收藏商品数量" , description = "查询用户收藏商品数量")
@Operation(summary = "查询用户收藏商品数量", description = "查询用户收藏商品数量")
public ServerResponseEntity<Long> findUserCollectionCount() {
String userId = SecurityUtils.getUser().getUserId();
return ServerResponseEntity.success(userCollectionService.count(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId, userId)));
}
/**
*
* pageProductServicecollectionProdsIDSecurityUtils
* ServerResponseEntity
*
* @param page
* @return ServerResponseEntityIPage<ProductDto>
*/
@GetMapping("/prods")
@Operation(summary = "获取用户收藏商品列表" , description = "获取用户收藏商品列表")
@Operation(summary = "获取用户收藏商品列表", description = "获取用户收藏商品列表")
public ServerResponseEntity<IPage<ProductDto>> collectionProds(PageParam page) {
String userId = SecurityUtils.getUser().getUserId();
IPage<ProductDto> productDtoPage = productService.collectionProds(page, userId);
return ServerResponseEntity.success(productDtoPage);
}
}
}
Loading…
Cancel
Save