From 1fd99967b59edfc25bac4aadf5da52d210d40b80 Mon Sep 17 00:00:00 2001 From: johnniang Date: Fri, 26 Apr 2019 10:05:39 +0800 Subject: [PATCH] Refactor category dto conversion --- .../admin/api/CategoryController.java | 47 +++++++------------ .../run/halo/app/service/CategoryService.java | 18 +++---- .../app/service/impl/CategoryServiceImpl.java | 31 ++++++------ 3 files changed, 42 insertions(+), 54 deletions(-) diff --git a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java index 04308f579..d8ffdb92e 100644 --- a/src/main/java/run/halo/app/controller/admin/api/CategoryController.java +++ b/src/main/java/run/halo/app/controller/admin/api/CategoryController.java @@ -37,21 +37,14 @@ public class CategoryController { this.postCategoryService = postCategoryService; } - - /** - * Get Category by id - * - * @param id id - * @return CategoryDTO - */ - @GetMapping("{id:\\d+}") - @ApiOperation("Get category detail by id") - public CategoryDTO getBy(@PathVariable("id") Integer id) { - return new CategoryDTO().convertFrom(categoryService.getById(id)); + @GetMapping("{categoryId:\\d+}") + @ApiOperation("Gets category detail") + public CategoryDTO getBy(@PathVariable("categoryId") Integer categoryId) { + return categoryService.convertTo(categoryService.getById(categoryId)); } @GetMapping - @ApiOperation("List all categories") + @ApiOperation("Lists all categories") public List listAll( @SortDefault(sort = "updateTime", direction = DESC) Sort sort, @RequestParam(name = "more", required = false, defaultValue = "false") boolean more) { @@ -63,37 +56,33 @@ public class CategoryController { } @GetMapping("tree_view") - @ApiOperation("List as category tree") + @ApiOperation("List all categories as tree") public List listAsTree(@SortDefault(sort = "name", direction = ASC) Sort sort) { return categoryService.listAsTree(sort); } @PostMapping - public CategoryDTO createBy(@Valid @RequestBody CategoryParam categoryParam) { + @ApiOperation("Creates category") + public CategoryDTO createBy(@RequestBody @Valid CategoryParam categoryParam) { // Convert to category Category category = categoryParam.convertTo(); // Save it - return new CategoryDTO().convertFrom(categoryService.create(category)); + return categoryService.convertTo(categoryService.create(category)); } - @PutMapping("{id:\\d+}") - @ApiOperation("Update category") - public CategoryDTO updateBy(@PathVariable("id") Integer id, + @PutMapping("{categoryId:\\d+}") + @ApiOperation("Updates category") + public CategoryDTO updateBy(@PathVariable("categoryId") Integer categoryId, @RequestBody @Valid CategoryParam categoryParam) { - Category categoryToUpdate = categoryService.getById(id); + Category categoryToUpdate = categoryService.getById(categoryId); categoryParam.update(categoryToUpdate); - return new CategoryDTO().convertFrom(categoryService.update(categoryToUpdate)); + return categoryService.convertTo(categoryService.update(categoryToUpdate)); } - /** - * Delete category by id. - * - * @param id id - */ - @DeleteMapping("{id:\\d+}") - @ApiOperation("Delete category") - public void deletePermanently(@PathVariable("id") Integer id) { - categoryService.removeCategoryAndPostCategoryBy(id); + @DeleteMapping("{categoryId:\\d+}") + @ApiOperation("Deletes category") + public void deletePermanently(@PathVariable("categoryId") Integer categoryId) { + categoryService.removeCategoryAndPostCategoryBy(categoryId); } } diff --git a/src/main/java/run/halo/app/service/CategoryService.java b/src/main/java/run/halo/app/service/CategoryService.java index 418663d57..cd65120e3 100755 --- a/src/main/java/run/halo/app/service/CategoryService.java +++ b/src/main/java/run/halo/app/service/CategoryService.java @@ -18,14 +18,6 @@ import java.util.List; */ public interface CategoryService extends CrudService { - /** - * Remove category and relationship - * - * @param id id - */ - @Deprecated - void remove(@NonNull Integer id); - /** * Lists as category tree. * @@ -52,9 +44,19 @@ public interface CategoryService extends CrudService { @Transactional void removeCategoryAndPostCategoryBy(Integer categoryId); + /** * Converts to category dto. * + * @param category category must not be null + * @return category dto + */ + @NonNull + CategoryDTO convertTo(@NonNull Category category); + + /** + * Converts to category dto list. + * * @param categories category list * @return a list of category dto */ diff --git a/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java index 37502bb3a..54e093f1a 100644 --- a/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/CategoryServiceImpl.java @@ -1,5 +1,11 @@ package run.halo.app.service.impl; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Sort; +import org.springframework.lang.NonNull; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import run.halo.app.exception.AlreadyExistsException; import run.halo.app.exception.NotFoundException; import run.halo.app.model.dto.CategoryDTO; @@ -9,12 +15,6 @@ import run.halo.app.repository.CategoryRepository; import run.halo.app.service.CategoryService; import run.halo.app.service.PostCategoryService; import run.halo.app.service.base.AbstractCrudService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Sort; -import org.springframework.lang.NonNull; -import org.springframework.stereotype.Service; -import org.springframework.util.Assert; -import org.springframework.util.CollectionUtils; import java.util.Collections; import java.util.LinkedList; @@ -42,16 +42,6 @@ public class CategoryServiceImpl extends AbstractCrudService this.postCategoryService = postCategoryService; } - /** - * Remove category and relationship - * - * @param id id - */ - @Override - public void remove(Integer id) { - // TODO 删除分类,以及和文章的对应关系 - } - @Override public Category create(Category category) { Assert.notNull(category, "Category to create must not be null"); @@ -174,6 +164,13 @@ public class CategoryServiceImpl extends AbstractCrudService postCategoryService.removeByCategoryId(categoryId); } + @Override + public CategoryDTO convertTo(Category category) { + Assert.notNull(category, "Category must not be null"); + + return new CategoryDTO().convertFrom(category); + } + @Override public List convertTo(List categories) { if (CollectionUtils.isEmpty(categories)) { @@ -182,7 +179,7 @@ public class CategoryServiceImpl extends AbstractCrudService return categories .stream() - .map(category -> new CategoryDTO().convertFrom(category)) + .map(this::convertTo) .collect(Collectors.toList()); } }