11package com .raysmond .blog .services ;
22
33import com .raysmond .blog .Constants ;
4+ import com .raysmond .blog .error .NotFoundException ;
45import com .raysmond .blog .models .Post ;
56import com .raysmond .blog .models .support .PostFormat ;
67import com .raysmond .blog .models .support .PostStatus ;
78import com .raysmond .blog .models .support .PostType ;
89import com .raysmond .blog .repositories .PostRepository ;
9- import com .raysmond .blog .repositories .UserRepository ;
1010import com .raysmond .blog .utils .Markdown ;
1111import org .slf4j .Logger ;
1212import 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
0 commit comments