Service层修改 单元测试

This commit is contained in:
禾几海
2020-05-25 22:18:50 +08:00
parent c4ed6602e7
commit d19e5b6286
6 changed files with 29 additions and 76 deletions

View File

@@ -57,7 +57,8 @@ public class CategoryController {
* @return Response
*/
@GetMapping("/categories")
public Response getPage() {
return ResponseUtil.success(categoryService.retrievePage());
public Response getPage(@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "count", defaultValue = "1000") int count) {
return ResponseUtil.success(categoryService.retrievePage(page, count));
}
}

View File

@@ -1,12 +1,10 @@
package cn.celess.blog.entity.model;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.Article;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@@ -22,22 +20,5 @@ public class CategoryModel {
private String name;
private List<Integer> articles;
public CategoryModel(Category category) {
this.id = category.getId();
this.name = category.getName();
if (category.getArticles() == null || category.getArticles().length() == 0) {
articles = null;
} else {
articles = new ArrayList<>();
for (String s : category.getArticles().split(",")) {
if ("".equals(s)) {
return;
}
articles.add(Integer.parseInt(s));
}
}
}
private List<Article> articles;
}

View File

@@ -2,6 +2,7 @@ package cn.celess.blog.service;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.model.CategoryModel;
import cn.celess.blog.entity.model.PageData;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -20,14 +21,6 @@ public interface CategoryService {
*/
CategoryModel create(String name);
/**
* 增加一个分类
*
* @param category 分类对象
* @return 所增加的分类数据
*/
CategoryModel create(Category category);
/**
* 通过id删除分类
*
@@ -50,6 +43,6 @@ public interface CategoryService {
*
* @return 全部的分类数据
*/
List<CategoryModel> retrievePage();
PageData<CategoryModel> retrievePage(int page, int count);
}

View File

@@ -1,13 +1,16 @@
package cn.celess.blog.service.serviceimpl;
import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.Article;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.model.CategoryModel;
import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.exception.MyException;
import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CategoryMapper;
import cn.celess.blog.service.CategoryService;
import cn.celess.blog.util.ModalTrans;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -35,46 +38,19 @@ public class CategoryServiceImpl implements CategoryService {
}
Category category = new Category();
category.setName(name);
category.setArticles("");
categoryMapper.insert(category);
return new CategoryModel(category);
}
@Override
public CategoryModel create(Category category) {
if (category == null) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
categoryMapper.insert(category);
return new CategoryModel(category);
return ModalTrans.category(category);
}
@Override
public boolean delete(long id) {
Category category = categoryMapper.findCategoryById(id);
if (category == null) {
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
}
String[] articleArray = category.getArticles().split(",");
for (int i = 0; i < articleArray.length; i++) {
if (articleArray[i] == null || "".equals(articleArray[i])) {
continue;
}
long articleId = Long.parseLong(articleArray[i]);
Article article = articleMapper.findArticleById(articleId);
if (article == null) {
continue;
}
article.setCategoryId(-1L);
//一个 文章只对应一个分类,分类不存在则文章默认不可见
article.setOpen(false);
articleMapper.update(article);
}
return categoryMapper.delete(id) == 1;
}
@Override
public CategoryModel update(Long id, String name) {
if (id == null) {
@@ -83,13 +59,15 @@ public class CategoryServiceImpl implements CategoryService {
Category category = categoryMapper.findCategoryById(id);
category.setName(name);
categoryMapper.update(category);
return new CategoryModel(category);
return ModalTrans.category(category);
}
@Override
public List<CategoryModel> retrievePage() {
List<CategoryModel> list = new ArrayList<>();
categoryMapper.findAll().forEach(e -> list.add(new CategoryModel(e)));
return list;
public PageData<CategoryModel> retrievePage(int page, int count) {
PageHelper.startPage(page, count);
List<Category> all = categoryMapper.findAll();
List<CategoryModel> modelList = new ArrayList<>();
all.forEach(e -> modelList.add(ModalTrans.category(e)));
return new PageData<CategoryModel>(new PageInfo<Category>(all), modelList);
}
}

View File

@@ -17,47 +17,47 @@
update tag_category
set t_name=#{name}
where t_id = #{id}
and is_category = true;
and is_category = true
</update>
<update id="delete">
update tag_category
set is_delete= true
where t_id = #{id}
and is_category = true;
and is_category = true
</update>
<select id="findCategoryByName" resultMap="categoryResultMap">
select *
from tag_category
where t_name = #{name}
and is_category = true;
and is_category = true
</select>
<select id="findCategoryById" resultMap="categoryResultMap">
select *
from tag_category
where t_id = #{id}
and is_category = true;
and is_category = true
</select>
<select id="findAll" resultMap="categoryResultMap">
select *
from tag_category
where is_category = true;
where is_category = true
</select>
<select id="getAllName" resultType="java.lang.String">
select t_name
from tag_category
where is_category = true;
where is_category = true
</select>
<select id="getNameById" resultType="java.lang.String">
select t_name
from tag_category
where is_category = true
and t_id = #{id};
and t_id = #{id}
</select>
<select id="getIdByName" resultType="java.lang.Long">