目录
接口说明
接口url:/articles/publish
请求方式:POST
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
title | string | 文章标题 |
id | long | 文章id(编辑有值) |
body | object({content: “ww”, contentHtml: “ ww ↵”}) | 文章内容 |
category | {id: 2, avatar: “/category/back.png”, categoryName: “后端”} | 文章类别 |
summary | string | 文章概述 |
tags | [{id: 5}, {id: 6}] | 文章标签 |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": {"id":12232323}
}
代码结构:
编码实现
Dao持久层
pojo实体类
ArticleTag:
package com.huing.blog.dao.pojo;
import lombok.Data;
/**
* @Author huing
* @Create 2022-07-12 14:01
*/
@Data
public class ArticleTag {
private Long id;
private Long articleId;
private Long tagId;
}
Mapper接口
ArticleTagMapper:
package com.huing.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huing.blog.dao.pojo.ArticleTag;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author huing
* @Create 2022-07-12 14:02
*/
@Mapper
public interface ArticleTagMapper extends BaseMapper<ArticleTag> {
}
Param传参对象
ArticleParm:
package com.huing.blog.vo.params;
import com.huing.blog.vo.CategoryVo;
import com.huing.blog.vo.TagVo;
import lombok.Data;
import java.util.List;
/**
* @Author huing
* @Create 2022-07-12 13:46
*/
@Data
public class ArticleParm {
/**
* 文章id(编辑有值)
*/
private Long id;
/**
*文章内容
*/
private ArticleBodyParam body;
/**
*文章类别
*/
private CategoryVo category;
/**
* 文章概述
*/
private String summary;
/**
* 文章标签
*/
private List<TagVo> tags;
/**
* 文章标题
*/
private String title;
private String search;
}
ArticleBodyParam:
package com.huing.blog.vo.params;
import lombok.Data;
/**
* @Author huing
* @Create 2022-07-12 13:46
*/
@Data
public class ArticleBodyParam {
private String content;
private String contentHtml;
}
Controller控制层
ArticleController:
/**
* 写文章
* @param articleParm
* @return
*/
// @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
// 而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。
@PostMapping("publish")
public Result publish(@RequestBody ArticleParm articleParm){
return articleService.publish(articleParm);
}
Service业务逻辑层
ArticleService接口:
/**
* 写文章
* @param articleParm
* @return
*/
Result publish(ArticleParm articleParm);
ArticleServiceImpl实现类:
@Override
public Result publish(ArticleParm articleParm) {
//加入到登录拦截中
SysUser sysUser = UserThreadLocal.get();
/**
* 1.发布文章 目的是构建Article对象
* 2.作者id 当前的登录用户
* 3.标签 要将标签加入到 关联列表中
* 4.body 内容存储 article bodyId
*/
Article article = new Article();
if (articleParm.getId() != null){
article.setId(articleParm.getId());
article.setTitle(articleParm.getTitle());
article.setSummary(articleParm.getSummary());
article.setCategoryId(Long.parseLong(articleParm.getCategory().getId()));
articleMapper.insert(article);
}else {
article = new Article();
article.setAuthorId(sysUser.getId());
article.setWeight(Article.Article_Common);
article.setViewCounts(0);
article.setTitle(articleParm.getTitle());
article.setSummary(articleParm.getSummary());
article.setCommentCounts(0);
article.setCreateDate(System.currentTimeMillis());
article.setCategoryId(Long.parseLong(articleParm.getCategory().getId()));
articleMapper.insert(article);
}
//tag
List<TagVo> tags = articleParm.getTags();
if (tags != null){
for (TagVo tag : tags) {
Long articleId = article.getId();
ArticleTag articleTag = new ArticleTag();
articleTag.setTagId(Long.parseLong(tag.getId()));
articleTag.setArticleId(articleId);
articleTagMapper.insert(articleTag);
}
}
//body
ArticleBody articleBody = new ArticleBody();
articleBody.setArticleId(article.getId());
articleBody.setContent(articleParm.getBody().getContent());
articleBody.setContentHtml(articleParm.getBody().getContentHtml());
articleBodyMapper.insert(articleBody);
article.setBodyId(articleBody.getId());
articleMapper.updateById(article);
Map<String,String> map = new HashMap<>();
map.put("id",article.getId().toString());
return Result.success(map);
}
前端测试