Skip to content

Commit

Permalink
添加分类功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Dec 18, 2019
1 parent 56da7b7 commit 222481d
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/TypesMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface TypesMapper {
List<Types> selectAll();

int updateByPrimaryKey(Types record);

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

private String typeMsg;

private Date creatTime;
private String creatTime;

private static final long serialVersionUID = 1L;

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

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

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

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

import com.swpu.uchain.blog.form.InsertTypesForm;
import com.swpu.uchain.blog.form.UpdateTypesForm;
import com.swpu.uchain.blog.vo.ResultVO;
import org.springframework.stereotype.Service;

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

/***
* 添加分类
* @param form 添加分类表单
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO insertTypes(InsertTypesForm form);

/**
* 修改分类
* @param form 更新分类表单
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO updateTypes(UpdateTypesForm form);

/**
* 删除分类
* @param id 分类id
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO deleteTypes(Integer id);

/***
* 查询所有分类
* @return com.swpu.uchain.blog.vo.ResultVO
*/
ResultVO selectAllTypes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.swpu.uchain.blog.service.impl;

import com.swpu.uchain.blog.dao.ArticleMapper;
import com.swpu.uchain.blog.dao.TypesMapper;
import com.swpu.uchain.blog.entity.Article;
import com.swpu.uchain.blog.entity.Types;
import com.swpu.uchain.blog.enums.DefaultEnum;
import com.swpu.uchain.blog.enums.ResultEnum;
import com.swpu.uchain.blog.form.InsertTypesForm;
import com.swpu.uchain.blog.form.UpdateTypesForm;
import com.swpu.uchain.blog.service.ArticleService;
import com.swpu.uchain.blog.service.TypesService;
import com.swpu.uchain.blog.util.ResultVOUtil;
import com.swpu.uchain.blog.util.TimeUtil;
import com.swpu.uchain.blog.vo.ResultVO;
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
public class TypesServiceImpl implements TypesService {

@Autowired
private TypesMapper typesMapper;

@Autowired
private ArticleMapper articleMapper;

@Autowired
private ArticleService articleService;

@Override
public ResultVO insertTypes(InsertTypesForm form) {
if (typesMapper.selectByTypeMsg(form.getTypeMsg()) != null) {
return ResultVOUtil.error(ResultEnum.TYPE_IS_ALREADY_EXIST);
}
Types types = new Types();
BeanUtils.copyProperties(form, types);
types.setCreatTime(TimeUtil.getNowTime());
if (typesMapper.insert(types) == 1) {
return ResultVOUtil.success(types);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO updateTypes(UpdateTypesForm form) {
if (typesMapper.selectByPrimaryKey(form.getId()) == null) {
return ResultVOUtil.error(ResultEnum.TYPE_NOT_EXIST);
}
Types types = new Types();
BeanUtils.copyProperties(form, types);
if (typesMapper.updateByPrimaryKey(types) == 1) {
return ResultVOUtil.success(types);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO deleteTypes(Integer id) {
if (id.equals(DefaultEnum.TYPE_DEFAULT_ENUM.getValue())) {
return ResultVOUtil.error(ResultEnum.DEFAULT_GROUP_CAN_NOT_DELETE);
}
if (typesMapper.selectByPrimaryKey(id) == null) {
return ResultVOUtil.error(ResultEnum.TYPE_NOT_EXIST);
}
List<Article> articles = articleMapper.selectByTypeId(id);
if (typesMapper.deleteByPrimaryKey(id) == 1) {
for (Article article : articles) {
article.setTypeId(DefaultEnum.TYPE_DEFAULT_ENUM.getValue());
articleService.update(article);
}
return ResultVOUtil.success();
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO selectAllTypes() {
List<Types> types = typesMapper.selectAll();
return ResultVOUtil.success(types);
}
}
11 changes: 8 additions & 3 deletions src/main/resources/mappers/TypesMapper.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.Types" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="type_msg" property="typeMsg" jdbcType="VARCHAR" />
<result column="creat_time" property="creatTime" jdbcType="TIMESTAMP" />
<result column="creat_time" property="creatTime" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from types
Expand All @@ -13,13 +13,13 @@
<insert id="insert" parameterType="com.swpu.uchain.blog.entity.Types" >
insert into types (id, type_msg, creat_time
)
values (#{id,jdbcType=INTEGER}, #{typeMsg,jdbcType=VARCHAR}, #{creatTime,jdbcType=TIMESTAMP}
values (#{id,jdbcType=INTEGER}, #{typeMsg,jdbcType=VARCHAR}, #{creatTime,jdbcType=VARCHAR}
)
</insert>
<update id="updateByPrimaryKey" parameterType="com.swpu.uchain.blog.entity.Types" >
update types
set type_msg = #{typeMsg,jdbcType=VARCHAR},
creat_time = #{creatTime,jdbcType=TIMESTAMP}
creat_time = #{creatTime,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
Expand All @@ -31,4 +31,9 @@
select id, type_msg, creat_time
from types
</select>
<select id="selectByTypeMsg" resultType="com.swpu.uchain.blog.entity.Types">
select type_msg
from types
where type_msg = #{typeMsg}
</select>
</mapper>

0 comments on commit 222481d

Please sign in to comment.