调整数据库字段,优化部分接口 #1
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -23,7 +23,7 @@ public class CategoryControllerTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void addOne() throws Exception {
|
||||
String categoryName = UUID.randomUUID().toString().substring(0, 4);
|
||||
String categoryName = randomStr(4);
|
||||
System.out.println("categoryName: ==> " + categoryName);
|
||||
// 未登录
|
||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)).andExpect(status().isOk())
|
||||
@@ -84,7 +84,7 @@ public class CategoryControllerTest extends BaseTest {
|
||||
@Test
|
||||
public void updateOne() throws Exception {
|
||||
Category category = categoryMapper.getLastestCategory();
|
||||
String name = UUID.randomUUID().toString().substring(0, 4);
|
||||
String name = randomStr(4);
|
||||
// 未登录
|
||||
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)).andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
@@ -119,7 +119,7 @@ public class CategoryControllerTest extends BaseTest {
|
||||
.andDo(result -> {
|
||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||
JSONArray jsonArray = object.getJSONArray(Result);
|
||||
JSONArray jsonArray = object.getJSONObject(Result).getJSONArray("list");
|
||||
assertNotNull(jsonArray);
|
||||
jsonArray.forEach(o -> {
|
||||
CategoryModel c = (CategoryModel) JSONObject.toBean(JSONObject.fromObject(o), CategoryModel.class);
|
||||
|
||||
Reference in New Issue
Block a user