Skip to content

Commit 61284ca

Browse files
committed
feat: consumer, category
1 parent 6fb7cd5 commit 61284ca

13 files changed

Lines changed: 781 additions & 1 deletion

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.hackyle.blog.consumer.controller;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.hackyle.blog.consumer.dto.PageRequestDto;
5+
import com.hackyle.blog.consumer.dto.PageResponseDto;
6+
import com.hackyle.blog.consumer.qo.CategoryQo;
7+
import com.hackyle.blog.consumer.service.ArticleCategoryService;
8+
import com.hackyle.blog.consumer.vo.ArticleVo;
9+
import com.hackyle.blog.consumer.vo.CategoryVo;
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.stereotype.Controller;
15+
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestMethod;
18+
import org.springframework.web.servlet.ModelAndView;
19+
20+
import javax.servlet.http.HttpServletRequest;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
/**
25+
* 分类:读取所有的文章分类,渲染到页面category.html。
26+
* 点击分类,获取该目录下的所有文章,category/分类名/页码
27+
*/
28+
@Controller
29+
public class CategoryController {
30+
private static final Logger LOGGER = LoggerFactory.getLogger(CategoryController.class);
31+
32+
@Autowired
33+
private ArticleCategoryService articleCategoryService;
34+
35+
/**
36+
* 查询文章分类
37+
*/
38+
@RequestMapping(value = {"/category"}, method = {RequestMethod.GET, RequestMethod.POST})
39+
public ModelAndView queryCategory(ModelAndView modelAndView) {
40+
//直接获取全部有文章的分类就行了,不需要根据关键字查文章分类
41+
//String categoryKeys = request.getParameter("categoryKeys");
42+
//LOGGER.info("查询文章分类-controller层入参-queryKeywords={}", categoryKeys);
43+
//if(StringUtils.isNotBlank(categoryKeys)) {
44+
// modelAndView.addObject("categoryKeys", categoryKeys);
45+
//}
46+
47+
List<CategoryVo> categoryVoList = articleCategoryService.selectCategory();
48+
49+
modelAndView.addObject("categoryVoList", categoryVoList);
50+
LOGGER.info("查询文章分类-controller层出参-categoryVoList={}", JSON.toJSONString(categoryVoList));
51+
52+
modelAndView.setViewName("category");
53+
return modelAndView;
54+
}
55+
56+
/**
57+
* 分页获取某分类下的所有文章
58+
*/
59+
@RequestMapping(value = {"/category/{categoryCode}/{pageNum}"}, method = {RequestMethod.GET, RequestMethod.POST})
60+
public ModelAndView categoryArticle(ModelAndView modelAndView, @PathVariable("categoryCode") String categoryCode,
61+
@PathVariable("pageNum") Integer pageNum, HttpServletRequest request) {
62+
PageRequestDto<CategoryQo> pageRequestDto = new PageRequestDto<>();
63+
CategoryQo categoryQo = new CategoryQo();
64+
categoryQo.setCategoryCode(categoryCode);
65+
66+
if(pageNum == null || pageNum < 1) {
67+
pageNum = 1;
68+
}
69+
pageRequestDto.setCurrentPage(pageNum);
70+
71+
String pageSize = request.getParameter("pageSize");
72+
if(StringUtils.isBlank(pageSize)) {
73+
pageRequestDto.setPageSize(15);
74+
} else {
75+
pageRequestDto.setPageSize(Integer.parseInt(pageSize));
76+
}
77+
78+
//多个关键字,使用逗号分割
79+
String categoryKeys = request.getParameter("categoryKeys");
80+
if(StringUtils.isNotBlank(categoryKeys)) {
81+
categoryQo.setQueryKeywords(categoryKeys);
82+
modelAndView.addObject("categoryKeys", categoryKeys);
83+
}
84+
pageRequestDto.setCondition(categoryQo);
85+
86+
LOGGER.info("分页获取某分类下的所有文章-controller入参-pageRequestDto={}", JSON.toJSONString(pageRequestDto));
87+
PageResponseDto<ArticleVo> pageResponseDto = articleCategoryService.selectArticleByCategory(pageRequestDto);
88+
String articleUris = pageResponseDto.getRows().stream().map(ArticleVo::getUri).collect(Collectors.joining(","));
89+
LOGGER.info("分页获取某分类下的所有文章-controller出参-pageRequestDto={}-articleUris={}", JSON.toJSONString(pageRequestDto), articleUris);
90+
91+
modelAndView.addObject("pageResponseDto", pageResponseDto);
92+
modelAndView.addObject("categoryCode", categoryCode);
93+
modelAndView.addObject("categoryName", pageRequestDto.getCondition().getCategoryName());
94+
95+
modelAndView.setViewName("category");
96+
return modelAndView;
97+
}
98+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.hackyle.blog.consumer.entity;
2+
3+
import com.baomidou.mybatisplus.annotation.TableField;
4+
import com.baomidou.mybatisplus.annotation.TableId;
5+
import com.baomidou.mybatisplus.annotation.TableName;
6+
7+
import java.io.Serializable;
8+
import java.util.Date;
9+
10+
/**
11+
* 文章分类
12+
*/
13+
@TableName(value ="tb_category")
14+
public class CategoryEntity implements Serializable {
15+
/**
16+
* ID:为了后续数据迁移,不使用自增主键,使用时间戳
17+
*/
18+
@TableId
19+
private Long id;
20+
21+
/**
22+
* 分类名称
23+
*/
24+
private String name;
25+
26+
/**
27+
* 分类简称
28+
*/
29+
private String code;
30+
31+
/**
32+
* 分类描述
33+
*/
34+
private String description;
35+
36+
/**
37+
* 分类的图标URL
38+
*/
39+
private String iconUrl;
40+
41+
/**
42+
* 上一级分类
43+
*/
44+
private Long parentId;
45+
46+
/**
47+
* 创建时间: 年-月-日 时:分:秒
48+
*/
49+
private Date createTime;
50+
51+
/**
52+
* 更新时间
53+
*/
54+
private Date updateTime;
55+
56+
/**
57+
* 是否删除:0-false-未删除;1-true-已删除
58+
*/
59+
@TableField("is_deleted")
60+
private Boolean deleted;
61+
62+
@TableField(exist = false)
63+
private static final long serialVersionUID = 1L;
64+
65+
public Long getId() {
66+
return id;
67+
}
68+
69+
public void setId(Long id) {
70+
this.id = id;
71+
}
72+
73+
public String getName() {
74+
return name;
75+
}
76+
77+
public void setName(String name) {
78+
this.name = name;
79+
}
80+
81+
public String getCode() {
82+
return code;
83+
}
84+
85+
public void setCode(String code) {
86+
this.code = code;
87+
}
88+
89+
public String getDescription() {
90+
return description;
91+
}
92+
93+
public void setDescription(String description) {
94+
this.description = description;
95+
}
96+
97+
public String getIconUrl() {
98+
return iconUrl;
99+
}
100+
101+
public void setIconUrl(String iconUrl) {
102+
this.iconUrl = iconUrl;
103+
}
104+
105+
public Long getParentId() {
106+
return parentId;
107+
}
108+
109+
public void setParentId(Long parentId) {
110+
this.parentId = parentId;
111+
}
112+
113+
public Date getCreateTime() {
114+
return createTime;
115+
}
116+
117+
public void setCreateTime(Date createTime) {
118+
this.createTime = createTime;
119+
}
120+
121+
public Date getUpdateTime() {
122+
return updateTime;
123+
}
124+
125+
public void setUpdateTime(Date updateTime) {
126+
this.updateTime = updateTime;
127+
}
128+
129+
public Boolean getDeleted() {
130+
return deleted;
131+
}
132+
133+
public void setDeleted(Boolean deleted) {
134+
this.deleted = deleted;
135+
}
136+
}

blog-consumer/src/main/java/com/hackyle/blog/consumer/mapper/ArticleCategoryMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
44
import com.hackyle.blog.consumer.entity.ArticleCategoryEntity;
5+
import com.hackyle.blog.consumer.entity.CategoryEntity;
56
import com.hackyle.blog.consumer.po.ArticleCategoryPo;
67
import org.apache.ibatis.annotations.Mapper;
78
import org.apache.ibatis.annotations.Param;
@@ -12,6 +13,13 @@
1213
public interface ArticleCategoryMapper extends BaseMapper<ArticleCategoryEntity> {
1314

1415
List<ArticleCategoryPo> selectByArticleIds(@Param("articleIds") List<Long> articleIds);
16+
17+
//List<ArticleCategoryPo> queryCategory(@Param("keyList") List<String> keyList);
18+
List<ArticleCategoryPo> queryCategory();
19+
20+
CategoryEntity selectCategoryByCategory(@Param("categoryCode") String categoryCode);
21+
22+
List<Long> selectArticleByCategory(@Param("categoryCode") String categoryCode);
1523
}
1624

1725

blog-consumer/src/main/java/com/hackyle/blog/consumer/po/ArticleCategoryPo.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.hackyle.blog.consumer.po;
22

3+
import java.util.Date;
4+
35
public class ArticleCategoryPo {
46
private Long articleId;
57

68
private Long categoryId;
79

10+
/**
11+
* 该个categoryId下的文章数量
12+
*/
13+
private Integer articleNum;
14+
815
/**
916
* 分类名称
1017
*/
@@ -25,6 +32,12 @@ public class ArticleCategoryPo {
2532
*/
2633
private String iconUrl;
2734

35+
private Long parentId;
36+
37+
private Date createTime;
38+
39+
private Date updateTime;
40+
2841
public Long getArticleId() {
2942
return articleId;
3043
}
@@ -41,6 +54,14 @@ public void setCategoryId(Long categoryId) {
4154
this.categoryId = categoryId;
4255
}
4356

57+
public Integer getArticleNum() {
58+
return articleNum;
59+
}
60+
61+
public void setArticleNum(Integer articleNum) {
62+
this.articleNum = articleNum;
63+
}
64+
4465
public String getName() {
4566
return name;
4667
}
@@ -72,4 +93,28 @@ public String getIconUrl() {
7293
public void setIconUrl(String iconUrl) {
7394
this.iconUrl = iconUrl;
7495
}
96+
97+
public Long getParentId() {
98+
return parentId;
99+
}
100+
101+
public void setParentId(Long parentId) {
102+
this.parentId = parentId;
103+
}
104+
105+
public Date getCreateTime() {
106+
return createTime;
107+
}
108+
109+
public void setCreateTime(Date createTime) {
110+
this.createTime = createTime;
111+
}
112+
113+
public Date getUpdateTime() {
114+
return updateTime;
115+
}
116+
117+
public void setUpdateTime(Date updateTime) {
118+
this.updateTime = updateTime;
119+
}
75120
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.hackyle.blog.consumer.qo;
2+
3+
public class CategoryQo {
4+
private String categoryCode;
5+
private String categoryName;
6+
private String queryKeywords;
7+
8+
public String getCategoryCode() {
9+
return categoryCode;
10+
}
11+
12+
public void setCategoryCode(String categoryCode) {
13+
this.categoryCode = categoryCode;
14+
}
15+
16+
public String getCategoryName() {
17+
return categoryName;
18+
}
19+
20+
public void setCategoryName(String categoryName) {
21+
this.categoryName = categoryName;
22+
}
23+
24+
public String getQueryKeywords() {
25+
return queryKeywords;
26+
}
27+
28+
public void setQueryKeywords(String queryKeywords) {
29+
this.queryKeywords = queryKeywords;
30+
}
31+
}

blog-consumer/src/main/java/com/hackyle/blog/consumer/service/ArticleCategoryService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.hackyle.blog.consumer.service;
22

33
import com.baomidou.mybatisplus.extension.service.IService;
4+
import com.hackyle.blog.consumer.dto.PageRequestDto;
5+
import com.hackyle.blog.consumer.dto.PageResponseDto;
46
import com.hackyle.blog.consumer.entity.ArticleCategoryEntity;
57
import com.hackyle.blog.consumer.po.ArticleCategoryPo;
8+
import com.hackyle.blog.consumer.qo.CategoryQo;
9+
import com.hackyle.blog.consumer.vo.ArticleVo;
10+
import com.hackyle.blog.consumer.vo.CategoryVo;
611

712
import java.util.List;
813
import java.util.Map;
@@ -11,4 +16,8 @@ public interface ArticleCategoryService extends IService<ArticleCategoryEntity>
1116

1217
Map<Long, List<ArticleCategoryPo>> selectByArticleIds(List<Long> articleIds);
1318

19+
List<CategoryVo> selectCategory();
20+
21+
PageResponseDto<ArticleVo> selectArticleByCategory(PageRequestDto<CategoryQo> requestDto);
22+
1423
}

0 commit comments

Comments
 (0)