修改响应的Category的articles字段类型

This commit is contained in:
小海
2020-03-29 13:55:59 +08:00
parent 7ba287d261
commit 8181fab48d
4 changed files with 67 additions and 18 deletions

View File

@@ -0,0 +1,43 @@
package cn.celess.blog.entity.model;
import cn.celess.blog.entity.Category;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: 小海
* @Date: 2020-03-29 12:18
* @Desc:
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryModel {
private Long id;
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));
}
}
}
}

View File

@@ -1,6 +1,7 @@
package cn.celess.blog.service;
import cn.celess.blog.entity.Category;
import cn.celess.blog.entity.model.CategoryModel;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -17,7 +18,7 @@ public interface CategoryService {
* @param name 分类名
* @return 所增加的分类数据
*/
Category create(String name);
CategoryModel create(String name);
/**
* 增加一个分类
@@ -25,7 +26,7 @@ public interface CategoryService {
* @param category 分类对象
* @return 所增加的分类数据
*/
Category create(Category category);
CategoryModel create(Category category);
/**
* 通过id删除分类
@@ -42,13 +43,13 @@ public interface CategoryService {
* @param name 分类名字
* @return 更新后的分类的数据
*/
Category update(Long id, String name);
CategoryModel update(Long id, String name);
/**
* 获取全部的分类数据
*
* @return 全部的分类数据
*/
List<Category> retrievePage();
List<CategoryModel> retrievePage();
}

View File

@@ -3,6 +3,7 @@ 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.exception.MyException;
import cn.celess.blog.mapper.ArticleMapper;
import cn.celess.blog.mapper.CategoryMapper;
@@ -11,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
@@ -27,7 +29,7 @@ public class CategoryServiceImpl implements CategoryService {
ArticleMapper articleMapper;
@Override
public Category create(String name) {
public CategoryModel create(String name) {
if (categoryMapper.existsByName(name)) {
throw new MyException(ResponseEnum.CATEGORY_HAS_EXIST);
}
@@ -35,16 +37,16 @@ public class CategoryServiceImpl implements CategoryService {
category.setName(name);
category.setArticles("");
categoryMapper.insert(category);
return category;
return new CategoryModel(category);
}
@Override
public Category create(Category category) {
public CategoryModel create(Category category) {
if (category == null) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
}
categoryMapper.insert(category);
return category;
return new CategoryModel(category);
}
@Override
@@ -74,18 +76,20 @@ public class CategoryServiceImpl implements CategoryService {
@Override
public Category update(Long id, String name) {
public CategoryModel update(Long id, String name) {
if (id == null) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不可为空");
}
Category category = categoryMapper.findCategoryById(id);
category.setName(name);
categoryMapper.update(category);
return category;
return new CategoryModel(category);
}
@Override
public List<Category> retrievePage() {
return categoryMapper.findAll();
public List<CategoryModel> retrievePage() {
List<CategoryModel> list = new ArrayList<>();
categoryMapper.findAll().forEach(e -> list.add(new CategoryModel(e)));
return list;
}
}