mirror of https://github.com/halo-dev/halo
Refactor category dto conversion
parent
ad751f1797
commit
1fd99967b5
|
@ -37,21 +37,14 @@ public class CategoryController {
|
||||||
this.postCategoryService = postCategoryService;
|
this.postCategoryService = postCategoryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("{categoryId:\\d+}")
|
||||||
/**
|
@ApiOperation("Gets category detail")
|
||||||
* Get Category by id
|
public CategoryDTO getBy(@PathVariable("categoryId") Integer categoryId) {
|
||||||
*
|
return categoryService.convertTo(categoryService.getById(categoryId));
|
||||||
* @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
|
@GetMapping
|
||||||
@ApiOperation("List all categories")
|
@ApiOperation("Lists all categories")
|
||||||
public List<? extends CategoryDTO> listAll(
|
public List<? extends CategoryDTO> listAll(
|
||||||
@SortDefault(sort = "updateTime", direction = DESC) Sort sort,
|
@SortDefault(sort = "updateTime", direction = DESC) Sort sort,
|
||||||
@RequestParam(name = "more", required = false, defaultValue = "false") boolean more) {
|
@RequestParam(name = "more", required = false, defaultValue = "false") boolean more) {
|
||||||
|
@ -63,37 +56,33 @@ public class CategoryController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("tree_view")
|
@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) {
|
public List<CategoryVO> listAsTree(@SortDefault(sort = "name", direction = ASC) Sort sort) {
|
||||||
return categoryService.listAsTree(sort);
|
return categoryService.listAsTree(sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public CategoryDTO createBy(@Valid @RequestBody CategoryParam categoryParam) {
|
@ApiOperation("Creates category")
|
||||||
|
public CategoryDTO createBy(@RequestBody @Valid CategoryParam categoryParam) {
|
||||||
// Convert to category
|
// Convert to category
|
||||||
Category category = categoryParam.convertTo();
|
Category category = categoryParam.convertTo();
|
||||||
|
|
||||||
// Save it
|
// Save it
|
||||||
return new CategoryDTO().convertFrom(categoryService.create(category));
|
return categoryService.convertTo(categoryService.create(category));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("{id:\\d+}")
|
@PutMapping("{categoryId:\\d+}")
|
||||||
@ApiOperation("Update category")
|
@ApiOperation("Updates category")
|
||||||
public CategoryDTO updateBy(@PathVariable("id") Integer id,
|
public CategoryDTO updateBy(@PathVariable("categoryId") Integer categoryId,
|
||||||
@RequestBody @Valid CategoryParam categoryParam) {
|
@RequestBody @Valid CategoryParam categoryParam) {
|
||||||
Category categoryToUpdate = categoryService.getById(id);
|
Category categoryToUpdate = categoryService.getById(categoryId);
|
||||||
categoryParam.update(categoryToUpdate);
|
categoryParam.update(categoryToUpdate);
|
||||||
return new CategoryDTO().convertFrom(categoryService.update(categoryToUpdate));
|
return categoryService.convertTo(categoryService.update(categoryToUpdate));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@DeleteMapping("{categoryId:\\d+}")
|
||||||
* Delete category by id.
|
@ApiOperation("Deletes category")
|
||||||
*
|
public void deletePermanently(@PathVariable("categoryId") Integer categoryId) {
|
||||||
* @param id id
|
categoryService.removeCategoryAndPostCategoryBy(categoryId);
|
||||||
*/
|
|
||||||
@DeleteMapping("{id:\\d+}")
|
|
||||||
@ApiOperation("Delete category")
|
|
||||||
public void deletePermanently(@PathVariable("id") Integer id) {
|
|
||||||
categoryService.removeCategoryAndPostCategoryBy(id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,14 +18,6 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public interface CategoryService extends CrudService<Category, Integer> {
|
public interface CategoryService extends CrudService<Category, Integer> {
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove category and relationship
|
|
||||||
*
|
|
||||||
* @param id id
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
void remove(@NonNull Integer id);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists as category tree.
|
* Lists as category tree.
|
||||||
*
|
*
|
||||||
|
@ -52,9 +44,19 @@ public interface CategoryService extends CrudService<Category, Integer> {
|
||||||
@Transactional
|
@Transactional
|
||||||
void removeCategoryAndPostCategoryBy(Integer categoryId);
|
void removeCategoryAndPostCategoryBy(Integer categoryId);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts to category dto.
|
* 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
|
* @param categories category list
|
||||||
* @return a list of category dto
|
* @return a list of category dto
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
package run.halo.app.service.impl;
|
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.AlreadyExistsException;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
import run.halo.app.model.dto.CategoryDTO;
|
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.CategoryService;
|
||||||
import run.halo.app.service.PostCategoryService;
|
import run.halo.app.service.PostCategoryService;
|
||||||
import run.halo.app.service.base.AbstractCrudService;
|
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.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -42,16 +42,6 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
|
||||||
this.postCategoryService = postCategoryService;
|
this.postCategoryService = postCategoryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove category and relationship
|
|
||||||
*
|
|
||||||
* @param id id
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void remove(Integer id) {
|
|
||||||
// TODO 删除分类,以及和文章的对应关系
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Category create(Category category) {
|
public Category create(Category category) {
|
||||||
Assert.notNull(category, "Category to create must not be null");
|
Assert.notNull(category, "Category to create must not be null");
|
||||||
|
@ -174,6 +164,13 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
|
||||||
postCategoryService.removeByCategoryId(categoryId);
|
postCategoryService.removeByCategoryId(categoryId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CategoryDTO convertTo(Category category) {
|
||||||
|
Assert.notNull(category, "Category must not be null");
|
||||||
|
|
||||||
|
return new CategoryDTO().convertFrom(category);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CategoryDTO> convertTo(List<Category> categories) {
|
public List<CategoryDTO> convertTo(List<Category> categories) {
|
||||||
if (CollectionUtils.isEmpty(categories)) {
|
if (CollectionUtils.isEmpty(categories)) {
|
||||||
|
@ -182,7 +179,7 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
|
||||||
|
|
||||||
return categories
|
return categories
|
||||||
.stream()
|
.stream()
|
||||||
.map(category -> new CategoryDTO().<CategoryDTO>convertFrom(category))
|
.map(this::convertTo)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue