在线博客系统——发布文章

目录

接口说明

编码实现

Dao持久层

pojo实体类

Mapper接口

Param传参对象

Controller控制层

Service业务逻辑层

前端测试


接口说明

接口url:/articles/publish

请求方式:POST

请求参数:

参数名称参数类型说明
titlestring文章标题
idlong文章id(编辑有值)
bodyobject({content: “ww”, contentHtml: “

ww

↵”})

文章内容

category{id: 2, avatar: “/category/back.png”, categoryName: “后端”}文章类别
summarystring文章概述
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);
    }

前端测试

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值