个人博客项目笔记_08-3. 分类文章列表

3.1 接口说明

接口url:/category/detail/{id}

请求方式:GET

请求参数:

参数名称 参数类型 说明
id 分类id 路径参数

返回数据:

{
    "success": true, 
    "code": 200, 
    "msg": "success", 
    "data": 
        {
            "id": 1, 
            "avatar": "/static/category/front.png", 
            "categoryName": "前端", 
            "description": "前端是什么,大前端"
        }
}

3.2 Controller

CategoryController:

  @GetMapping("detail/{id}")
    public Result categoriesDetailById(@PathVariable("id") Long id){
        return categoryService.categoriesDetailById(id);
    }

3.3 Service

CategoryService:

Result categoriesDetailById(Long id);

CategoryServiceImpl:

@Override
    public Result categoriesDetailById(Long id) {
        Category category = categoryMapper.selectById(id);
        CategoryVo categoryVo = copy(category);
        return Result.success(categoryVo);
    }

ArticleServiceImpl:

新增如下代码:

​ //查询文章的参数 加上分类id,判断不为空 加上分类条件
​ if (pageParams.getCategoryId() != null) {
​ queryWrapper.eq(Article::getCategoryId,pageParams.getCategoryId());
​ }

@Override
    public List<ArticleVo> listArticlesPage(PageParams pageParams) {
    //  分页查询article数据库表
        Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPageSize());
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //查询文章的参数 加上分类id,判断不为空 加上分类条件,SELECT * FROM article WHERE category_id = ?
        if (pageParams.getCategoryId() != null) {
            queryWrapper.eq(Article::getCategoryId,pageParams.getCategoryId());
        }
        //是否置顶排序,SELECT * FROM article ORDER BY weight DESC, create_date DESC
        queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);
        //SELECT * FROM article WHERE category_id = ? ORDER BY weight DESC, create_date DESC
        Page<Article> articlePage = articleMapper.selectPage(page,queryWrapper);
        List<Article> records = articlePage.getRecords();
        List<ArticleVo> articleVoList = copyList(records,true,false,true);
        return articleVoList;
    }
package com.cherriesovo.blog.vo.params;

import lombok.Data;

@Data
public class PageParams {

    private int page = 1;

    private int pageSize = 10;

    private Long categoryId;

    private Long tagId;
}

上一篇:275. 传纸条(DP)