Skip to content

Commit

Permalink
feat: #1103 (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Dec 1, 2020
1 parent a05c74a commit dccd213
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ public String content(@PathVariable("prefix") String prefix,
Model model) {
if (optionService.getArchivesPrefix().equals(prefix)) {
return postModel.archives(page, model);
} else if (optionService.getJournalsPrefix().equals(prefix)) {
}

if (optionService.getJournalsPrefix().equals(prefix)) {
return journalModel.list(page, model);
} else if (optionService.getPhotosPrefix().equals(prefix)) {
}

if (optionService.getPhotosPrefix().equals(prefix)) {
return photoModel.list(page, model);
} else {
throw new NotFoundException("Not Found");
}

throw new NotFoundException("Not Found");
}

@GetMapping("{prefix}/{slug}")
Expand All @@ -124,23 +128,36 @@ public String content(@PathVariable("prefix") String prefix,
@RequestParam(value = "token", required = false) String token,
Model model) {
PostPermalinkType postPermalinkType = optionService.getPostPermalinkType();
if (optionService.getArchivesPrefix().equals(prefix)) {
if (postPermalinkType.equals(PostPermalinkType.DEFAULT)) {
Post post = postService.getBySlug(slug);
return postModel.content(post, token, model);
}
if (postPermalinkType.equals(PostPermalinkType.ID_SLUG) && StringUtils.isNumeric(slug)) {
Post post = postService.getById(Integer.parseInt(slug));
return postModel.content(post, token, model);
}
}

if (postPermalinkType.equals(PostPermalinkType.DEFAULT) && optionService.getArchivesPrefix().equals(prefix)) {
Post post = postService.getBySlug(slug);
return postModel.content(post, token, model);
} else if (postPermalinkType.equals(PostPermalinkType.YEAR) && prefix.length() == 4 && StringUtils.isNumeric(prefix)) {
Post post = postService.getBy(Integer.parseInt(prefix), slug);
return postModel.content(post, token, model);
} else if (optionService.getSheetPrefix().equals(prefix)) {
if (optionService.getSheetPrefix().equals(prefix)) {
Sheet sheet = sheetService.getBySlug(slug);
return sheetModel.content(sheet, token, model);
} else if (optionService.getCategoriesPrefix().equals(prefix)) {
}

if (optionService.getCategoriesPrefix().equals(prefix)) {
return categoryModel.listPost(model, slug, 1);
} else if (optionService.getTagsPrefix().equals(prefix)) {
}

if (optionService.getTagsPrefix().equals(prefix)) {
return tagModel.listPost(model, slug, 1);
} else {
throw new NotFoundException("Not Found");
}

if (postPermalinkType.equals(PostPermalinkType.YEAR) && prefix.length() == 4 && StringUtils.isNumeric(prefix)) {
Post post = postService.getBy(Integer.parseInt(prefix), slug);
return postModel.content(post, token, model);
}

throw new NotFoundException("Not Found");
}

@GetMapping("{prefix}/{slug}/page/{page:\\d+}")
Expand All @@ -150,11 +167,13 @@ public String content(@PathVariable("prefix") String prefix,
Model model) {
if (optionService.getCategoriesPrefix().equals(prefix)) {
return categoryModel.listPost(model, slug, page);
} else if (optionService.getTagsPrefix().equals(prefix)) {
}

if (optionService.getTagsPrefix().equals(prefix)) {
return tagModel.listPost(model, slug, page);
} else {
throw new NotFoundException("Not Found");
}

throw new NotFoundException("Not Found");
}

@GetMapping("{year:\\d+}/{month:\\d+}/{slug}")
Expand All @@ -167,9 +186,9 @@ public String content(@PathVariable("year") Integer year,
if (postPermalinkType.equals(PostPermalinkType.DATE)) {
Post post = postService.getBy(year, month, slug);
return postModel.content(post, token, model);
} else {
throw new NotFoundException("Not Found");
}

throw new NotFoundException("Not Found");
}

@GetMapping("{year:\\d+}/{month:\\d+}/{day:\\d+}/{slug}")
Expand All @@ -183,9 +202,9 @@ public String content(@PathVariable("year") Integer year,
if (postPermalinkType.equals(PostPermalinkType.DAY)) {
Post post = postService.getBy(year, month, day, slug);
return postModel.content(post, token, model);
} else {
throw new NotFoundException("Not Found");
}

throw new NotFoundException("Not Found");
}

@PostMapping(value = "archives/{slug:.*}/password")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public enum PostPermalinkType implements ValueEnum<Integer> {
/**
* /1970/${slug}
*/
YEAR(4);
YEAR(4),

/**
* archives/${id}
*/
ID_SLUG(5);

private final Integer value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ private BasePostMinimalDTO buildPostFullPath(BasePostMinimalDTO post) {
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
} else if (permalinkType.equals(PostPermalinkType.ID_SLUG)) {
fullPath.append(archivesPrefix)
.append(URL_SEPARATOR)
.append(post.getId())
.append(pathSuffix);
}

post.setFullPath(fullPath.toString());
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/run/halo/app/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,11 @@ private String buildFullPath(Post post) {
.append(URL_SEPARATOR)
.append(post.getSlug())
.append(pathSuffix);
} else if (permalinkType.equals(PostPermalinkType.ID_SLUG)) {
fullPath.append(archivesPrefix)
.append(URL_SEPARATOR)
.append(post.getId())
.append(pathSuffix);
}
return fullPath.toString();
}
Expand Down

0 comments on commit dccd213

Please sign in to comment.