提交 204bc8b4 编写于 作者: J johnniang

Refactor sheet apis

上级 56ff28cb
......@@ -5,6 +5,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
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.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
......@@ -33,8 +34,9 @@ public class SheetController {
@GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet")
public Sheet getBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.getById(sheetId);
public SheetDetailDTO getBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.getById(sheetId);
return sheetService.convertToDetailDto(sheet);
}
@GetMapping
......@@ -46,20 +48,22 @@ public class SheetController {
@PostMapping
@ApiOperation("Creates a sheet")
public Sheet createBy(@RequestBody @Valid SheetParam sheetParam) {
return sheetService.createBy(sheetParam.convertTo());
public SheetDetailDTO createBy(@RequestBody @Valid SheetParam sheetParam) {
Sheet sheet = sheetService.createBy(sheetParam.convertTo());
return sheetService.convertToDetailDto(sheet);
}
@PutMapping("{sheetId:\\d+}")
@ApiOperation("Updates a sheet")
public Sheet updateBy(
public SheetDetailDTO updateBy(
@PathVariable("sheetId") Integer sheetId,
@RequestBody @Valid SheetParam sheetParam) {
return sheetService.updateBy(sheetParam.convertTo());
Sheet sheet = sheetService.updateBy(sheetParam.convertTo());
return sheetService.convertToDetailDto(sheet);
}
@PutMapping("{sheetId:\\d+}/{status}")
public Sheet updateStatusBy(
public void updateStatusBy(
@PathVariable("sheetId") Integer sheetId,
@PathVariable("status") PostStatus status) {
Sheet sheet = sheetService.getById(sheetId);
......@@ -67,13 +71,14 @@ public class SheetController {
// Set status
sheet.setStatus(status);
// Update and return
return sheetService.update(sheet);
// Update
sheetService.update(sheet);
}
@DeleteMapping("{sheetId:\\d+}")
@ApiOperation("Deletes a sheet")
public Sheet deleteBy(@PathVariable("sheetId") Integer sheetId) {
return sheetService.removeById(sheetId);
public SheetDetailDTO deleteBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.removeById(sheetId);
return sheetService.convertToDetailDto(sheet);
}
}
......@@ -44,15 +44,6 @@ public interface SheetService extends CrudService<Sheet, Integer> {
@NonNull
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.
*
......@@ -63,6 +54,17 @@ public interface SheetService extends CrudService<Sheet, Integer> {
@NonNull
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.
*
......@@ -71,4 +73,6 @@ public interface SheetService extends CrudService<Sheet, Integer> {
*/
@NonNull
Page<SheetListDTO> convertToListDto(@NonNull Page<Sheet> sheetPage);
}
......@@ -137,6 +137,7 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
// The sheet will be updated
// Set edit time
sheet.setEditTime(DateUtils.now());
// Update it
return update(sheet);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册