调整数据库字段,优化部分接口 #1
@@ -57,7 +57,8 @@ public class CategoryController {
|
|||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
@GetMapping("/categories")
|
@GetMapping("/categories")
|
||||||
public Response getPage() {
|
public Response getPage(@RequestParam(name = "page", defaultValue = "1") int page,
|
||||||
return ResponseUtil.success(categoryService.retrievePage());
|
@RequestParam(name = "count", defaultValue = "1000") int count) {
|
||||||
|
return ResponseUtil.success(categoryService.retrievePage(page, count));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package cn.celess.blog.entity.model;
|
package cn.celess.blog.entity.model;
|
||||||
|
|
||||||
import cn.celess.blog.entity.Category;
|
import cn.celess.blog.entity.Article;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,22 +20,5 @@ public class CategoryModel {
|
|||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private List<Integer> articles;
|
private List<Article> 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cn.celess.blog.service;
|
|||||||
|
|
||||||
import cn.celess.blog.entity.Category;
|
import cn.celess.blog.entity.Category;
|
||||||
import cn.celess.blog.entity.model.CategoryModel;
|
import cn.celess.blog.entity.model.CategoryModel;
|
||||||
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -20,14 +21,6 @@ public interface CategoryService {
|
|||||||
*/
|
*/
|
||||||
CategoryModel create(String name);
|
CategoryModel create(String name);
|
||||||
|
|
||||||
/**
|
|
||||||
* 增加一个分类
|
|
||||||
*
|
|
||||||
* @param category 分类对象
|
|
||||||
* @return 所增加的分类数据
|
|
||||||
*/
|
|
||||||
CategoryModel create(Category category);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过id删除分类
|
* 通过id删除分类
|
||||||
*
|
*
|
||||||
@@ -50,6 +43,6 @@ public interface CategoryService {
|
|||||||
*
|
*
|
||||||
* @return 全部的分类数据
|
* @return 全部的分类数据
|
||||||
*/
|
*/
|
||||||
List<CategoryModel> retrievePage();
|
PageData<CategoryModel> retrievePage(int page, int count);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package cn.celess.blog.service.serviceimpl;
|
package cn.celess.blog.service.serviceimpl;
|
||||||
|
|
||||||
import cn.celess.blog.enmu.ResponseEnum;
|
import cn.celess.blog.enmu.ResponseEnum;
|
||||||
import cn.celess.blog.entity.Article;
|
|
||||||
import cn.celess.blog.entity.Category;
|
import cn.celess.blog.entity.Category;
|
||||||
import cn.celess.blog.entity.model.CategoryModel;
|
import cn.celess.blog.entity.model.CategoryModel;
|
||||||
|
import cn.celess.blog.entity.model.PageData;
|
||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.mapper.ArticleMapper;
|
import cn.celess.blog.mapper.ArticleMapper;
|
||||||
import cn.celess.blog.mapper.CategoryMapper;
|
import cn.celess.blog.mapper.CategoryMapper;
|
||||||
import cn.celess.blog.service.CategoryService;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -35,46 +38,19 @@ public class CategoryServiceImpl implements CategoryService {
|
|||||||
}
|
}
|
||||||
Category category = new Category();
|
Category category = new Category();
|
||||||
category.setName(name);
|
category.setName(name);
|
||||||
category.setArticles("");
|
|
||||||
categoryMapper.insert(category);
|
categoryMapper.insert(category);
|
||||||
return new CategoryModel(category);
|
return ModalTrans.category(category);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CategoryModel create(Category category) {
|
|
||||||
if (category == null) {
|
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
|
||||||
}
|
|
||||||
categoryMapper.insert(category);
|
|
||||||
return new CategoryModel(category);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean delete(long id) {
|
public boolean delete(long id) {
|
||||||
Category category = categoryMapper.findCategoryById(id);
|
Category category = categoryMapper.findCategoryById(id);
|
||||||
|
|
||||||
if (category == null) {
|
if (category == null) {
|
||||||
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
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;
|
return categoryMapper.delete(id) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CategoryModel update(Long id, String name) {
|
public CategoryModel update(Long id, String name) {
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
@@ -83,13 +59,15 @@ public class CategoryServiceImpl implements CategoryService {
|
|||||||
Category category = categoryMapper.findCategoryById(id);
|
Category category = categoryMapper.findCategoryById(id);
|
||||||
category.setName(name);
|
category.setName(name);
|
||||||
categoryMapper.update(category);
|
categoryMapper.update(category);
|
||||||
return new CategoryModel(category);
|
return ModalTrans.category(category);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CategoryModel> retrievePage() {
|
public PageData<CategoryModel> retrievePage(int page, int count) {
|
||||||
List<CategoryModel> list = new ArrayList<>();
|
PageHelper.startPage(page, count);
|
||||||
categoryMapper.findAll().forEach(e -> list.add(new CategoryModel(e)));
|
List<Category> all = categoryMapper.findAll();
|
||||||
return list;
|
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
|
update tag_category
|
||||||
set t_name=#{name}
|
set t_name=#{name}
|
||||||
where t_id = #{id}
|
where t_id = #{id}
|
||||||
and is_category = true;
|
and is_category = true
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<update id="delete">
|
<update id="delete">
|
||||||
update tag_category
|
update tag_category
|
||||||
set is_delete= true
|
set is_delete= true
|
||||||
where t_id = #{id}
|
where t_id = #{id}
|
||||||
and is_category = true;
|
and is_category = true
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<select id="findCategoryByName" resultMap="categoryResultMap">
|
<select id="findCategoryByName" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from tag_category
|
from tag_category
|
||||||
where t_name = #{name}
|
where t_name = #{name}
|
||||||
and is_category = true;
|
and is_category = true
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findCategoryById" resultMap="categoryResultMap">
|
<select id="findCategoryById" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from tag_category
|
from tag_category
|
||||||
where t_id = #{id}
|
where t_id = #{id}
|
||||||
and is_category = true;
|
and is_category = true
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAll" resultMap="categoryResultMap">
|
<select id="findAll" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from tag_category
|
from tag_category
|
||||||
where is_category = true;
|
where is_category = true
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getAllName" resultType="java.lang.String">
|
<select id="getAllName" resultType="java.lang.String">
|
||||||
select t_name
|
select t_name
|
||||||
from tag_category
|
from tag_category
|
||||||
where is_category = true;
|
where is_category = true
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getNameById" resultType="java.lang.String">
|
<select id="getNameById" resultType="java.lang.String">
|
||||||
select t_name
|
select t_name
|
||||||
from tag_category
|
from tag_category
|
||||||
where is_category = true
|
where is_category = true
|
||||||
and t_id = #{id};
|
and t_id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getIdByName" resultType="java.lang.Long">
|
<select id="getIdByName" resultType="java.lang.Long">
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addOne() throws Exception {
|
public void addOne() throws Exception {
|
||||||
String categoryName = UUID.randomUUID().toString().substring(0, 4);
|
String categoryName = randomStr(4);
|
||||||
System.out.println("categoryName: ==> " + categoryName);
|
System.out.println("categoryName: ==> " + categoryName);
|
||||||
// 未登录
|
// 未登录
|
||||||
mockMvc.perform(post("/admin/category/create?name=" + categoryName)).andExpect(status().isOk())
|
mockMvc.perform(post("/admin/category/create?name=" + categoryName)).andExpect(status().isOk())
|
||||||
@@ -84,7 +84,7 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
@Test
|
@Test
|
||||||
public void updateOne() throws Exception {
|
public void updateOne() throws Exception {
|
||||||
Category category = categoryMapper.getLastestCategory();
|
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())
|
mockMvc.perform(put("/admin/category/update?id=" + category.getId() + "&name=" + name)).andExpect(status().isOk())
|
||||||
.andDo(result -> {
|
.andDo(result -> {
|
||||||
@@ -119,7 +119,7 @@ public class CategoryControllerTest extends BaseTest {
|
|||||||
.andDo(result -> {
|
.andDo(result -> {
|
||||||
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||||
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
assertEquals(SUCCESS.getCode(), object.getInt(Code));
|
||||||
JSONArray jsonArray = object.getJSONArray(Result);
|
JSONArray jsonArray = object.getJSONObject(Result).getJSONArray("list");
|
||||||
assertNotNull(jsonArray);
|
assertNotNull(jsonArray);
|
||||||
jsonArray.forEach(o -> {
|
jsonArray.forEach(o -> {
|
||||||
CategoryModel c = (CategoryModel) JSONObject.toBean(JSONObject.fromObject(o), CategoryModel.class);
|
CategoryModel c = (CategoryModel) JSONObject.toBean(JSONObject.fromObject(o), CategoryModel.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user