Skip to content

Commit

Permalink
1、添加归档模块
Browse files Browse the repository at this point in the history
2、分页 UI 美化
  • Loading branch information
weiwosuoai committed Jul 16, 2023
1 parent 00a0e6c commit 0f0bf8f
Show file tree
Hide file tree
Showing 17 changed files with 740 additions and 133 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 简介

一款由 Spring Boot + Vue 3.2 开发的前后端分离博客。
一款由 Spring Boot + Vue 3.2 + Vite 4.3 开发的前后端分离博客。

![Weblog 后台仪表盘](https://img.quanxiaoha.com/quanxiaoha/168887767469647 "Weblog 后台仪表盘")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ public class PageResponse<T> extends Response<List<T>> {
private long size = 10L;
// 当前页码
private long current;
// 总页数
private long pages = 0L;

public static <T> PageResponse<T> success(IPage page, List<T> data) {
PageResponse<T> response = new PageResponse<>();
response.setSuccess(true);
response.setCurrent(Objects.isNull(page) ? 0 : page.getCurrent());
response.setTotal(Objects.isNull(page) ? 0 : page.getTotal());
response.setSize(Objects.isNull(page) ? 0 : page.getSize());
response.setPages(Objects.isNull(page) ? 0 : page.getPages());
response.setData(data);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public interface Constants {

DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

DateFormat MONTH_FORMAT = new SimpleDateFormat("yyyy-MM");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.quanxiaoha.weblog.web.controller;

import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.aspect.ApiOperationLog;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchivePageListReqVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryIndexArticlePageListReqVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryTagArticlePageListReqVO;
import com.quanxiaoha.weblog.web.service.ArchiveService;
import com.quanxiaoha.weblog.web.service.ArticleService;
import com.quanxiaoha.weblog.web.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author: 犬小哈
* @url: www.quanxiaoha.com
* @date: 2023-04-18 8:14
* @description: 归档
**/
@RestController
@RequestMapping("/archive")
public class ArchiveController {

@Autowired
private ArchiveService archiveService;

@PostMapping("/list")
@ApiOperationLog(description = "获取文章归档列表")
public Response queryArchive(@RequestBody QueryArchivePageListReqVO queryArchivePageListReqVO) {
return archiveService.queryArchive(queryArchivePageListReqVO);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.quanxiaoha.weblog.web.model.vo.archive;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class QueryArchiveItemRspVO {
private Long id;
private String titleImage;
private String title;
private String createMonth;
private String createTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.quanxiaoha.weblog.web.model.vo.archive;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class QueryArchivePageListReqVO {
private Long current = 1L;
private Long size = 10L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.quanxiaoha.weblog.web.model.vo.archive;


import com.quanxiaoha.weblog.web.model.vo.category.QueryCategoryListRspVO;
import com.quanxiaoha.weblog.web.model.vo.tag.QueryTagListRspVO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class QueryArchivePageListRspVO {
private String month;
private List<QueryArchiveItemRspVO> articles;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.quanxiaoha.weblog.web.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.domain.dos.ArticleDO;
import com.quanxiaoha.weblog.common.domain.dos.TagDO;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchivePageListReqVO;


public interface ArchiveService extends IService<ArticleDO> {

Response queryArchive(QueryArchivePageListReqVO queryArchivePageListReqVO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchivePageListReqVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryArticleDetailReqVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryCategoryArticlePageListReqVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryIndexArticlePageListReqVO;
Expand All @@ -17,4 +18,5 @@ public interface ArticleService {
Response queryArticleDetail(QueryArticleDetailReqVO queryArticleDetailReqVO);

PageResponse queryTagArticlePageList(QueryTagArticlePageListReqVO queryTagArticlePageListReqVO);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.quanxiaoha.weblog.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.constant.Constants;
import com.quanxiaoha.weblog.common.domain.dos.*;
import com.quanxiaoha.weblog.common.domain.mapper.ArticleMapper;
import com.quanxiaoha.weblog.common.domain.mapper.TagMapper;
import com.quanxiaoha.weblog.web.dao.ArticleDao;
import com.quanxiaoha.weblog.web.dao.TagDao;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchiveItemRspVO;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchivePageListReqVO;
import com.quanxiaoha.weblog.web.model.vo.archive.QueryArchivePageListRspVO;
import com.quanxiaoha.weblog.web.model.vo.article.QueryIndexArticlePageListRspVO;
import com.quanxiaoha.weblog.web.model.vo.category.QueryCategoryListRspVO;
import com.quanxiaoha.weblog.web.model.vo.tag.QueryTagListRspVO;
import com.quanxiaoha.weblog.web.service.ArchiveService;
import com.quanxiaoha.weblog.web.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.YearMonth;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author: 犬小哈
* @url: www.quanxiaoha.com
* @date: 2023-04-17 12:08
* @description: TODO
**/
@Service
@Slf4j
public class ArchiveServiceImpl extends ServiceImpl<ArticleMapper, ArticleDO> implements ArchiveService {

@Autowired
private ArticleDao articleDao;

@Override
public Response queryArchive(QueryArchivePageListReqVO queryArchivePageListReqVO) {
Long current = queryArchivePageListReqVO.getCurrent();
Long size = queryArchivePageListReqVO.getSize();

IPage<ArticleDO> articleDOIPage = articleDao.queryArticlePageList(current, size);
List<ArticleDO> records = articleDOIPage.getRecords();

List<QueryArchivePageListRspVO> list = Lists.newArrayList();
List<QueryArchiveItemRspVO> itemRspVOList = null;
if (!CollectionUtils.isEmpty(records)) {
itemRspVOList = records.stream()
.map(p -> QueryArchiveItemRspVO.builder()
.id(p.getId())
.title(p.getTitle())
.titleImage(p.getTitleImage())
.createMonth(Constants.MONTH_FORMAT.format(p.getCreateTime()))
.createTime(Constants.DATE_FORMAT.format(p.getCreateTime()))
.build())
.collect(Collectors.toList());

Map<String, List<QueryArchiveItemRspVO>> map = itemRspVOList.stream().collect(Collectors.groupingBy(QueryArchiveItemRspVO::getCreateMonth));
Map<String, List<QueryArchiveItemRspVO>> sortedMap = new TreeMap<>(new MonthKeyComparator());
sortedMap.putAll(map);

sortedMap.forEach((k, v) -> list.add(QueryArchivePageListRspVO.builder().month(k).articles(v).build()));
}
return PageResponse.success(articleDOIPage, list);
}

class MonthKeyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
// 使用YearMonth类将字符串解析成日期,并根据日期进行倒序排序
YearMonth ym1 = YearMonth.parse(o1);
YearMonth ym2 = YearMonth.parse(o2);
return ym2.compareTo(ym1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.eventbus.EventBus;
import com.quanxiaoha.weblog.common.PageResponse;
import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.constant.Constants;
import com.quanxiaoha.weblog.common.domain.dos.*;
import com.quanxiaoha.weblog.common.enums.EventEnum;
import com.quanxiaoha.weblog.common.eventbus.ArticleEvent;
Expand Down Expand Up @@ -52,8 +53,6 @@ public class ArticleServiceImpl implements ArticleService {
@Autowired
private EventBus eventBus;

private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");

@Override
public PageResponse queryIndexArticlePageList(QueryIndexArticlePageListReqVO queryIndexArticlePageListReqVO) {
Long current = queryIndexArticlePageListReqVO.getCurrent();
Expand All @@ -70,7 +69,7 @@ public PageResponse queryIndexArticlePageList(QueryIndexArticlePageListReqVO que
.title(p.getTitle())
.titleImage(p.getTitleImage())
.description(p.getDescription())
.createTime(FORMAT.format(p.getCreateTime()))
.createTime(Constants.DATE_FORMAT.format(p.getCreateTime()))
.build())
.collect(Collectors.toList());

Expand Down Expand Up @@ -152,7 +151,7 @@ public PageResponse queryCategoryArticlePageList(QueryCategoryArticlePageListReq
.title(p.getTitle())
.titleImage(p.getTitleImage())
.description(p.getDescription())
.createTime(FORMAT.format(p.getCreateTime()))
.createTime(Constants.DATE_FORMAT.format(p.getCreateTime()))
.build())
.collect(Collectors.toList());

Expand Down Expand Up @@ -296,7 +295,7 @@ public PageResponse queryTagArticlePageList(QueryTagArticlePageListReqVO queryTa
.title(p.getTitle())
.titleImage(p.getTitleImage())
.description(p.getDescription())
.createTime(FORMAT.format(p.getCreateTime()))
.createTime(Constants.DATE_FORMAT.format(p.getCreateTime()))
.build())
.collect(Collectors.toList());

Expand Down
Loading

0 comments on commit 0f0bf8f

Please sign in to comment.