Skip to content

Commit 100a67a

Browse files
committed
Post permalink
1 parent 3e6e114 commit 100a67a

File tree

15 files changed

+123
-36
lines changed

15 files changed

+123
-36
lines changed

src/main/java/com/raysmond/blog/Constants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
*/
66
public final class Constants {
77

8+
public static final String ENV_PRODUCTION = "production";
9+
10+
public static final String ENV_DEVELOPMENT = "development";
11+
812
public static final String DEFAULT_ADMIN__EMAIL = "admin@admin.com";
913

1014
public static final String DEFAULT_ADMIN_PASSWORD = "admin";
1115

12-
public static final String ABOUT_PAGE_TITLE = "About";
16+
public static final String ABOUT_PAGE_PERMALINK = "About";
1317

1418
}

src/main/java/com/raysmond/blog/WebConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package com.raysmond.blog;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Function;
36
import com.raysmond.blog.support.web.ViewHelper;
7+
import de.neuland.jade4j.spring.view.JadeViewResolver;
48
import org.springframework.beans.factory.annotation.Autowired;
59
import org.springframework.context.annotation.Bean;
610
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.core.env.Environment;
712
import org.springframework.security.web.csrf.CsrfToken;
813
import org.springframework.web.servlet.HandlerInterceptor;
914
import org.springframework.web.servlet.ModelAndView;
1015
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
1116
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
1217
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
1318

19+
import javax.annotation.PostConstruct;
1420
import javax.servlet.http.HttpServletRequest;
1521
import javax.servlet.http.HttpServletResponse;
1622

23+
import static com.raysmond.blog.Constants.*;
24+
1725
/**
1826
* @author Raysmond<jiankunlei@gmail.com>.
1927
*/
@@ -22,12 +30,20 @@ public class WebConfig extends WebMvcConfigurerAdapter {
2230
@Autowired
2331
private ViewHelper viewHelper;
2432

33+
@Autowired
34+
private Environment env;
35+
2536
@Override
2637
public void addInterceptors(InterceptorRegistry registry) {
2738
registry.addInterceptor(viewObjectAddingInterceptor());
2839
super.addInterceptors(registry);
2940
}
3041

42+
@PostConstruct
43+
public void registerJadeViewHelpers(){
44+
viewHelper.setApplicationEnv(this.getApplicationEnv());
45+
}
46+
3147
@Bean
3248
public HandlerInterceptor viewObjectAddingInterceptor() {
3349
return new HandlerInterceptorAdapter() {
@@ -47,4 +63,8 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
4763
}
4864
};
4965
}
66+
67+
public String getApplicationEnv(){
68+
return this.env.acceptsProfiles(ENV_PRODUCTION) ? ENV_PRODUCTION : ENV_DEVELOPMENT;
69+
}
5070
}

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

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

