Refactor category dto conversion

pull/146/head
johnniang 2019-04-26 10:05:39 +08:00
parent ad751f1797
commit 1fd99967b5
3 changed files with 42 additions and 54 deletions

View File

@ -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<? extends CategoryDTO> 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<CategoryVO> 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);
}
}

View File

@ -18,14 +18,6 @@ import java.util.List;
*/
public interface CategoryService extends CrudService<Category, Integer> {
/**
* 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<Category, Integer> {
@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
*/

View File

@ -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<Category, Integer>
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<Category, Integer>
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<CategoryDTO> convertTo(List<Category> categories) {
if (CollectionUtils.isEmpty(categories)) {
@ -182,7 +179,7 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
return categories
.stream()
.map(category -> new CategoryDTO().<CategoryDTO>convertFrom(category))
.map(this::convertTo)
.collect(Collectors.toList());
}
}