Refactor sheet service and contorller

pull/146/head
johnniang 2019-04-25 22:09:05 +08:00
parent 64397753be
commit 16476f5a9b
4 changed files with 14 additions and 37 deletions

View File

@ -5,11 +5,11 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault; import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.post.SheetDetailDTO; import run.halo.app.model.dto.post.BasePostDetailDTO;
import run.halo.app.model.vo.SheetListVO;
import run.halo.app.model.entity.Sheet; import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.SheetParam; import run.halo.app.model.params.SheetParam;
import run.halo.app.model.vo.SheetListVO;
import run.halo.app.service.SheetService; import run.halo.app.service.SheetService;
import javax.validation.Valid; import javax.validation.Valid;
@ -34,28 +34,28 @@ public class SheetController {
@GetMapping("{sheetId:\\d+}") @GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet") @ApiOperation("Gets a sheet")
public SheetDetailDTO getBy(@PathVariable("sheetId") Integer sheetId) { public BasePostDetailDTO getBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.getById(sheetId); Sheet sheet = sheetService.getById(sheetId);
return sheetService.convertToDetailDto(sheet); return sheetService.convertToDetail(sheet);
} }
@GetMapping @GetMapping
@ApiOperation("Gets a page of sheet") @ApiOperation("Gets a page of sheet")
public Page<SheetListVO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) { public Page<SheetListVO> pageBy(@PageableDefault(sort = "editTime", direction = DESC) Pageable pageable) {
Page<Sheet> sheetPage = sheetService.pageBy(pageable); Page<Sheet> sheetPage = sheetService.pageBy(pageable);
return sheetService.convertToListDto(sheetPage); return sheetService.convertToListVo(sheetPage);
} }
@PostMapping @PostMapping
@ApiOperation("Creates a sheet") @ApiOperation("Creates a sheet")
public SheetDetailDTO createBy(@RequestBody @Valid SheetParam sheetParam) { public BasePostDetailDTO createBy(@RequestBody @Valid SheetParam sheetParam) {
Sheet sheet = sheetService.createBy(sheetParam.convertTo()); Sheet sheet = sheetService.createBy(sheetParam.convertTo());
return sheetService.convertToDetailDto(sheet); return sheetService.convertToDetail(sheet);
} }
@PutMapping("{sheetId:\\d+}") @PutMapping("{sheetId:\\d+}")
@ApiOperation("Updates a sheet") @ApiOperation("Updates a sheet")
public SheetDetailDTO updateBy( public BasePostDetailDTO updateBy(
@PathVariable("sheetId") Integer sheetId, @PathVariable("sheetId") Integer sheetId,
@RequestBody @Valid SheetParam sheetParam) { @RequestBody @Valid SheetParam sheetParam) {
Sheet sheetToUpdate = sheetService.getById(sheetId); Sheet sheetToUpdate = sheetService.getById(sheetId);
@ -64,7 +64,7 @@ public class SheetController {
Sheet sheet = sheetService.updateBy(sheetToUpdate); Sheet sheet = sheetService.updateBy(sheetToUpdate);
return sheetService.convertToDetailDto(sheet); return sheetService.convertToDetail(sheet);
} }
@PutMapping("{sheetId:\\d+}/{status}") @PutMapping("{sheetId:\\d+}/{status}")
@ -82,8 +82,8 @@ public class SheetController {
@DeleteMapping("{sheetId:\\d+}") @DeleteMapping("{sheetId:\\d+}")
@ApiOperation("Deletes a sheet") @ApiOperation("Deletes a sheet")
public SheetDetailDTO deleteBy(@PathVariable("sheetId") Integer sheetId) { public BasePostDetailDTO deleteBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.removeById(sheetId); Sheet sheet = sheetService.removeById(sheetId);
return sheetService.convertToDetailDto(sheet); return sheetService.convertToDetail(sheet);
} }
} }

View File

@ -8,7 +8,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.model.entity.Sheet; import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.PostCommentService;
import run.halo.app.service.SheetService; import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
@ -22,15 +21,11 @@ public class ContentSheetController {
private final SheetService sheetService; private final SheetService sheetService;
private final PostCommentService postCommentService;
private final ThemeService themeService; private final ThemeService themeService;
public ContentSheetController(SheetService sheetService, public ContentSheetController(SheetService sheetService,
PostCommentService postCommentService,
ThemeService themeService) { ThemeService themeService) {
this.sheetService = sheetService; this.sheetService = sheetService;
this.postCommentService = postCommentService;
this.themeService = themeService; this.themeService = themeService;
} }
@ -67,7 +62,7 @@ public class ContentSheetController {
Model model) { Model model) {
Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url); Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url);
model.addAttribute("sheet", sheetService.convertToDetailDto(sheet)); model.addAttribute("sheet", sheetService.convertToDetail(sheet));
if (StrUtil.isNotEmpty(sheet.getTemplate())) { if (StrUtil.isNotEmpty(sheet.getTemplate())) {
return themeService.render(sheet.getTemplate()); return themeService.render(sheet.getTemplate());

View File

@ -37,16 +37,6 @@ public interface SheetService extends BasePostService<Sheet> {
@Override @Override
Sheet getBy(PostStatus status, String url); Sheet getBy(PostStatus status, String url);
/**
* Converts to detail dto.
*
* @param sheet sheet must not be null
* @return sheet detail dto
*/
@NonNull
SheetDetailDTO convertToDetailDto(@NonNull Sheet sheet);
/** /**
* Converts to list dto page. * Converts to list dto page.
* *
@ -54,7 +44,7 @@ public interface SheetService extends BasePostService<Sheet> {
* @return a page of sheet list dto * @return a page of sheet list dto
*/ */
@NonNull @NonNull
Page<SheetListVO> convertToListDto(@NonNull Page<Sheet> sheetPage); Page<SheetListVO> convertToListVo(@NonNull Page<Sheet> sheetPage);
} }

View File

@ -80,15 +80,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
} }
@Override @Override
public SheetDetailDTO convertToDetailDto(Sheet sheet) { public Page<SheetListVO> convertToListVo(Page<Sheet> sheetPage) {
Assert.notNull(sheet, "Sheet must not be null");
// Convert and return
return new SheetDetailDTO().convertFrom(sheet);
}
@Override
public Page<SheetListVO> convertToListDto(Page<Sheet> sheetPage) {
Assert.notNull(sheetPage, "Sheet page must not be null"); Assert.notNull(sheetPage, "Sheet page must not be null");
// Get all sheet id // Get all sheet id