Skip to content

Commit 7571db7

Browse files
author
leijiankun
committed
Support post type: PAGE
1 parent c519d60 commit 7571db7

13 files changed

Lines changed: 200 additions & 133 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies {
4747

4848
// view
4949
compile 'net.sourceforge.nekohtml:nekohtml:1.9.22'
50-
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE'
50+
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2+'
5151
compile 'de.neuland-bfi:spring-jade4j:0.4.2'
5252
compile 'org.pegdown:pegdown:1.6.0'
5353

src/main/java/com/raysmond/blog/admin/controllers/PostController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.raysmond.blog.models.Post;
55
import com.raysmond.blog.models.support.PostFormat;
66
import com.raysmond.blog.models.support.PostStatus;
7+
import com.raysmond.blog.models.support.PostType;
78
import com.raysmond.blog.repositories.PostRepository;
89
import com.raysmond.blog.repositories.UserRepository;
910
import com.raysmond.blog.services.PostService;
@@ -15,6 +16,7 @@
1516
import org.springframework.stereotype.Controller;
1617
import org.springframework.ui.Model;
1718
import org.springframework.validation.Errors;
19+
import org.springframework.web.bind.annotation.GetMapping;
1820
import org.springframework.web.bind.annotation.PathVariable;
1921
import org.springframework.web.bind.annotation.RequestMapping;
2022
import org.springframework.web.bind.annotation.RequestParam;
@@ -39,7 +41,7 @@ public class PostController {
3941
@Autowired
4042
private UserRepository userRepository;
4143

42-
@RequestMapping(value = "")
44+
@GetMapping("")
4345
public String index(@RequestParam(defaultValue = "0") int page, Model model) {
4446
Page<Post> posts = postRepository.findAll(new PageRequest(page, PAGE_SIZE, Sort.Direction.DESC, "id"));
4547

@@ -50,13 +52,14 @@ public String index(@RequestParam(defaultValue = "0") int page, Model model) {
5052
return "admin/posts/index";
5153
}
5254

53-
@RequestMapping(value = "new")
55+
@GetMapping("new")
5456
public String newPost(Model model) {
5557
PostForm postForm = DTOUtil.map(new Post(), PostForm.class);
5658
postForm.setPostTags("");
5759

5860
model.addAttribute("postForm", postForm);
5961
model.addAttribute("postFormats", PostFormat.values());
62+
model.addAttribute("postTypes", PostType.values());
6063
model.addAttribute("postStatus", PostStatus.values());
6164

6265
return "admin/posts/new";
@@ -72,6 +75,7 @@ public String editPost(@PathVariable Long postId, Model model) {
7275
model.addAttribute("post", post);
7376
model.addAttribute("postForm", postForm);
7477
model.addAttribute("postFormats", PostFormat.values());
78+
model.addAttribute("postTypes", PostType.values());
7579
model.addAttribute("postStatus", PostStatus.values());
7680

7781
return "admin/posts/edit";
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.raysmond.blog.controllers;
22

3-
import com.raysmond.blog.Constants;
43
import com.raysmond.blog.models.Post;
54
import com.raysmond.blog.services.AppSetting;
65
import com.raysmond.blog.services.PostService;
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.data.domain.Page;
98
import org.springframework.stereotype.Controller;
109
import org.springframework.ui.Model;
11-
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.GetMapping;
1211
import org.springframework.web.bind.annotation.RequestParam;
1312

14-
import static org.springframework.web.bind.annotation.RequestMethod.GET;
15-
1613
@Controller
1714
public class HomeController {
1815

@@ -22,28 +19,14 @@ public class HomeController {
2219
@Autowired
2320
private AppSetting appSetting;
2421

25-
@RequestMapping(value = "", method = GET)
22+
@GetMapping(value = "")
2623
public String index(@RequestParam(defaultValue = "1") int page, Model model) {
2724
page = page < 1 ? 0 : page - 1;
2825
Page<Post> posts = postService.getAllPublishedPostsByPage(page, appSetting.getPageSize());
2926

3027
model.addAttribute("totalPages", posts.getTotalPages());
3128
model.addAttribute("posts", posts);
3229
model.addAttribute("page", page + 1);
33-
3430
return "home/home";
3531
}
36-
37-
@RequestMapping(value = "about", method = GET)
38-
public String about(Model model) {
39-
Post post = postService.getPublishedPostByPermalink(Constants.ABOUT_PAGE_PERMALINK);
40-
41-
if (post == null) {
42-
post = postService.createAboutPage();
43-
}
44-
45-
model.addAttribute("about", post);
46-
return "home/about";
47-
}
48-
4932
}

src/main/java/com/raysmond/blog/controllers/PostController.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import com.google.common.collect.Maps;
55
import com.raysmond.blog.error.NotFoundException;
66
import com.raysmond.blog.models.Post;
7+
import com.raysmond.blog.models.support.PostType;
78
import com.raysmond.blog.services.PostService;
89

910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.stereotype.Controller;
1112
import org.springframework.ui.Model;
13+
import org.springframework.web.bind.annotation.GetMapping;
1214
import org.springframework.web.bind.annotation.PathVariable;
1315
import org.springframework.web.bind.annotation.RequestMapping;
1416

@@ -21,12 +23,11 @@
2123
* @author Raysmond
2224
*/
2325
@Controller
24-
@RequestMapping("posts")
2526
public class PostController {
2627
@Autowired
2728
private PostService postService;
2829

29-
@RequestMapping(value = "archive", method = GET)
30+
@GetMapping(value = "posts/archive")
3031
public String archive(Model model) {
3132
Map<Integer, List<Post>> posts = Maps.newHashMap();
3233
postService.getArchivePosts().forEach(post -> {
@@ -39,27 +40,38 @@ public String archive(Model model) {
3940
return "posts/archive";
4041
}
4142

42-
@RequestMapping(value = "{permalink}", method = GET)
43+
@GetMapping(value = "posts/{permalink}")
4344
public String show(@PathVariable String permalink, Model model) {
44-
Post post = null;
45+
return showPost(permalink, model, PostType.POST);
46+
}
47+
48+
@GetMapping(value = "{permalink}")
49+
public String page(@PathVariable String permalink, Model model) {
50+
return showPost(permalink, model, PostType.PAGE);
51+
}
52+
53+
private String showPost(String permalink, Model model, PostType postType) {
54+
Post post;
4555

4656
try {
4757
post = postService.getPublishedPostByPermalink(permalink);
4858
} catch (NotFoundException ex) {
49-
if (permalink.matches("\\d+")) {
59+
if (permalink.matches("\\d+") && postType.equals(PostType.POST)) {
5060
post = postService.getPost(Long.valueOf(permalink));
61+
} else {
62+
throw new NotFoundException();
5163
}
5264
}
5365

54-
if (post == null) {
55-
throw new NotFoundException("Post with permalink " + permalink + " is not found");
66+
if (!post.getPostType().equals(postType)) {
67+
throw new NotFoundException();
5668
}
5769

58-
model.addAttribute("post", post);
59-
model.addAttribute("tags", postService.getPostTags(post));
60-
6170
postService.incrementViews(post.getId());
6271

72+
model.addAttribute("postType", postType.name());
73+
model.addAttribute("post", post);
74+
model.addAttribute("tags", postService.getPostTags(post));
6375
return "posts/post";
6476
}
6577

src/main/java/com/raysmond/blog/forms/PostForm.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.raysmond.blog.models.support.PostFormat;
44
import com.raysmond.blog.models.support.PostStatus;
5+
import com.raysmond.blog.models.support.PostType;
56

67
import lombok.Data;
78

@@ -35,4 +36,7 @@ public class PostForm {
3536
@NotNull
3637
private String postTags;
3738

39+
@NotNull
40+
private PostType postType;
41+
3842
}

src/main/java/com/raysmond/blog/forms/SettingsForm.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.raysmond.blog.forms;
22

33
import lombok.Data;
4+
45
import org.hibernate.validator.constraints.NotEmpty;
56

67
import javax.validation.constraints.NotNull;
@@ -20,4 +21,8 @@ public class SettingsForm {
2021
@NotNull
2122
private Integer pageSize;
2223

24+
private String intro;
25+
26+
private String pictureUrl;
27+
2328
}

src/main/resources/templates/admin/home/settings.jade

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ block content
2121
td.setting-label Page Size
2222
td.input
2323
input.form-control(type="text", name="pageSize", value="#{settings.getPageSize()}")
24-
24+
25+
tr
26+
td.setting-label Intro
27+
td.input
28+
input.form-control(type="text", name="intro", value="#{settings.getIntro()}")
29+
30+
tr
31+
td.setting-label Picture Url
32+
td.input
33+
input.form-control(type="text", name="pictureUrl", value="#{settings.getPictureUrl()}")
34+
2535
tr
2636
td.setting-label
2737
td.input
Lines changed: 80 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,96 @@
11
extends ../layout/admin
22

33
block head
4-
script(src="/bower_components/ace-builds/src-min/ace.js")
5-
script(src="/bower_components/ace-builds/src-min/theme-github.js")
6-
script(src="/bower_components/ace-builds/src-min/mode-markdown.js")
4+
script(src="/bower_components/ace-builds/src-min/ace.js")
5+
script(src="/bower_components/ace-builds/src-min/theme-github.js")
6+
script(src="/bower_components/ace-builds/src-min/mode-markdown.js")
77

88
block content
9-
h1 Edit Post
10-
hr
9+
h1 Edit Post
10+
hr
1111

12-
form.post-form(method="post",action="/admin/posts/#{post.getId()}")
13-
.item-row
14-
input(type="hidden", name='_csrf', value='#{_csrf.token}')
15-
.form-group
16-
label 标题
17-
input.form-control(type="text", name="title", value='#{postForm.getTitle()}')
12+
form.post-form(method="post",action="/admin/posts/#{post.getId()}")
13+
.item-row
14+
input(type="hidden", name='_csrf', value='#{_csrf.token}')
15+
.form-group
16+
label 标题
17+
input.form-control(type="text", name="title", value='#{postForm.getTitle()}')
1818

19-
.form-group
20-
label 正文
21-
textarea.form-control#content(name="content", style="display:none;")
22-
= postForm.getContent()
23-
div#content-editor
24-
#{postForm.getContent()}
19+
.form-group
20+
label 正文
21+
textarea.form-control#content(name="content", style="display:none;")
22+
= postForm.getContent()
23+
div#content-editor
24+
#{postForm.getContent()}
2525

26-
.form-group
27-
label 摘要
28-
textarea.form-control#summary(name="summary", style="display:none;")
29-
= postForm.getSummary()
30-
div#summary-editor
31-
#{postForm.getSummary()}
26+
.form-group
27+
label 摘要
28+
textarea.form-control#summary(name="summary", style="display:none;")
29+
= postForm.getSummary()
30+
div#summary-editor
31+
#{postForm.getSummary()}
3232

33-
.item-row
34-
hr
35-
.row
36-
.col-sm-3
37-
span Format
38-
select.form-control(name="postFormat")
39-
for format in postFormats
40-
if format != postForm.getPostFormat()
41-
option(value="#{format.getId()}") #{format.getDisplayName()}
42-
else
43-
option(value="#{format.getId()}", selected="selected") #{format.getDisplayName()}
44-
.col-sm-3
45-
span Status
46-
select.form-control(name="postStatus")
47-
for status in postStatus
48-
if status != postForm.getPostStatus()
49-
option(value="#{status.getId()}") #{status.getName()}
50-
else
51-
option(value="#{status.getId()}", selected="selected") #{status.getName()}
52-
.col-sm-3
53-
span Permalink
54-
input.form-control(name="permalink", value="#{postForm.getPermalink()}")
33+
.item-row
34+
hr
35+
.row
36+
.col-sm-2
37+
.form-group
38+
label Type
39+
select.form-control(name="postType")
40+
for item in postTypes
41+
if item != postForm.getPostType()
42+
option(value="#{item.getId()}") #{item.getName()}
43+
else
44+
option(value="#{item.getId()}", selected="selected") #{item.getName()}
45+
.col-sm-2
46+
.form-group
47+
label Format
48+
select.form-control(name="postFormat")
49+
for format in postFormats
50+
if format != postForm.getPostFormat()
51+
option(value="#{format.getId()}") #{format.getDisplayName()}
52+
else
53+
option(value="#{format.getId()}", selected="selected") #{format.getDisplayName()}
54+
.col-sm-2
55+
.form-group
56+
label Status
57+
select.form-control(name="postStatus")
58+
for status in postStatus
59+
if status != postForm.getPostStatus()
60+
option(value="#{status.getId()}") #{status.getName()}
61+
else
62+
option(value="#{status.getId()}", selected="selected") #{status.getName()}
5563

56-
.col-sm-3
57-
span Tags
58-
input.form-control(name="postTags", value="#{postForm.getPostTags()}")
59-
.item-row
60-
hr
61-
button.btn.btn-primary.btn-lg(type="submit") Save
62-
hr
64+
.col-sm-2
65+
.form-group
66+
label Permalink
67+
input.form-control(name="permalink", value="#{postForm.getPermalink()}")
68+
69+
.col-sm-3
70+
.form-group
71+
label Tags
72+
input.form-control(name="postTags", value="#{postForm.getPostTags()}")
73+
.item-row
74+
hr
75+
button.btn.btn-primary.btn-lg(type="submit") Save
76+
hr
6377

6478
script
65-
var editor = ace.edit("content-editor");
66-
var summaryEditor = ace.edit("summary-editor");
79+
var editor = ace.edit("content-editor");
80+
var summaryEditor = ace.edit("summary-editor");
6781

68-
editor.setTheme("ace/theme/github");
69-
summaryEditor.setTheme("ace/theme/github");
82+
editor.setTheme("ace/theme/github");
83+
summaryEditor.setTheme("ace/theme/github");
7084

71-
var MarkdownMode = ace.require("ace/mode/markdown").Mode;
72-
editor.getSession().setMode(new MarkdownMode());
73-
editor.getSession().setUseWrapMode(true);
85+
var MarkdownMode = ace.require("ace/mode/markdown").Mode;
86+
editor.getSession().setMode(new MarkdownMode());
87+
editor.getSession().setUseWrapMode(true);
7488

75-
summaryEditor.getSession().setMode(new MarkdownMode());
76-
summaryEditor.getSession().setUseWrapMode(true);
89+
summaryEditor.getSession().setMode(new MarkdownMode());
90+
summaryEditor.getSession().setUseWrapMode(true);
7791

78-
$("form").submit(function(){
79-
$("#content").val(editor.getValue());
80-
$("#summary").val(summaryEditor.getValue());
81-
return true;
82-
});
92+
$("form").submit(function(){
93+
$("#content").val(editor.getValue());
94+
$("#summary").val(summaryEditor.getValue());
95+
return true;
96+
});

0 commit comments

Comments
 (0)