Skip to content

Commit

Permalink
标签查询
Browse files Browse the repository at this point in the history
  • Loading branch information
itwanger committed May 29, 2023
1 parent 5b3f556 commit 23733e7
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.paicoding.forum.api.model.vo.article;

import lombok.Data;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/27/23
*/
@Data
public class SearchCategoryReq {
// 类目名称
private String category;
// 分页
private Long pageNumber;
private Long pageSize;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.paicoding.forum.api.model.vo.article;

import lombok.Data;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/29/23
*/
@Data
public class SearchTagReq {
// 标签名称
private String tag;
// 分页
private Long pageNumber;
private Long pageSize;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.paicoding.forum.service.article.conveter;

import com.github.paicoding.forum.api.model.vo.article.SearchCategoryReq;
import com.github.paicoding.forum.api.model.vo.article.dto.CategoryDTO;
import com.github.paicoding.forum.service.article.repository.entity.CategoryDO;
import com.github.paicoding.forum.service.article.repository.params.SearchCategoryParams;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/27/23
*/
@Mapper
public interface CategoryStructMapper {
// instance
CategoryStructMapper INSTANCE = Mappers.getMapper( CategoryStructMapper.class );

// req to params
@Mapping(source = "pageNumber", target = "pageNum")
SearchCategoryParams toSearchParams(SearchCategoryReq req);

// do to dto
@Mapping(source = "id", target = "categoryId")
@Mapping(source = "categoryName", target = "category")
CategoryDTO toDTO(CategoryDO categoryDO);

List<CategoryDTO> toDTOs(List<CategoryDO> list);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.paicoding.forum.service.article.conveter;

import com.github.paicoding.forum.api.model.vo.article.SearchTagReq;
import com.github.paicoding.forum.api.model.vo.article.dto.TagDTO;
import com.github.paicoding.forum.service.article.repository.entity.TagDO;
import com.github.paicoding.forum.service.article.repository.params.SearchTagParams;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/29/23
*/
@Mapper
public interface TagStructMapper {
// instance
TagStructMapper INSTANCE = Mappers.getMapper( TagStructMapper.class );

// req to params
@Mapping(source = "pageNumber", target = "pageNum")
SearchTagParams toSearchParams(SearchTagReq req);

// do to dto
@Mapping(source = "id", target = "tagId")
@Mapping(source = "tagName", target = "tag")
TagDTO toDTO(TagDO tagDO);

List<TagDTO> toDTOs(List<TagDO> list);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.github.paicoding.forum.service.article.repository.dao;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.paicoding.forum.api.model.enums.PushStatusEnum;
import com.github.paicoding.forum.api.model.enums.YesOrNoEnum;
import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.article.dto.CategoryDTO;
import com.github.paicoding.forum.service.article.conveter.ArticleConverter;
import com.github.paicoding.forum.service.article.conveter.CategoryStructMapper;
import com.github.paicoding.forum.service.article.repository.entity.CategoryDO;
import com.github.paicoding.forum.service.article.repository.mapper.CategoryMapper;
import com.github.paicoding.forum.service.article.repository.params.SearchCategoryParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -30,29 +33,36 @@ public List<CategoryDO> listAllCategoriesFromDb() {
.list();
}

// 抽一个私有方法,构造查询条件
private LambdaQueryChainWrapper<CategoryDO> createCategoryQuery(SearchCategoryParams params) {
return lambdaQuery()
.eq(CategoryDO::getDeleted, YesOrNoEnum.NO.getCode())
.like(StringUtils.isNotBlank(params.getCategory()), CategoryDO::getCategoryName, params.getCategory());
}

/**
* 获取所有 Categorys 列表(分页)
*
* @return
*/
public List<CategoryDTO> listCategory(PageParam pageParam) {
List<CategoryDO> list = lambdaQuery()
.eq(CategoryDO::getDeleted, YesOrNoEnum.NO.getCode())
public List<CategoryDTO> listCategory(SearchCategoryParams params) {
List<CategoryDO> list = createCategoryQuery(params)
.orderByDesc(CategoryDO::getUpdateTime)
.orderByAsc(CategoryDO::getRank)
.last(PageParam.getLimitSql(pageParam))
.last(PageParam.getLimitSql(
PageParam.newPageInstance(params.getPageNum(), params.getPageSize())
))
.list();
return ArticleConverter.toCategoryDtoList(list);
return CategoryStructMapper.INSTANCE.toDTOs(list);
}

/**
* 获取所有 Categorys 总数(分页)
*
* @return
*/
public Integer countCategory() {
return lambdaQuery()
.eq(CategoryDO::getDeleted, YesOrNoEnum.NO.getCode())
.count()
.intValue();
public Long countCategory(SearchCategoryParams params) {
return createCategoryQuery(params)
.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.paicoding.forum.api.model.enums.PushStatusEnum;
import com.github.paicoding.forum.api.model.enums.YesOrNoEnum;
import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.article.dto.TagDTO;
import com.github.paicoding.forum.service.article.conveter.ArticleConverter;
import com.github.paicoding.forum.service.article.conveter.TagStructMapper;
import com.github.paicoding.forum.service.article.repository.entity.TagDO;
import com.github.paicoding.forum.service.article.repository.mapper.TagMapper;
import com.github.paicoding.forum.service.article.repository.params.SearchTagParams;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand Down Expand Up @@ -54,30 +57,37 @@ public Integer countOnlineTag(String key) {
.intValue();
}

private LambdaQueryChainWrapper<TagDO> createTagQuery(SearchTagParams params) {
return lambdaQuery()
.eq(TagDO::getDeleted, YesOrNoEnum.NO.getCode())
.like(StringUtils.isNotBlank(params.getTag()), TagDO::getTagName, params.getTag());
}

/**
* 获取所有 Tags 列表(分页)
*
* @return
*/
public List<TagDTO> listTag(PageParam pageParam) {
List<TagDO> list = lambdaQuery()
.eq(TagDO::getDeleted, YesOrNoEnum.NO.getCode())
.orderByDesc(TagDO::getId)
.last(PageParam.getLimitSql(pageParam))
public List<TagDTO> listTag(SearchTagParams params) {
List<TagDO> list = createTagQuery(params)
.orderByDesc(TagDO::getUpdateTime)
.last(PageParam.getLimitSql(
PageParam.newPageInstance(params.getPageNum(), params.getPageSize())
))
.list();
return ArticleConverter.toDtoList(list);
return TagStructMapper.INSTANCE.toDTOs(list);
}



/**
* 获取所有 Tags 总数(分页)
*
* @return
*/
public Integer countTag() {
return lambdaQuery()
.eq(TagDO::getDeleted, YesOrNoEnum.NO.getCode())
.count()
.intValue();
public Long countTag(SearchTagParams params) {
return createTagQuery(params)
.count();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.paicoding.forum.service.article.repository.params;

import com.github.paicoding.forum.api.model.vo.PageParam;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/27/23
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class SearchCategoryParams extends PageParam {
// 类目名称
private String category;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.paicoding.forum.service.article.repository.params;

import com.github.paicoding.forum.api.model.vo.PageParam;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* 微信搜索「沉默王二」,回复 Java
*
* @author 沉默王二
* @date 5/29/23
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class SearchTagParams extends PageParam {
// 标签名称
private String tag;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.paicoding.forum.service.article.service;

import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.PageVo;
import com.github.paicoding.forum.api.model.vo.article.CategoryReq;
import com.github.paicoding.forum.api.model.vo.article.SearchCategoryReq;
import com.github.paicoding.forum.api.model.vo.article.dto.CategoryDTO;

/**
Expand All @@ -25,5 +25,5 @@ public interface CategorySettingService {
* @param pageParam
* @return
*/
PageVo<CategoryDTO> getCategoryList(PageParam pageParam);
PageVo<CategoryDTO> getCategoryList(SearchCategoryReq params);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.paicoding.forum.service.article.service;

import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.PageVo;
import com.github.paicoding.forum.api.model.vo.article.SearchTagReq;
import com.github.paicoding.forum.api.model.vo.article.TagReq;
import com.github.paicoding.forum.api.model.vo.article.dto.TagDTO;

Expand All @@ -25,7 +25,7 @@ public interface TagSettingService {
* @param pageParam
* @return
*/
PageVo<TagDTO> getTagList(PageParam pageParam);
PageVo<TagDTO> getTagList(SearchTagReq req);

TagDTO getTagById(Long tagId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.paicoding.forum.service.article.service.impl;

import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.PageVo;
import com.github.paicoding.forum.api.model.vo.article.CategoryReq;
import com.github.paicoding.forum.api.model.vo.article.SearchCategoryReq;
import com.github.paicoding.forum.api.model.vo.article.dto.CategoryDTO;
import com.github.paicoding.forum.core.util.NumUtil;
import com.github.paicoding.forum.service.article.conveter.ArticleConverter;
import com.github.paicoding.forum.service.article.conveter.CategoryStructMapper;
import com.github.paicoding.forum.service.article.repository.dao.CategoryDao;
import com.github.paicoding.forum.service.article.repository.entity.CategoryDO;
import com.github.paicoding.forum.service.article.repository.params.SearchCategoryParams;
import com.github.paicoding.forum.service.article.service.CategoryService;
import com.github.paicoding.forum.service.article.service.CategorySettingService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -62,9 +64,12 @@ public void operateCategory(Integer categoryId, Integer pushStatus) {
}

@Override
public PageVo<CategoryDTO> getCategoryList(PageParam pageParam) {
List<CategoryDTO> tagDTOS = categoryDao.listCategory(pageParam);
Integer totalCount = categoryDao.countCategory();
return PageVo.build(tagDTOS, pageParam.getPageSize(), pageParam.getPageNum(), totalCount);
public PageVo<CategoryDTO> getCategoryList(SearchCategoryReq req) {
// 转换
SearchCategoryParams params = CategoryStructMapper.INSTANCE.toSearchParams(req);
// 查询
List<CategoryDTO> categoryDTOS = categoryDao.listCategory(params);
Long totalCount = categoryDao.countCategory(params);
return PageVo.build(categoryDTOS, params.getPageSize(), params.getPageNum(), totalCount);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.github.paicoding.forum.service.article.service.impl;

import com.github.paicoding.forum.api.model.vo.PageParam;
import com.github.paicoding.forum.api.model.vo.PageVo;
import com.github.paicoding.forum.api.model.vo.article.SearchTagReq;
import com.github.paicoding.forum.api.model.vo.article.TagReq;
import com.github.paicoding.forum.api.model.vo.article.dto.TagDTO;
import com.github.paicoding.forum.core.cache.RedisClient;
import com.github.paicoding.forum.core.util.JsonUtil;
import com.github.paicoding.forum.core.util.NumUtil;
import com.github.paicoding.forum.service.article.conveter.ArticleConverter;
import com.github.paicoding.forum.service.article.repository.dao.CategoryDao;
import com.github.paicoding.forum.service.article.conveter.TagStructMapper;
import com.github.paicoding.forum.service.article.repository.dao.TagDao;
import com.github.paicoding.forum.service.article.repository.entity.TagDO;
import com.github.paicoding.forum.service.article.repository.params.SearchTagParams;
import com.github.paicoding.forum.service.article.service.TagSettingService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

Expand Down Expand Up @@ -84,10 +83,13 @@ public void operateTag(Integer tagId, Integer pushStatus) {
}

@Override
public PageVo<TagDTO> getTagList(PageParam pageParam) {
List<TagDTO> tagDTOS = tagDao.listTag(pageParam);
Integer totalCount = tagDao.countTag();
return PageVo.build(tagDTOS, pageParam.getPageSize(), pageParam.getPageNum(), totalCount);
public PageVo<TagDTO> getTagList(SearchTagReq req) {
// 转换
SearchTagParams params = TagStructMapper.INSTANCE.toSearchParams(req);
// 查询
List<TagDTO> tagDTOS = tagDao.listTag(params);
Long totalCount = tagDao.countTag(params);
return PageVo.build(tagDTOS, params.getPageSize(), params.getPageNum(), totalCount);
}

@Override
Expand Down
Loading

0 comments on commit 23733e7

Please sign in to comment.