Refactor tag dto conversion

pull/146/head
johnniang 2019-04-26 10:13:27 +08:00
parent 1fd99967b5
commit 72a2a0647b
4 changed files with 43 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package run.halo.app.controller.admin.api; package run.halo.app.controller.admin.api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault; import org.springframework.data.web.SortDefault;
@ -36,7 +37,9 @@ public class TagController {
} }
@GetMapping @GetMapping
@ApiOperation("Lists tag")
public List<? extends TagDTO> listTags(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort, public List<? extends TagDTO> listTags(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort,
@ApiParam("Return more information(post count) if it is set")
@RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) { @RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) {
if (more) { if (more) {
return postTagService.listTagWithCountDtos(sort); return postTagService.listTagWithCountDtos(sort);
@ -45,6 +48,7 @@ public class TagController {
} }
@PostMapping @PostMapping
@ApiOperation("Creates tag")
public TagDTO createTag(@Valid @RequestBody TagParam tagParam) { public TagDTO createTag(@Valid @RequestBody TagParam tagParam) {
// Convert to tag // Convert to tag
Tag tag = tagParam.convertTo(); Tag tag = tagParam.convertTo();
@ -52,7 +56,7 @@ public class TagController {
log.debug("Tag to be created: [{}]", tag); log.debug("Tag to be created: [{}]", tag);
// Create and convert // Create and convert
return new TagDTO().convertFrom(tagService.create(tag)); return tagService.convertTo(tagService.create(tag));
} }
/** /**
@ -64,7 +68,7 @@ public class TagController {
@GetMapping("{tagId:\\d+}") @GetMapping("{tagId:\\d+}")
@ApiOperation("Get tag detail by id") @ApiOperation("Get tag detail by id")
public TagDTO getBy(@PathVariable("tagId") Integer tagId) { public TagDTO getBy(@PathVariable("tagId") Integer tagId) {
return new TagDTO().convertFrom(tagService.getById(tagId)); return tagService.convertTo(tagService.getById(tagId));
} }
@PutMapping("{tagId:\\d+}") @PutMapping("{tagId:\\d+}")
@ -78,20 +82,17 @@ public class TagController {
tagParam.update(tag); tagParam.update(tag);
// Update tag // Update tag
return new TagDTO().convertFrom(tagService.update(tag)); return tagService.convertTo(tagService.update(tag));
} }
/**
* Delete tag by id.
*
* @param tagId tag id
*/
@DeleteMapping("{tagId:\\d+}") @DeleteMapping("{tagId:\\d+}")
@ApiOperation("Delete tag by id") @ApiOperation("Deletes tag")
public void deletePermanently(@PathVariable("tagId") Integer tagId) { public TagDTO deletePermanently(@PathVariable("tagId") Integer tagId) {
// Remove the tag // Remove the tag
tagService.removeById(tagId); Tag deletedTag = tagService.removeById(tagId);
// Remove the post tag relationship // Remove the post tag relationship
postTagService.removeByTagId(tagId); postTagService.removeByTagId(tagId);
return tagService.convertTo(deletedTag);
} }
} }

View File

@ -1,6 +1,7 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.TagDTO;
import run.halo.app.model.entity.Tag; import run.halo.app.model.entity.Tag;
import run.halo.app.service.base.CrudService; import run.halo.app.service.base.CrudService;
@ -25,11 +26,20 @@ public interface TagService extends CrudService<Tag, Integer> {
Tag getBySlugNameOfNonNull(@NonNull String slugName); Tag getBySlugNameOfNonNull(@NonNull String slugName);
/** /**
* Converts to tag output dtos. * Converts to tag dto.
*
* @param tag tag must not be null
* @return tag dto
*/
@NonNull
TagDTO convertTo(@NonNull Tag tag);
/**
* Converts to tag dtos.
* *
* @param tags tag list * @param tags tag list
* @return a list of tag output dto * @return a list of tag output dto
*/ */
@NonNull @NonNull
List<TagDTO> convertTo(List<Tag> tags); List<TagDTO> convertTo(@Nullable List<Tag> tags);
} }

View File

@ -177,8 +177,7 @@ public class CategoryServiceImpl extends AbstractCrudService<Category, Integer>
return Collections.emptyList(); return Collections.emptyList();
} }
return categories return categories.stream()
.stream()
.map(this::convertTo) .map(this::convertTo)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -1,5 +1,9 @@
package run.halo.app.service.impl; package run.halo.app.service.impl;
import lombok.extern.slf4j.Slf4j;
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.TagDTO; import run.halo.app.model.dto.TagDTO;
@ -7,9 +11,6 @@ import run.halo.app.model.entity.Tag;
import run.halo.app.repository.TagRepository; import run.halo.app.repository.TagRepository;
import run.halo.app.service.TagService; import run.halo.app.service.TagService;
import run.halo.app.service.base.AbstractCrudService; import run.halo.app.service.base.AbstractCrudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -59,12 +60,21 @@ public class TagServiceImpl extends AbstractCrudService<Tag, Integer> implements
return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName)); return tagRepository.getBySlugName(slugName).orElseThrow(() -> new NotFoundException("The tag does not exist").setErrorData(slugName));
} }
@Override
public TagDTO convertTo(Tag tag) {
Assert.notNull(tag, "Tag must not be null");
return new TagDTO().convertFrom(tag);
}
@Override @Override
public List<TagDTO> convertTo(List<Tag> tags) { public List<TagDTO> convertTo(List<Tag> tags) {
return CollectionUtils.isEmpty(tags) ? if (CollectionUtils.isEmpty(tags)) {
Collections.emptyList() : return Collections.emptyList();
tags.stream() }
.map(tag -> (TagDTO) new TagDTO().convertFrom(tag))
return tags.stream()
.map(this::convertTo)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }