Skip to content

Commit

Permalink
标签接口
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Dec 12, 2019
1 parent 54893f6 commit 0c36059
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 7 deletions.
48 changes: 48 additions & 0 deletions src/main/java/com/swpu/uchain/blog/controller/TagsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.swpu.uchain.blog.controller;

import com.swpu.uchain.blog.form.InsertTagsForm;
import com.swpu.uchain.blog.form.UpdateTagsForm;
import com.swpu.uchain.blog.service.TagsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author hobo
* @description
*/
@RestController
@RequestMapping("/tags")
public class TagsController {

@Autowired
private TagsService tagsService;

@ApiOperation("添加标签")
@PostMapping(name = "添加标签", value = "/insert")
public Object insertTags(InsertTagsForm form) {
return tagsService.insertTags(form);
}

@ApiOperation("更新标签")
@PostMapping(name = "更新标签", value = "/update")
public Object updateTags(UpdateTagsForm form) {
return tagsService.updateTags(form);
}

@ApiOperation("删除标签")
@GetMapping(name = "删除标签",value = "/delete")
public Object deleteTags(Integer id) {
return tagsService.deleteTags(id);
}

@ApiOperation("查看所有标签")
@GetMapping(name = "查看所有标签",value = "/all")
public Object selectAllTags(){
return tagsService.selectAllTags();
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/ArticleMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface ArticleMapper {

Article selectByArticleTitle(String title);

List<Article> selectByTagsId(Integer tagsId);

List<ArticleListVO> getArticleList();

ArticleListVO selectArticlesByTagId(Integer tagId);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/TagsMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface TagsMapper {
List<Tags> selectAll();

int updateByPrimaryKey(Tags record);

Tags selectByTag(String tagsMsg);
}
6 changes: 3 additions & 3 deletions src/main/java/com/swpu/uchain/blog/entity/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Tags implements Serializable {

private String tagsMsg;

private Date creatTime;
private String creatTime;

private static final long serialVersionUID = 1L;

Expand All @@ -28,11 +28,11 @@ public void setTagsMsg(String tagsMsg) {
this.tagsMsg = tagsMsg == null ? null : tagsMsg.trim();
}

public Date getCreatTime() {
public String getCreatTime() {
return creatTime;
}

public void setCreatTime(Date creatTime) {
public void setCreatTime(String creatTime) {
this.creatTime = creatTime;
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/swpu/uchain/blog/enums/DefaultEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.swpu.uchain.blog.enums;

import lombok.Getter;

/**
* @author hobo
* @description
*/
@Getter
public enum DefaultEnum {
/**
* 标签默认分组
*/
TAGS_DEFAULT_ENUM(1);;
private Integer value;

DefaultEnum(Integer value) {
this.value = value;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/swpu/uchain/blog/enums/ResultEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public enum ResultEnum {
FILE_PATH_NOT_EXIST(14,"上传失败,文件目录不存在" ),
USER_ALREADY_EXIST(15,"此手机已注册" ),
CODE_IS_NULL(16,"验证码已过期,请重新获取" ),
FILE_UPLOAD_ERROR(17,"文件上传失败");
FILE_UPLOAD_ERROR(17,"文件上传失败"),
TAG_IS_ALREADY_EXIST(18,"标签已存在" ),
TAG_NOT_EXIST(19,"标签不存在" ),
DEFAULT_GROUP_CAN_NOT_DELETE(20,"默认分组不能删除" );
private Integer code;

private String msg;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/swpu/uchain/blog/form/InsertTagsForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.swpu.uchain.blog.form;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
* @author hobo
* @description
*/
@Data
public class InsertTagsForm {

@ApiModelProperty("标签")
private String tagsMsg;
}
16 changes: 16 additions & 0 deletions src/main/java/com/swpu/uchain/blog/form/UpdateTagsForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.swpu.uchain.blog.form;

import lombok.Data;

/**
* @author hobo
* @description
*/
@Data
public class UpdateTagsForm {

private Integer id;

private String tagsMsg;

}
39 changes: 39 additions & 0 deletions src/main/java/com/swpu/uchain/blog/service/TagsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.swpu.uchain.blog.service;

import com.swpu.uchain.blog.form.InsertTagsForm;
import com.swpu.uchain.blog.form.UpdateTagsForm;
import com.swpu.uchain.blog.vo.ResultVO;

/**
* @author hobo
* @description
*/
public interface TagsService {

/**
* 添加标签
* @param form 添加标签表单
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO insertTags(InsertTagsForm form);

/**
* 修改标签
* @param form 修改标签表单
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO updateTags(UpdateTagsForm form);

/**
* 删除标签
* @param id 标签id
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO deleteTags(Integer id);

/**
* 查看所有标签
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO selectAllTags();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.swpu.uchain.blog.service.impl;

import com.swpu.uchain.blog.dao.ArticleMapper;
import com.swpu.uchain.blog.dao.TagsMapper;
import com.swpu.uchain.blog.entity.Article;
import com.swpu.uchain.blog.entity.Tags;
import com.swpu.uchain.blog.enums.DefaultEnum;
import com.swpu.uchain.blog.enums.ResultEnum;
import com.swpu.uchain.blog.form.InsertTagsForm;
import com.swpu.uchain.blog.form.UpdateTagsForm;
import com.swpu.uchain.blog.service.TagsService;
import com.swpu.uchain.blog.util.ResultVOUtil;
import com.swpu.uchain.blog.util.TimeUtil;
import com.swpu.uchain.blog.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hobo
* @description
*/
@Service
@Slf4j
public class TagsServiceImpl implements TagsService {

@Autowired
private TagsMapper tagsMapper;

@Autowired
private ArticleMapper articleMapper;


@Override
public ResultVO insertTags(InsertTagsForm form) {
if (tagsMapper.selectByTag(form.getTagsMsg()) != null) {
return ResultVOUtil.error(ResultEnum.TAG_IS_ALREADY_EXIST);
}
Tags tags = new Tags();
BeanUtils.copyProperties(form, tags);
tags.setCreatTime(TimeUtil.getNowTime());
if (tagsMapper.insert(tags) == 1) {
return ResultVOUtil.success(tags);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO updateTags(UpdateTagsForm form) {
if (tagsMapper.selectByPrimaryKey(form.getId()) == null) {
return ResultVOUtil.error(ResultEnum.TAG_NOT_EXIST);
}
Tags tags = new Tags();
BeanUtils.copyProperties(form, tags);
if (tagsMapper.updateByPrimaryKey(tags) == 1) {
return ResultVOUtil.success(tags);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO deleteTags(Integer id) {
if (id.equals(DefaultEnum.TAGS_DEFAULT_ENUM.getValue())) {
return ResultVOUtil.error(ResultEnum.DEFAULT_GROUP_CAN_NOT_DELETE);
}
if (tagsMapper.selectByPrimaryKey(id) == null) {
return ResultVOUtil.error(ResultEnum.TAG_NOT_EXIST);
}
List<Article> articles = articleMapper.selectByTagsId(id);
// 将要删除的标签下的所有文章放入默认分组
for (Article article : articles) {
article.setTagsId(DefaultEnum.TAGS_DEFAULT_ENUM.getValue());
}
if (tagsMapper.deleteByPrimaryKey(id) == 1) {
return ResultVOUtil.success();
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO selectAllTags() {
List<Tags> tags = tagsMapper.selectAll();
return ResultVOUtil.success(tags);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/mappers/ArticleMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@
WHERE types.id=#{typeId}
GROUP BY at.id
</select>
<select id="selectByTagsId" resultType="com.swpu.uchain.blog.entity.Article">
SELECT id,tags_id
FROM tags
WHERE tags_id =#{tagsId}
</select>
</mapper>
11 changes: 8 additions & 3 deletions src/main/resources/mappers/TagsMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<resultMap id="BaseResultMap" type="com.swpu.uchain.blog.entity.Tags">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="tags_msg" jdbcType="VARCHAR" property="tagsMsg" />
<result column="creat_time" jdbcType="TIMESTAMP" property="creatTime" />
<result column="creat_time" jdbcType="VARCHAR" property="creatTime" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from tags
Expand All @@ -13,13 +13,13 @@
<insert id="insert" parameterType="com.swpu.uchain.blog.entity.Tags">
insert into tags (id, tags_msg, creat_time
)
values (#{id,jdbcType=INTEGER}, #{tagsMsg,jdbcType=VARCHAR}, #{creatTime,jdbcType=TIMESTAMP}
values (#{id,jdbcType=INTEGER}, #{tagsMsg,jdbcType=VARCHAR}, #{creatTime,jdbcType=VARCHAR}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.swpu.uchain.blog.entity.Tags">
update tags
set tags_msg = #{tagsMsg,jdbcType=VARCHAR},
creat_time = #{creatTime,jdbcType=TIMESTAMP}
creat_time = #{creatTime,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
Expand All @@ -31,4 +31,9 @@
select id, tags_msg, creat_time
from tags
</select>
<select id="selectByTag" resultType="com.swpu.uchain.blog.entity.Tags">
select tags_msg
from tags
where tags_msg = #{tagsMsg}
</select>
</mapper>

0 comments on commit 0c36059

Please sign in to comment.