3+
import com.raysmond.blog.Constants;
34
import com.raysmond.blog.models.Post;
45
import com.raysmond.blog.models.support.PostType;
56
import com.raysmond.blog.repositories.PostRepository;
@@ -20,11 +21,6 @@ public class HomeController {
2021
@Autowired
2122
private AppSetting appSetting;
2223

23-
@Autowired
24-
private PostRepository postRepository;
25-
26-
private static Long ABOUT_PAGE_ID = null;
27-
2824
@RequestMapping("")
2925
public String index(@RequestParam(defaultValue = "1") int page, Model model) {
3026
Page<Post> posts = postService.getAllPublishedPostsByPage(page < 0 ? 1 : page - 1, appSetting.getPageSize());
@@ -38,16 +34,13 @@ public String index(@RequestParam(defaultValue = "1") int page, Model model) {
3834

3935
@RequestMapping("about")
4036
public String about(Model model) {
41-
if (ABOUT_PAGE_ID == null || postService.getPost(ABOUT_PAGE_ID) == null) {
42-
Post post = postRepository.findByTitleAndPostType("About", PostType.PAGE);
43-
if (post == null) {
44-
post = postService.createAboutPage();
45-
}
37+
Post post = postService.getPublishedPostByPermalink(Constants.ABOUT_PAGE_PERMALINK);
4638

47-
ABOUT_PAGE_ID = post.getId();
39+
if (post == null) {
40+
post = postService.createAboutPage();
4841
}
4942

50-
model.addAttribute("about", postService.getPost(ABOUT_PAGE_ID));
43+
model.addAttribute("about", post);
5144
return "home/about";
5245
}
5346

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.raysmond.blog.error.NotFoundException;
44
import com.raysmond.blog.models.Post;
5+
import com.raysmond.blog.models.User;
56
import com.raysmond.blog.services.PostService;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.stereotype.Controller;
89
import org.springframework.ui.Model;
910
import org.springframework.web.bind.annotation.PathVariable;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112

13+
import static org.springframework.web.bind.annotation.RequestMethod.*;
14+
1215

1316
/**
1417
* @author Raysmond<jiankunlei@gmail.com>
@@ -19,22 +22,29 @@ public class PostController {
1922
@Autowired
2023
private PostService postService;
2124

22-
@RequestMapping(value = "archive")
25+
@RequestMapping(value = "archive", method = GET)
2326
public String archive(Model model){
2427
model.addAttribute("posts", postService.getArchivePosts());
2528

2629
return "posts/archive";
2730
}
2831

29-
@RequestMapping(value = "{postId:[0-9]+}")
30-
public String show(@PathVariable Long postId, Model model){
31-
Post post = postService.getPost(postId);
32+
@RequestMapping(value = "{permalink}", method = GET)
33+
public String show(@PathVariable String permalink, Model model){
34+
Post post = null;
3235

33-
if(post == null){
34-
throw new NotFoundException("Post @" + postId + " is not found.");
36+
try{
37+
post = postService.getPublishedPostByPermalink(permalink);
38+
} catch (NotFoundException ex){
39+
if (permalink.matches("\\d+"))
40+
post = postService.getPost(Long.valueOf(permalink));
3541
}
3642

43+
if (post == null)
44+
throw new NotFoundException("Post with permalink " + permalink + " is not found");
45+
3746
model.addAttribute("post", post);
3847
return "posts/show";
3948
}
49+
4050
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ public class PostForm {
2222

2323
@NotNull
2424
private PostStatus postStatus;
25+
26+
@NotNull
27+
private String permalink;
2528
}

src/main/java/com/raysmond/blog/models/Post.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.annotations.Type;
1010

1111
import javax.persistence.*;
12+
import java.text.SimpleDateFormat;
1213

1314
/**
1415
* @author Raysmond<jiankunlei@gmail.com>
@@ -17,6 +18,8 @@
1718
@Table(name = "posts")
1819
@Getter @Setter
1920
public class Post extends BaseModel{
21+
private static final SimpleDateFormat SLUG_DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
22+
2023
@ManyToOne
2124
private User user;
2225

@@ -41,6 +44,8 @@ public class Post extends BaseModel{
4144
@Enumerated(EnumType.STRING)
4245
private PostType postType = PostType.POST;
4346

47+
private String permalink;
48+
4449
public String getRenderedContent() {
4550
if (this.postFormat == PostFormat.MARKDOWN)
4651
return renderedContent;

src/main/java/com/raysmond/blog/repositories/PostRepository.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818
@Transactional
1919
public interface PostRepository extends JpaRepository<Post, Long> {
2020
Post findByTitle(String title);
21+
2122
Post findByTitleAndPostType(String title, PostType postType);
23+
24+
Post findByPostStatus(PostStatus postStatus);
25+
26+
Post findByPermalinkAndPostStatus(String permalink, PostStatus postStatus);
27+
2228
Page<Post> findAllByPostType(PostType postType, Pageable pageRequest);
29+
2330
Page<Post> findAllByPostTypeAndPostStatus(PostType postType, PostStatus postStatus, Pageable pageRequest);
2431
}
2532

src/main/java/com/raysmond/blog/services/PostService.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.raysmond.blog.services;
22

33
import com.raysmond.blog.Constants;
4+
import com.raysmond.blog.error.NotFoundException;
45
import com.raysmond.blog.models.Post;
56
import com.raysmond.blog.models.support.PostFormat;
67
import com.raysmond.blog.models.support.PostStatus;
78
import com.raysmond.blog.models.support.PostType;
89
import com.raysmond.blog.repositories.PostRepository;
9-
import com.raysmond.blog.repositories.UserRepository;
1010
import com.raysmond.blog.utils.Markdown;
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
@@ -41,9 +41,27 @@ public class PostService {
4141

4242
@Cacheable(CACHE_NAME)
4343
public Post getPost(Long postId) {
44-
logger.info("Get post " + postId + " from database");
44+
logger.debug("Get post " + postId);
4545

46-
return postRepository.findOne(postId);
46+
Post post = postRepository.findOne(postId);
47+
48+
if (post == null){
49+
throw new NotFoundException("Post with id " + postId + " is not found.");
50+
}
51+
52+
return post;
53+
}
54+
55+
@Cacheable(CACHE_NAME)
56+
public Post getPublishedPostByPermalink(String permalink){
57+
logger.debug("Get post with permalink " + permalink);
58+
Post post = postRepository.findByPermalinkAndPostStatus(permalink, PostStatus.PUBLISHED);
59+
60+
if (post == null){
61+
throw new NotFoundException("Post with permalink '" + permalink + "' is not found.");
62+
}
63+
64+
return post;
4765
}
4866

4967
@Caching(evict = {
@@ -60,6 +78,7 @@ public Post createPost(Post post) {
6078

6179
@Caching(evict = {
6280
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
81+
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
6382
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
6483
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
6584
})
@@ -73,6 +92,7 @@ public Post updatePost(Post post) {
7392

7493
@Caching(evict = {
7594
@CacheEvict(value = CACHE_NAME, key = "#post.id"),
95+
@CacheEvict(value = CACHE_NAME, key = "#post.permalink", condition = "#post.permalink != null"),
7696
@CacheEvict(value = CACHE_NAME_ARCHIVE, allEntries = true),
7797
@CacheEvict(value = CACHE_NAME_PAGE, allEntries = true)
7898
})
@@ -82,7 +102,7 @@ public void deletePost(Post post) {
82102

83103
@Cacheable(value = CACHE_NAME_ARCHIVE, key = "#root.method.name")
84104
public List<Post> getArchivePosts() {
85-
logger.info("Get all archive posts from database.");
105+
logger.debug("Get all archive posts from database.");
86106

87107
Iterable<Post> archivePosts = postRepository.findAllByPostTypeAndPostStatus(
88108
PostType.POST,
@@ -103,7 +123,7 @@ public List<Post> getArchivePosts() {
103123

104124
@Cacheable(value = CACHE_NAME_PAGE, key = "T(java.lang.String).valueOf(#page).concat('-').concat(#pageSize)")
105125
public Page<Post> getAllPublishedPostsByPage(int page, int pageSize) {
106-
logger.info("Get posts by page " + page + " from database");
126+
logger.debug("Get posts by page " + page);
107127

108128
return postRepository.findAllByPostTypeAndPostStatus(
109129
PostType.POST,
@@ -112,11 +132,12 @@ public Page<Post> getAllPublishedPostsByPage(int page, int pageSize) {
112132
}
113133

114134
public Post createAboutPage() {
115-
logger.info("Create default about page");
135+
logger.debug("Create default about page");
116136

117137
Post post = new Post();
118-
post.setTitle(Constants.ABOUT_PAGE_TITLE);
119-
post.setContent(Constants.ABOUT_PAGE_TITLE.toLowerCase());
138+
post.setTitle(Constants.ABOUT_PAGE_PERMALINK);
139+
post.setContent(Constants.ABOUT_PAGE_PERMALINK.toLowerCase());
140+
post.setPermalink(Constants.ABOUT_PAGE_PERMALINK);
120141
post.setUser(userService.getSuperUser());
121142
post.setPostFormat(PostFormat.MARKDOWN);
122143

src/main/java/com/raysmond/blog/support/web/ViewHelper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class ViewHelper {
2020

2121
private AppSetting appSetting;
2222

23+
private String applicationEnv;
24+
2325
@Autowired
2426
public ViewHelper(AppSetting appSetting){
2527
this.appSetting = appSetting;
@@ -50,4 +52,12 @@ public String getMonthAndDay(Date date){
5052
public String metaTitle(String title){
5153
return title + " · " + appSetting.getSiteName();
5254
}
55+
56+
public String getApplicationEnv() {
57+
return applicationEnv;
58+
}
59+
60+
public void setApplicationEnv(String applicationEnv) {
61+
this.applicationEnv = applicationEnv;
62+
}
5363
}

src/main/resources/application.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ spring:
33
active: dev
44

55
thymeleaf:
6-
cache: true
6+
cache: false
77

88
jade4j:
9-
caching: true
9+
caching: false
1010

1111
dataSource:
1212
driverClassName: com.mysql.jdbc.Driver
@@ -17,7 +17,7 @@ spring:
1717
hibernate:
1818
dialect: org.hibernate.dialect.MySQLDialect
1919
hbm2ddl.auto: update
20-
show_sql: true
20+
show_sql: false
2121

2222
redis:
2323
host: localhost

0 commit comments

Comments
 (0)