Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assembler for post and sheet to replace service convert #1691

Merged
merged 5 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.service.assembler.PostAssembler;
import run.halo.app.utils.HaloUtils;

/**
Expand All @@ -55,12 +56,16 @@ public class PostController {

private final OptionService optionService;

private final PostAssembler postAssembler;

public PostController(PostService postService,
AbstractStringCacheStore cacheStore,
OptionService optionService) {
OptionService optionService,
PostAssembler postAssembler) {
this.postService = postService;
this.cacheStore = cacheStore;
this.optionService = optionService;
this.postAssembler = postAssembler;
}

@GetMapping
Expand All @@ -71,17 +76,17 @@ public Page<? extends BasePostSimpleDTO> pageBy(
@RequestParam(value = "more", defaultValue = "true") Boolean more) {
Page<Post> postPage = postService.pageBy(postQuery, pageable);
if (more) {
return postService.convertToListVo(postPage, true);
return postAssembler.convertToListVo(postPage);
}

return postService.convertToSimple(postPage);
return postAssembler.convertToSimple(postPage);
}

@GetMapping("latest")
@ApiOperation("Pages latest post")
public List<BasePostMinimalDTO> pageLatest(
@RequestParam(name = "top", defaultValue = "10") int top) {
return postService.convertToMinimal(postService.pageLatest(top).getContent());
return postAssembler.convertToMinimal(postService.pageLatest(top).getContent());
}

@GetMapping("status/{status}")
Expand All @@ -93,17 +98,17 @@ public Page<? extends BasePostSimpleDTO> pageByStatus(
Page<Post> posts = postService.pageBy(status, pageable);

if (more) {
return postService.convertToListVo(posts, true);
return postAssembler.convertToListVo(posts);
}

return postService.convertToSimple(posts);
return postAssembler.convertToSimple(posts);
}

@GetMapping("{postId:\\d+}")
@ApiOperation("Gets a post")
public PostDetailVO getBy(@PathVariable("postId") Integer postId) {
Post post = postService.getWithLatestContentById(postId);
return postService.convertToDetailVo(post, true);
return postAssembler.convertToDetailVo(post);
}

@PutMapping("{postId:\\d+}/likes")
Expand Down Expand Up @@ -164,7 +169,7 @@ public BasePostDetailDTO updateDraftBy(
// Update draft content
Post post = postService.updateDraftContent(formattedContent,
contentParam.getOriginalContent(), postId);
return postService.convertToDetail(post);
return postAssembler.convertToDetail(post);
}

@DeleteMapping("{postId:\\d+}")
Expand All @@ -187,7 +192,7 @@ public String preview(@PathVariable("postId") Integer postId)

post.setSlug(URLEncoder.encode(post.getSlug(), StandardCharsets.UTF_8.name()));

BasePostMinimalDTO postMinimalDTO = postService.convertToMinimal(post);
BasePostMinimalDTO postMinimalDTO = postAssembler.convertToMinimal(post);

String token = HaloUtils.simpleUUID();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import run.halo.app.model.vo.SheetListVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.SheetService;
import run.halo.app.service.assembler.SheetAssembler;
import run.halo.app.utils.HaloUtils;

/**
Expand All @@ -52,27 +53,31 @@ public class SheetController {

private final OptionService optionService;

private final SheetAssembler sheetAssembler;

public SheetController(SheetService sheetService,
AbstractStringCacheStore cacheStore,
OptionService optionService) {
OptionService optionService,
SheetAssembler sheetAssembler) {
this.sheetService = sheetService;
this.cacheStore = cacheStore;
this.optionService = optionService;
this.sheetAssembler = sheetAssembler;
}

@GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet")
public SheetDetailVO getBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.getWithLatestContentById(sheetId);
return sheetService.convertToDetailVo(sheet);
return sheetAssembler.convertToDetailVo(sheet);
}

@GetMapping
@ApiOperation("Gets a page of sheet")
public Page<SheetListVO> pageBy(
@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable) {
Page<Sheet> sheetPage = sheetService.pageBy(pageable);
return sheetService.convertToListVo(sheetPage);
return sheetAssembler.convertToListVo(sheetPage);
}

@GetMapping("independent")
Expand All @@ -88,7 +93,7 @@ public SheetDetailVO createBy(@RequestBody @Valid SheetParam sheetParam,
Boolean autoSave) {
Sheet sheet =
sheetService.createBy(sheetParam.convertTo(), sheetParam.getSheetMetas(), autoSave);
return sheetService.convertToDetailVo(sheet);
return sheetAssembler.convertToDetailVo(sheet);
}

@PutMapping("{sheetId:\\d+}")
Expand All @@ -104,7 +109,7 @@ public SheetDetailVO updateBy(

Sheet sheet = sheetService.updateBy(sheetToUpdate, sheetParam.getSheetMetas(), autoSave);

return sheetService.convertToDetailVo(sheet);
return sheetAssembler.convertToDetailVo(sheet);
}

@PutMapping("{sheetId:\\d+}/{status}")
Expand Down Expand Up @@ -132,14 +137,14 @@ public BasePostDetailDTO updateDraftBy(
// Update draft content
Sheet sheet = sheetService.updateDraftContent(formattedContent,
contentParam.getOriginalContent(), sheetId);
return sheetService.convertToDetail(sheet);
return sheetAssembler.convertToDetail(sheet);
}

@DeleteMapping("{sheetId:\\d+}")
@ApiOperation("Deletes a sheet")
public SheetDetailVO deleteBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.removeById(sheetId);
return sheetService.convertToDetailVo(sheet);
return sheetAssembler.convertToDetailVo(sheet);
}

@GetMapping("preview/{sheetId:\\d+}")
Expand All @@ -150,7 +155,7 @@ public String preview(@PathVariable("sheetId") Integer sheetId)

sheet.setSlug(URLEncoder.encode(sheet.getSlug(), StandardCharsets.UTF_8.name()));

BasePostMinimalDTO sheetMinimalDTO = sheetService.convertToMinimal(sheet);
BasePostMinimalDTO sheetMinimalDTO = sheetAssembler.convertToMinimal(sheet);

String token = HaloUtils.simpleUUID();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import run.halo.app.service.PostService;
import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService;
import run.halo.app.service.assembler.PostRenderAssembler;

/**
* @author ryanwang
Expand Down Expand Up @@ -78,6 +79,8 @@ public class ContentContentController {

private final ThemeService themeService;

private final PostRenderAssembler postRenderAssembler;

private final ContentAuthenticationManager providerManager;

public ContentContentController(PostModel postModel,
Expand All @@ -92,6 +95,7 @@ public ContentContentController(PostModel postModel,
SheetService sheetService,
CategoryService categoryService,
ThemeService themeService,
PostRenderAssembler postRenderAssembler,
ContentAuthenticationManager providerManager) {
this.postModel = postModel;
this.sheetModel = sheetModel;
Expand All @@ -105,6 +109,7 @@ public ContentContentController(PostModel postModel,
this.sheetService = sheetService;
this.categoryService = categoryService;
this.themeService = themeService;
this.postRenderAssembler = postRenderAssembler;
this.providerManager = providerManager;
}

Expand Down Expand Up @@ -270,7 +275,7 @@ private String authenticatePost(String slug, String type, String password,
authRequest.setPrincipal(EncryptTypeEnum.POST.getName());
try {
providerManager.authenticate(authRequest);
BasePostMinimalDTO basePostMinimal = postService.convertToMinimal(post);
BasePostMinimalDTO basePostMinimal = postRenderAssembler.convertToMinimal(post);
return "redirect:" + buildRedirectUrl(basePostMinimal.getFullPath());
} catch (AuthenticationException e) {
request.setAttribute("errorMsg", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.service.CategoryService;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.PostService;
import run.halo.app.service.assembler.PostRenderAssembler;

/**
* @author ryanwang
Expand All @@ -58,6 +58,8 @@ public class ContentFeedController {

private final PostService postService;

private final PostRenderAssembler postRenderAssembler;

private final CategoryService categoryService;

private final PostCategoryService postCategoryService;
Expand All @@ -67,11 +69,12 @@ public class ContentFeedController {
private final FreeMarkerConfigurer freeMarker;

public ContentFeedController(PostService postService,
CategoryService categoryService,
PostRenderAssembler postRenderAssembler, CategoryService categoryService,
PostCategoryService postCategoryService,
OptionService optionService,
FreeMarkerConfigurer freeMarker) {
this.postService = postService;
this.postRenderAssembler = postRenderAssembler;
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
this.optionService = optionService;
Expand Down Expand Up @@ -258,14 +261,7 @@ private List<PostDetailVO> buildPosts(@NonNull Pageable pageable) {
@NonNull
private Page<PostDetailVO> convertToDetailPageVo(Page<Post> postPage) {
Assert.notNull(postPage, "The postPage must not be null.");

// Populate post content
postPage.getContent().forEach(post -> {
Content postContent = postService.getContentById(post.getId());
post.setContent(Content.PatchedContent.of(postContent));
});

Page<PostDetailVO> posts = postService.convertToDetailVo(postPage);
Page<PostDetailVO> posts = postRenderAssembler.convertToDetailVo(postPage);
posts.getContent().forEach(postDetailVO -> {
postDetailVO.setContent(
RegExUtils.replaceAll(postDetailVO.getContent(), XML_INVALID_CHAR, ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.service.ThemeService;
import run.halo.app.service.assembler.PostRenderAssembler;

/**
* Search controller.
Expand All @@ -32,13 +33,17 @@ public class ContentSearchController {

private final PostService postService;

private final PostRenderAssembler postRenderAssembler;

private final OptionService optionService;

private final ThemeService themeService;

public ContentSearchController(PostService postService, OptionService optionService,
public ContentSearchController(PostService postService,
PostRenderAssembler postRenderAssembler, OptionService optionService,
ThemeService themeService) {
this.postService = postService;
this.postRenderAssembler = postRenderAssembler;
this.optionService = optionService;
this.themeService = themeService;
}
Expand Down Expand Up @@ -71,7 +76,7 @@ public String search(Model model,
final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort);
final Page<Post> postPage = postService.pageBy(keyword, pageable);

final Page<PostListVO> posts = postService.convertToListVo(postPage);
final Page<PostListVO> posts = postRenderAssembler.convertToListVo(postPage);

model.addAttribute("is_search", true);
model.addAttribute("keyword", HtmlUtils.htmlEscape(keyword));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.PostService;
import run.halo.app.service.assembler.PostRenderAssembler;

/**
* Content category controller.
Expand All @@ -45,20 +45,20 @@ public class CategoryController {

private final PostCategoryService postCategoryService;

private final PostService postService;
private final PostRenderAssembler postRenderAssembler;

private final CategoryAuthentication categoryAuthentication;

private final ContentAuthenticationManager contentAuthenticationManager;

public CategoryController(CategoryService categoryService,
PostCategoryService postCategoryService,
PostService postService,
PostRenderAssembler postRenderAssembler,
CategoryAuthentication categoryAuthentication,
ContentAuthenticationManager contentAuthenticationManager) {
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
this.postService = postService;
this.postRenderAssembler = postRenderAssembler;
this.categoryAuthentication = categoryAuthentication;
this.contentAuthenticationManager = contentAuthenticationManager;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public Page<PostListVO> listPostsBy(@PathVariable("slug") String slug,

Page<Post> postPage =
postCategoryService.pagePostBy(category.getId(), statusesToQuery, pageable);
return postService.convertToListVo(postPage);
return postRenderAssembler.convertToListVo(postPage);
}

private boolean allowIntimatePosts(Integer categoryId, String password) {
Expand Down
Loading