Refactor sheet apis

pull/146/head
johnniang 2019-04-24 22:01:10 +08:00
parent 56ff28cbbf
commit 204bc8b4a4
3 changed files with 30 additions and 20 deletions

View File

@ -5,6 +5,7 @@ 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.SheetListDTO; import run.halo.app.model.dto.post.SheetListDTO;
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;
@ -33,8 +34,9 @@ public class SheetController {
@GetMapping("{sheetId:\\d+}") @GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet") @ApiOperation("Gets a sheet")
public Sheet getBy(@PathVariable("sheetId") Integer sheetId) { public SheetDetailDTO getBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.getById(sheetId); Sheet sheet = sheetService.getById(sheetId);
return sheetService.convertToDetailDto(sheet);
} }
@GetMapping @GetMapping
@ -46,20 +48,22 @@ public class SheetController {
@PostMapping @PostMapping
@ApiOperation("Creates a sheet") @ApiOperation("Creates a sheet")
public Sheet createBy(@RequestBody @Valid SheetParam sheetParam) { public SheetDetailDTO createBy(@RequestBody @Valid SheetParam sheetParam) {
return sheetService.createBy(sheetParam.convertTo()); Sheet sheet = sheetService.createBy(sheetParam.convertTo());
return sheetService.convertToDetailDto(sheet);
} }
@PutMapping("{sheetId:\\d+}") @PutMapping("{sheetId:\\d+}")
@ApiOperation("Updates a sheet") @ApiOperation("Updates a sheet")
public Sheet updateBy( public SheetDetailDTO updateBy(
@PathVariable("sheetId") Integer sheetId, @PathVariable("sheetId") Integer sheetId,
@RequestBody @Valid SheetParam sheetParam) { @RequestBody @Valid SheetParam sheetParam) {
return sheetService.updateBy(sheetParam.convertTo()); Sheet sheet = sheetService.updateBy(sheetParam.convertTo());
return sheetService.convertToDetailDto(sheet);
} }
@PutMapping("{sheetId:\\d+}/{status}") @PutMapping("{sheetId:\\d+}/{status}")
public Sheet updateStatusBy( public void updateStatusBy(
@PathVariable("sheetId") Integer sheetId, @PathVariable("sheetId") Integer sheetId,
@PathVariable("status") PostStatus status) { @PathVariable("status") PostStatus status) {
Sheet sheet = sheetService.getById(sheetId); Sheet sheet = sheetService.getById(sheetId);
@ -67,13 +71,14 @@ public class SheetController {
// Set status // Set status
sheet.setStatus(status); sheet.setStatus(status);
// Update and return // Update
return sheetService.update(sheet); sheetService.update(sheet);
} }
@DeleteMapping("{sheetId:\\d+}") @DeleteMapping("{sheetId:\\d+}")
@ApiOperation("Deletes a sheet") @ApiOperation("Deletes a sheet")
public Sheet deleteBy(@PathVariable("sheetId") Integer sheetId) { public SheetDetailDTO deleteBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.removeById(sheetId); Sheet sheet = sheetService.removeById(sheetId);
return sheetService.convertToDetailDto(sheet);
} }
} }

View File

@ -44,15 +44,6 @@ public interface SheetService extends CrudService<Sheet, Integer> {
@NonNull @NonNull
Page<Sheet> pageBy(@NonNull Pageable pageable); Page<Sheet> pageBy(@NonNull Pageable pageable);
/**
* Converts to detail dto.
*
* @param sheet sheet must not be null
* @return sheet detail dto
*/
@NonNull
SheetDetailDTO convertToDetailDto(@NonNull Sheet sheet);
/** /**
* Gets sheet by post status and url. * Gets sheet by post status and url.
* *
@ -63,6 +54,17 @@ public interface SheetService extends CrudService<Sheet, Integer> {
@NonNull @NonNull
Sheet getBy(@NonNull PostStatus status, @NonNull String url); Sheet getBy(@NonNull PostStatus status, @NonNull 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.
* *
@ -71,4 +73,6 @@ public interface SheetService extends CrudService<Sheet, Integer> {
*/ */
@NonNull @NonNull
Page<SheetListDTO> convertToListDto(@NonNull Page<Sheet> sheetPage); Page<SheetListDTO> convertToListDto(@NonNull Page<Sheet> sheetPage);
} }

View File

@ -137,6 +137,7 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
// The sheet will be updated // The sheet will be updated
// Set edit time // Set edit time
sheet.setEditTime(DateUtils.now()); sheet.setEditTime(DateUtils.now());
// Update it // Update it
return update(sheet); return update(sheet);
} }