Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IEQEFCR
halo
提交
4a46132f
H
halo
项目概览
IEQEFCR
/
halo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
halo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4a46132f
编写于
4月 24, 2019
作者:
J
johnniang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor post service
上级
f44ffe50
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
514 addition
and
410 deletion
+514
-410
src/main/java/run/halo/app/event/post/AbstractVisitEventListener.java
...a/run/halo/app/event/post/AbstractVisitEventListener.java
+58
-0
src/main/java/run/halo/app/service/PostService.java
src/main/java/run/halo/app/service/PostService.java
+3
-144
src/main/java/run/halo/app/service/SheetService.java
src/main/java/run/halo/app/service/SheetService.java
+4
-22
src/main/java/run/halo/app/service/base/BasePostService.java
src/main/java/run/halo/app/service/base/BasePostService.java
+191
-0
src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java
...ava/run/halo/app/service/impl/BaseCommentServiceImpl.java
+3
-1
src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java
...n/java/run/halo/app/service/impl/BasePostServiceImpl.java
+246
-0
src/main/java/run/halo/app/service/impl/CommentServiceImpl.java
...in/java/run/halo/app/service/impl/CommentServiceImpl.java
+0
-1
src/main/java/run/halo/app/service/impl/PostServiceImpl.java
src/main/java/run/halo/app/service/impl/PostServiceImpl.java
+7
-179
src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java
...va/run/halo/app/service/impl/SheetCommentServiceImpl.java
+0
-1
src/main/java/run/halo/app/service/impl/SheetServiceImpl.java
...main/java/run/halo/app/service/impl/SheetServiceImpl.java
+2
-62
未找到文件。
src/main/java/run/halo/app/event/post/AbstractVisitEventListener.java
0 → 100644
浏览文件 @
4a46132f
package
run.halo.app.event.post
;
import
lombok.extern.slf4j.Slf4j
;
/**
* Abstract visit event listener.
*
* @author johnniang
* @date 19-4-24
*/
@Slf4j
public
abstract
class
AbstractVisitEventListener
{
// private final Map<Integer, BlockingQueue<Integer>> postVisitQueueMap;
//
// private final Map<Integer, PostVisitEventListener.PostVisitTask> postVisitTaskMap;
//
// protected final BasePostRepository basePostRepository;
//
// protected AbstractVisitEventListener(BasePostRepository basePostRepository) {
// this.basePostRepository = basePostRepository;
// }
//
//
// /**
// * Post visit task.
// */
// private class PostVisitTask implements Runnable {
//
// private final Integer postId;
//
// private PostVisitTask(Integer postId) {
// this.postId = postId;
// }
//
// @Override
// public void run() {
// while (!Thread.currentThread().isInterrupted()) {
// try {
// BlockingQueue<Integer> postVisitQueue = postVisitQueueMap.get(postId);
// Integer postId = postVisitQueue.take();
//
// log.debug("Took a new visit for post id: [{}]", postId);
//
// // Increase the visit
// postService.increaseVisit(postId);
//
// log.debug("Increased visits for post id: [{}]", postId);
// } catch (InterruptedException e) {
// log.debug("Post visit task: " + Thread.currentThread().getName() + " was interrupted", e);
// // Ignore this exception
// }
// }
//
// log.debug("Thread: [{}] has been interrupted", Thread.currentThread().getName());
// }
// }
}
src/main/java/run/halo/app/service/PostService.java
浏览文件 @
4a46132f
...
...
@@ -13,11 +13,9 @@ import run.halo.app.model.vo.ArchiveMonthVO;
import
run.halo.app.model.vo.ArchiveYearVO
;
import
run.halo.app.model.vo.PostDetailVO
;
import
run.halo.app.model.vo.PostListVO
;
import
run.halo.app.service.base.
Crud
Service
;
import
run.halo.app.service.base.
BasePost
Service
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Set
;
/**
...
...
@@ -26,7 +24,7 @@ import java.util.Set;
* @author johnniang
* @author RYAN0UP
*/
public
interface
PostService
extends
CrudService
<
Post
,
Integer
>
{
public
interface
PostService
extends
BasePostService
<
Post
>
{
/**
* Lists latest posts of minimal.
...
...
@@ -47,26 +45,6 @@ public interface PostService extends CrudService<Post, Integer> {
@NonNull
Page
<
PostSimpleDTO
>
pageLatestOfSimple
(
int
top
);
/**
* Lists latest posts.
*
* @param top top number must not be less than 0
* @return latest posts
*/
@NonNull
Page
<
Post
>
pageLatest
(
int
top
);
/**
* Lists by status.
*
* @param status post status must not be null
* @param pageable page info must not be null
* @return a page of post
*/
@NonNull
Page
<
Post
>
pageBy
(
@NonNull
PostStatus
status
,
@NonNull
Pageable
pageable
);
/**
* Pages posts.
*
...
...
@@ -107,14 +85,6 @@ public interface PostService extends CrudService<Post, Integer> {
@NonNull
Page
<
PostListVO
>
pageListVoBy
(
@NonNull
PostStatus
status
,
@NonNull
Pageable
pageable
);
/**
* Count posts by status.
*
* @param status status
* @return posts count
*/
Long
countByStatus
(
PostStatus
status
);
/**
* Creates post by post param.
*
...
...
@@ -139,15 +109,6 @@ public interface PostService extends CrudService<Post, Integer> {
@Transactional
PostDetailVO
updateBy
(
@NonNull
Post
postToUpdate
,
Set
<
Integer
>
tagIds
,
Set
<
Integer
>
categoryIds
);
/**
* Get post by url.
*
* @param url post url.
* @return Post
*/
@NonNull
Post
getByUrl
(
@NonNull
String
url
);
/**
* Gets post by post status and url.
*
...
...
@@ -156,6 +117,7 @@ public interface PostService extends CrudService<Post, Integer> {
* @return post info
*/
@NonNull
@Override
Post
getBy
(
@NonNull
PostStatus
status
,
@NonNull
String
url
);
/**
...
...
@@ -167,54 +129,6 @@ public interface PostService extends CrudService<Post, Integer> {
@NonNull
PostDetailVO
getDetailVoBy
(
@NonNull
Integer
postId
);
/**
* Counts visit total number.
*
* @return visit total number
*/
long
countVisit
();
/**
* Counts like total number.
*
* @return like total number
*/
long
countLike
();
/**
* Increases post visits.
*
* @param visits visits must not be less than 1
* @param postId post id must not be null
*/
@Transactional
void
increaseVisit
(
long
visits
,
@NonNull
Integer
postId
);
/**
* Increase post likes.
*
* @param likes likes must not be less than 1
* @param postId post id must not be null
*/
@Transactional
void
increaseLike
(
long
likes
,
@NonNull
Integer
postId
);
/**
* Increases post visits (1).
*
* @param postId post id must not be null
*/
@Transactional
void
increaseVisit
(
@NonNull
Integer
postId
);
/**
* Increase post likes(1).
*
* @param postId post id must not be null
*/
@Transactional
void
increaseLike
(
@NonNull
Integer
postId
);
/**
* Lists year archives.
*
...
...
@@ -249,59 +163,4 @@ public interface PostService extends CrudService<Post, Integer> {
@NonNull
Page
<
PostListVO
>
convertToListVo
(
@NonNull
Page
<
Post
>
postPage
);
/**
* Lists all posts by post status.
*
* @param status post status must not be null
* @return a list of post
*/
@NonNull
List
<
Post
>
listAllBy
(
@NonNull
PostStatus
status
);
/**
* Filters post content if the password is not blank.
*
* @param post original post must not be null
* @return filtered post
*/
@NonNull
Post
filterIfEncrypt
(
@NonNull
Post
post
);
/**
* Gets previous post.
*
* @param date date must not be null
* @return an optional post
*/
@NonNull
Optional
<
Post
>
getPrePost
(
@NonNull
Date
date
);
/**
* Gets next post.
*
* @param date date must not be null
* @return an optional post
*/
@NonNull
Optional
<
Post
>
getNextPost
(
@NonNull
Date
date
);
/**
* Lists previous posts.
*
* @param date date must not be null
* @param size previous max post size
* @return a list of previous post
*/
@NonNull
List
<
Post
>
listPrePosts
(
@NonNull
Date
date
,
int
size
);
/**
* Lits next posts.
*
* @param date date must not be null
* @param size next max post size
* @return a list of next post
*/
@NonNull
List
<
Post
>
listNextPosts
(
@NonNull
Date
date
,
int
size
);
}
src/main/java/run/halo/app/service/SheetService.java
浏览文件 @
4a46132f
package
run.halo.app.service
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.lang.NonNull
;
import
run.halo.app.model.dto.post.SheetDetailDTO
;
import
run.halo.app.model.dto.post.SheetListDTO
;
import
run.halo.app.model.entity.Sheet
;
import
run.halo.app.model.enums.PostStatus
;
import
run.halo.app.service.base.
Crud
Service
;
import
run.halo.app.service.base.
BasePost
Service
;
/**
* Sheet service interface.
...
...
@@ -15,7 +14,7 @@ import run.halo.app.service.base.CrudService;
* @author johnniang
* @date 19-4-24
*/
public
interface
SheetService
extends
CrudService
<
Sheet
,
Integer
>
{
public
interface
SheetService
extends
BasePostService
<
Sheet
>
{
/**
* Creates a sheet.
...
...
@@ -35,25 +34,8 @@ public interface SheetService extends CrudService<Sheet, Integer> {
@NonNull
Sheet
updateBy
(
@NonNull
Sheet
sheet
);
/**
* Gets a page of sheet.
*
* @param pageable page info must not be null
* @return a page of sheet
*/
@NonNull
Page
<
Sheet
>
pageBy
(
@NonNull
Pageable
pageable
);
/**
* Gets sheet by post status and url.
*
* @param status post status must not be null
* @param url sheet url must not be blank
* @return sheet info
*/
@NonNull
Sheet
getBy
(
@NonNull
PostStatus
status
,
@NonNull
String
url
);
@Override
Sheet
getBy
(
PostStatus
status
,
String
url
);
/**
* Converts to detail dto.
...
...
src/main/java/run/halo/app/service/base/BasePostService.java
0 → 100644
浏览文件 @
4a46132f
package
run.halo.app.service.base
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.lang.NonNull
;
import
org.springframework.transaction.annotation.Transactional
;
import
run.halo.app.model.entity.BasePost
;
import
run.halo.app.model.enums.PostStatus
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
/**
* Base post service implementation.
*
* @author johnniang
* @date 19-4-24
*/
public
interface
BasePostService
<
POST
extends
BasePost
>
extends
CrudService
<
POST
,
Integer
>
{
/**
* Counts visit total number.
*
* @return visit total number
*/
long
countVisit
();
/**
* Counts like total number.
*
* @return like total number
*/
long
countLike
();
/**
* Count posts by status.
*
* @param status status
* @return posts count
*/
long
countByStatus
(
PostStatus
status
);
/**
* Get post by url.
*
* @param url post url.
* @return Post
*/
@NonNull
POST
getByUrl
(
@NonNull
String
url
);
/**
* Gets post by post status and url.
*
* @param status post status must not be null
* @param url post url must not be blank
* @return post info
*/
@NonNull
POST
getBy
(
@NonNull
PostStatus
status
,
@NonNull
String
url
);
/**
* Lists all posts by post status.
*
* @param status post status must not be null
* @return a list of post
*/
@NonNull
List
<
POST
>
listAllBy
(
@NonNull
PostStatus
status
);
/**
* Lists previous posts.
*
* @param date date must not be null
* @param size previous max post size
* @return a list of previous post
*/
@NonNull
List
<
POST
>
listPrePosts
(
@NonNull
Date
date
,
int
size
);
/**
* Lits next posts.
*
* @param date date must not be null
* @param size next max post size
* @return a list of next post
*/
@NonNull
List
<
POST
>
listNextPosts
(
@NonNull
Date
date
,
int
size
);
/**
* Gets previous post.
*
* @param date date must not be null
* @return an optional post
*/
@NonNull
Optional
<
POST
>
getPrePost
(
@NonNull
Date
date
);
/**
* Gets next post.
*
* @param date date must not be null
* @return an optional post
*/
@NonNull
Optional
<
POST
>
getNextPost
(
@NonNull
Date
date
);
/**
* Lists latest posts.
*
* @param top top number must not be less than 0
* @return latest posts
*/
@NonNull
Page
<
POST
>
pageLatest
(
int
top
);
/**
* Gets a page of sheet.
*
* @param pageable page info must not be null
* @return a page of sheet
*/
@NonNull
Page
<
POST
>
pageBy
(
@NonNull
Pageable
pageable
);
/**
* Lists by status.
*
* @param status post status must not be null
* @param pageable page info must not be null
* @return a page of post
*/
@NonNull
Page
<
POST
>
pageBy
(
@NonNull
PostStatus
status
,
@NonNull
Pageable
pageable
);
/**
* Increases post visits.
*
* @param visits visits must not be less than 1
* @param postId post id must not be null
*/
@Transactional
void
increaseVisit
(
long
visits
,
@NonNull
Integer
postId
);
/**
* Increase post likes.
*
* @param likes likes must not be less than 1
* @param postId post id must not be null
*/
@Transactional
void
increaseLike
(
long
likes
,
@NonNull
Integer
postId
);
/**
* Increases post visits (1).
*
* @param postId post id must not be null
*/
@Transactional
void
increaseVisit
(
@NonNull
Integer
postId
);
/**
* Increases post likes(1).
*
* @param postId post id must not be null
*/
@Transactional
void
increaseLike
(
@NonNull
Integer
postId
);
/**
* Creates or updates by post.
*
* @param post post must not be null
* @return created or updated post
*/
@NonNull
POST
createOrUpdateBy
(
@NonNull
POST
post
);
/**
* Filters post content if the password is not blank.
*
* @param post original post must not be null
* @return filtered post
*/
@NonNull
POST
filterIfEncrypt
(
@NonNull
POST
post
);
}
src/main/java/run/halo/app/service/
base
/BaseCommentServiceImpl.java
→
src/main/java/run/halo/app/service/
impl
/BaseCommentServiceImpl.java
浏览文件 @
4a46132f
package
run.halo.app.service.
base
;
package
run.halo.app.service.
impl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.codec.digest.DigestUtils
;
...
...
@@ -31,6 +31,8 @@ import run.halo.app.repository.base.BaseCommentRepository;
import
run.halo.app.security.authentication.Authentication
;
import
run.halo.app.security.context.SecurityContextHolder
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.base.AbstractCrudService
;
import
run.halo.app.service.base.BaseCommentService
;
import
run.halo.app.utils.ServiceUtils
;
import
run.halo.app.utils.ServletUtils
;
...
...
src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java
0 → 100644
浏览文件 @
4a46132f
package
run.halo.app.service.impl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.lang.NonNull
;
import
org.springframework.util.Assert
;
import
org.springframework.util.CollectionUtils
;
import
run.halo.app.exception.AlreadyExistsException
;
import
run.halo.app.exception.BadRequestException
;
import
run.halo.app.exception.NotFoundException
;
import
run.halo.app.model.entity.BasePost
;
import
run.halo.app.model.enums.PostStatus
;
import
run.halo.app.repository.base.BasePostRepository
;
import
run.halo.app.service.base.AbstractCrudService
;
import
run.halo.app.service.base.BasePostService
;
import
run.halo.app.utils.DateUtils
;
import
run.halo.app.utils.MarkdownUtils
;
import
run.halo.app.utils.ServiceUtils
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Optional
;
import
static
org
.
springframework
.
data
.
domain
.
Sort
.
Direction
.
ASC
;
import
static
org
.
springframework
.
data
.
domain
.
Sort
.
Direction
.
DESC
;
/**
* Base post service implementation.
*
* @author johnniang
* @date 19-4-24
*/
@Slf4j
public
abstract
class
BasePostServiceImpl
<
POST
extends
BasePost
>
extends
AbstractCrudService
<
POST
,
Integer
>
implements
BasePostService
<
POST
>
{
private
final
BasePostRepository
<
POST
>
basePostRepository
;
public
BasePostServiceImpl
(
BasePostRepository
<
POST
>
basePostRepository
)
{
super
(
basePostRepository
);
this
.
basePostRepository
=
basePostRepository
;
}
@Override
public
long
countVisit
()
{
return
Optional
.
ofNullable
(
basePostRepository
.
countVisit
()).
orElse
(
0L
);
}
@Override
public
long
countLike
()
{
return
Optional
.
ofNullable
(
basePostRepository
.
countLike
()).
orElse
(
0L
);
}
@Override
public
long
countByStatus
(
PostStatus
status
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
return
basePostRepository
.
countByStatus
(
status
);
}
@Override
public
POST
getByUrl
(
String
url
)
{
Assert
.
hasText
(
url
,
"Url must not be blank"
);
return
basePostRepository
.
getByUrl
(
url
).
orElseThrow
(()
->
new
NotFoundException
(
"The post does not exist"
).
setErrorData
(
url
));
}
@Override
public
POST
getBy
(
PostStatus
status
,
String
url
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
Assert
.
hasText
(
url
,
"Post url must not be blank"
);
Optional
<
POST
>
postOptional
=
basePostRepository
.
getByUrlAndStatus
(
url
,
status
);
return
postOptional
.
orElseThrow
(()
->
new
NotFoundException
(
"The post with status "
+
status
+
" and url "
+
url
+
"was not existed"
).
setErrorData
(
url
));
}
@Override
public
List
<
POST
>
listAllBy
(
PostStatus
status
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
return
basePostRepository
.
findAllByStatus
(
status
);
}
@Override
public
List
<
POST
>
listPrePosts
(
Date
date
,
int
size
)
{
Assert
.
notNull
(
date
,
"Date must not be null"
);
return
basePostRepository
.
findAllByStatusAndCreateTimeAfter
(
PostStatus
.
PUBLISHED
,
date
,
PageRequest
.
of
(
0
,
size
,
Sort
.
by
(
ASC
,
"createTime"
)))
.
getContent
();
}
@Override
public
List
<
POST
>
listNextPosts
(
Date
date
,
int
size
)
{
Assert
.
notNull
(
date
,
"Date must not be null"
);
return
basePostRepository
.
findAllByStatusAndCreateTimeBefore
(
PostStatus
.
PUBLISHED
,
date
,
PageRequest
.
of
(
0
,
size
,
Sort
.
by
(
DESC
,
"createTime"
)))
.
getContent
();
}
@Override
public
Optional
<
POST
>
getPrePost
(
Date
date
)
{
List
<
POST
>
posts
=
listPrePosts
(
date
,
1
);
return
CollectionUtils
.
isEmpty
(
posts
)
?
Optional
.
empty
()
:
Optional
.
of
(
posts
.
get
(
0
));
}
@Override
public
Optional
<
POST
>
getNextPost
(
Date
date
)
{
List
<
POST
>
posts
=
listNextPosts
(
date
,
1
);
return
CollectionUtils
.
isEmpty
(
posts
)
?
Optional
.
empty
()
:
Optional
.
of
(
posts
.
get
(
0
));
}
@Override
public
Page
<
POST
>
pageLatest
(
int
top
)
{
Assert
.
isTrue
(
top
>
0
,
"Top number must not be less than 0"
);
PageRequest
latestPageable
=
PageRequest
.
of
(
0
,
top
,
Sort
.
by
(
DESC
,
"editTime"
));
return
listAll
(
latestPageable
);
}
@Override
public
Page
<
POST
>
pageBy
(
Pageable
pageable
)
{
Assert
.
notNull
(
pageable
,
"Page info must not be null"
);
return
listAll
(
pageable
);
}
@Override
public
Page
<
POST
>
pageBy
(
PostStatus
status
,
Pageable
pageable
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
Assert
.
notNull
(
pageable
,
"Page info must not be null"
);
return
basePostRepository
.
findAllByStatus
(
status
,
pageable
);
}
@Override
public
void
increaseVisit
(
long
visits
,
Integer
postId
)
{
Assert
.
isTrue
(
visits
>
0
,
"Visits to increase must not be less than 1"
);
Assert
.
notNull
(
postId
,
"Goods id must not be null"
);
long
affectedRows
=
basePostRepository
.
updateVisit
(
visits
,
postId
);
if
(
affectedRows
!=
1
)
{
log
.
error
(
"Post with id: [{}] may not be found"
,
postId
);
throw
new
BadRequestException
(
"Failed to increase visits "
+
visits
+
" for post with id "
+
postId
);
}
}
@Override
public
void
increaseLike
(
long
likes
,
Integer
postId
)
{
Assert
.
isTrue
(
likes
>
0
,
"Likes to increase must not be less than 1"
);
Assert
.
notNull
(
postId
,
"Goods id must not be null"
);
long
affectedRows
=
basePostRepository
.
updateLikes
(
likes
,
postId
);
if
(
affectedRows
!=
1
)
{
log
.
error
(
"Post with id: [{}] may not be found"
,
postId
);
throw
new
BadRequestException
(
"Failed to increase likes "
+
likes
+
" for post with id "
+
postId
);
}
}
@Override
public
void
increaseVisit
(
Integer
postId
)
{
increaseVisit
(
1L
,
postId
);
}
@Override
public
void
increaseLike
(
Integer
postId
)
{
increaseLike
(
1L
,
postId
);
}
@Override
public
POST
createOrUpdateBy
(
POST
post
)
{
Assert
.
notNull
(
post
,
"Post must not be null"
);
// Check url
urlMustNotExist
(
post
);
// Render content
post
.
setFormatContent
(
MarkdownUtils
.
renderMarkdown
(
post
.
getOriginalContent
()));
// Create or update post
if
(
ServiceUtils
.
isEmptyId
(
post
.
getId
()))
{
// The sheet will be created
return
create
(
post
);
}
// The sheet will be updated
// Set edit time
post
.
setEditTime
(
DateUtils
.
now
());
// Update it
return
update
(
post
);
}
@Override
public
POST
filterIfEncrypt
(
POST
post
)
{
Assert
.
notNull
(
post
,
"Post must not be null"
);
if
(
StringUtils
.
isNotBlank
(
post
.
getPassword
()))
{
String
tip
=
"The post is encrypted by author"
;
post
.
setSummary
(
tip
);
post
.
setOriginalContent
(
tip
);
post
.
setFormatContent
(
tip
);
}
return
post
;
}
/**
* Check if the url is exist.
*
* @param post post must not be null
*/
protected
void
urlMustNotExist
(
@NonNull
POST
post
)
{
Assert
.
notNull
(
post
,
"Sheet must not be null"
);
// TODO Refactor this method with BasePostService
// TODO May refactor these queries
// Get url count
long
count
;
if
(
ServiceUtils
.
isEmptyId
(
post
.
getId
()))
{
// The sheet will be created
count
=
basePostRepository
.
countByUrl
(
post
.
getUrl
());
}
else
{
// The sheet will be updated
count
=
basePostRepository
.
countByIdNotAndUrl
(
post
.
getId
(),
post
.
getUrl
());
}
if
(
count
>
0
)
{
throw
new
AlreadyExistsException
(
"The sheet url has been exist"
);
}
}
}
src/main/java/run/halo/app/service/impl/CommentServiceImpl.java
浏览文件 @
4a46132f
...
...
@@ -21,7 +21,6 @@ import run.halo.app.security.authentication.Authentication;
import
run.halo.app.security.context.SecurityContextHolder
;
import
run.halo.app.service.CommentService
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.base.BaseCommentServiceImpl
;
import
run.halo.app.utils.ServiceUtils
;
import
run.halo.app.utils.ValidationUtils
;
...
...
src/main/java/run/halo/app/service/impl/PostServiceImpl.java
浏览文件 @
4a46132f
...
...
@@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.data.jpa.domain.Specification
;
...
...
@@ -15,9 +14,6 @@ import org.springframework.util.Assert;
import
org.springframework.util.CollectionUtils
;
import
run.halo.app.event.logger.LogEvent
;
import
run.halo.app.event.post.PostVisitEvent
;
import
run.halo.app.exception.AlreadyExistsException
;
import
run.halo.app.exception.BadRequestException
;
import
run.halo.app.exception.NotFoundException
;
import
run.halo.app.model.dto.CategoryDTO
;
import
run.halo.app.model.dto.TagDTO
;
import
run.halo.app.model.dto.post.PostMinimalDTO
;
...
...
@@ -32,20 +28,16 @@ import run.halo.app.model.vo.PostDetailVO;
import
run.halo.app.model.vo.PostListVO
;
import
run.halo.app.repository.PostRepository
;
import
run.halo.app.service.*
;
import
run.halo.app.service.base.AbstractCrudService
;
import
run.halo.app.utils.DateUtils
;
import
run.halo.app.utils.MarkdownUtils
;
import
run.halo.app.utils.ServiceUtils
;
import
javax.persistence.criteria.Predicate
;
import
javax.persistence.criteria.Root
;
import
javax.persistence.criteria.Subquery
;
import
java.util.*
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
import
java.util.stream.Collectors
;
import
static
org
.
springframework
.
data
.
domain
.
Sort
.
Direction
.
ASC
;
import
static
org
.
springframework
.
data
.
domain
.
Sort
.
Direction
.
DESC
;
/**
...
...
@@ -56,7 +48,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
*/
@Slf4j
@Service
public
class
PostServiceImpl
extends
AbstractCrudService
<
Post
,
Integer
>
implements
PostService
{
public
class
PostServiceImpl
extends
BasePostServiceImpl
<
Post
>
implements
PostService
{
private
final
PostRepository
postRepository
;
...
...
@@ -99,23 +91,6 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return
pageLatest
(
top
).
map
(
post
->
new
PostSimpleDTO
().
convertFrom
(
post
));
}
@Override
public
Page
<
Post
>
pageLatest
(
int
top
)
{
Assert
.
isTrue
(
top
>
0
,
"Top number must not be less than 0"
);
PageRequest
latestPageable
=
PageRequest
.
of
(
0
,
top
,
Sort
.
by
(
DESC
,
"editTime"
));
return
listAll
(
latestPageable
);
}
@Override
public
Page
<
Post
>
pageBy
(
PostStatus
status
,
Pageable
pageable
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
Assert
.
notNull
(
pageable
,
"Page info must not be null"
);
return
postRepository
.
findAllByStatus
(
status
,
pageable
);
}
@Override
public
Page
<
Post
>
pageBy
(
PostQuery
postQuery
,
Pageable
pageable
)
{
Assert
.
notNull
(
postQuery
,
"Post query must not be null"
);
...
...
@@ -198,20 +173,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return
convertToListVo
(
postPage
);
}
/**
* Counts posts by status.
*
* @param status status
* @return posts count
*/
@Override
public
Long
countByStatus
(
PostStatus
status
)
{
return
postRepository
.
countByStatus
(
status
);
}
@Override
public
PostDetailVO
createBy
(
Post
postToCreate
,
Set
<
Integer
>
tagIds
,
Set
<
Integer
>
categoryIds
)
{
return
createOrUpdate
(
postToCreate
,
tagIds
,
categoryIds
,
this
::
create
);
return
createOrUpdate
(
postToCreate
,
tagIds
,
categoryIds
);
}
@Override
...
...
@@ -219,7 +183,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
// Set edit time
postToUpdate
.
setEditTime
(
DateUtils
.
now
());
return
createOrUpdate
(
postToUpdate
,
tagIds
,
categoryIds
,
this
::
update
);
return
createOrUpdate
(
postToUpdate
,
tagIds
,
categoryIds
);
}
@Override
...
...
@@ -244,30 +208,11 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return
updatedPost
;
}
private
PostDetailVO
createOrUpdate
(
@NonNull
Post
post
,
Set
<
Integer
>
tagIds
,
Set
<
Integer
>
categoryIds
,
@NonNull
Function
<
Post
,
Post
>
postOperation
)
{
private
PostDetailVO
createOrUpdate
(
@NonNull
Post
post
,
Set
<
Integer
>
tagIds
,
Set
<
Integer
>
categoryIds
)
{
Assert
.
notNull
(
post
,
"Post param must not be null"
);
Assert
.
notNull
(
postOperation
,
"Post operation must not be null"
);
// Check url
long
count
;
boolean
isUpdating
=
post
.
getId
()
!=
null
;
if
(
isUpdating
)
{
// For updating
count
=
postRepository
.
countByIdNotAndUrl
(
post
.
getId
(),
post
.
getUrl
());
}
else
{
// For creating
count
=
postRepository
.
countByUrl
(
post
.
getUrl
());
}
if
(
count
>
0
)
{
throw
new
AlreadyExistsException
(
"The post url has been exist already"
).
setErrorData
(
post
.
getUrl
());
}
// Render content
post
.
setFormatContent
(
MarkdownUtils
.
renderMarkdown
(
post
.
getOriginalContent
()));
//
U
pdate post
post
=
postOperation
.
appl
y
(
post
);
//
Create or u
pdate post
post
=
createOrUpdateB
y
(
post
);
// List all tags
List
<
Tag
>
tags
=
tagService
.
listAllByIds
(
tagIds
);
...
...
@@ -291,25 +236,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
()
->
ServiceUtils
.
fetchProperty
(
postCategories
,
PostCategory:
:
getCategoryId
));
}
/**
* Gets post by url.
*
* @param url post url.
* @return Post
*/
@Override
public
Post
getByUrl
(
String
url
)
{
return
postRepository
.
getByUrl
(
url
).
orElseThrow
(()
->
new
NotFoundException
(
"The post does not exist"
).
setErrorData
(
url
));
}
@Override
public
Post
getBy
(
PostStatus
status
,
String
url
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
Assert
.
hasText
(
url
,
"Post url must not be blank"
);
Optional
<
Post
>
postOptional
=
postRepository
.
getByUrlAndStatus
(
url
,
status
);
Post
post
=
postOptional
.
orElseThrow
(()
->
new
NotFoundException
(
"The post with status "
+
status
+
" and url "
+
url
+
"was not existed"
).
setErrorData
(
url
));
Post
post
=
super
.
getBy
(
status
,
url
);
if
(
PostStatus
.
PUBLISHED
.
equals
(
status
))
{
// Log it
...
...
@@ -329,52 +258,6 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return
convertTo
(
post
);
}
@Override
public
long
countVisit
()
{
return
Optional
.
ofNullable
(
postRepository
.
countVisit
()).
orElse
(
0L
);
}
@Override
public
long
countLike
()
{
return
Optional
.
ofNullable
(
postRepository
.
countLike
()).
orElse
(
0L
);
}
@Override
public
void
increaseVisit
(
long
visits
,
Integer
postId
)
{
Assert
.
isTrue
(
visits
>
0
,
"Visits to increase must not be less than 1"
);
Assert
.
notNull
(
postId
,
"Goods id must not be null"
);
long
affectedRows
=
postRepository
.
updateVisit
(
visits
,
postId
);
if
(
affectedRows
!=
1
)
{
log
.
error
(
"Post with id: [{}] may not be found"
,
postId
);
throw
new
BadRequestException
(
"Failed to increase visits "
+
visits
+
" for post with id "
+
postId
);
}
}
@Override
public
void
increaseLike
(
long
likes
,
Integer
postId
)
{
Assert
.
isTrue
(
likes
>
0
,
"Likes to increase must not be less than 1"
);
Assert
.
notNull
(
postId
,
"Goods id must not be null"
);
long
affectedRows
=
postRepository
.
updateLikes
(
likes
,
postId
);
if
(
affectedRows
!=
1
)
{
log
.
error
(
"Post with id: [{}] may not be found"
,
postId
);
throw
new
BadRequestException
(
"Failed to increase likes "
+
likes
+
" for post with id "
+
postId
);
}
}
@Override
public
void
increaseVisit
(
Integer
postId
)
{
increaseVisit
(
1L
,
postId
);
}
@Override
public
void
increaseLike
(
Integer
postId
)
{
increaseLike
(
1L
,
postId
);
}
@Override
public
List
<
ArchiveYearVO
>
listYearArchives
()
{
// Get all posts
...
...
@@ -517,61 +400,6 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
});
}
@Override
public
List
<
Post
>
listAllBy
(
PostStatus
status
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
return
postRepository
.
findAllByStatus
(
status
);
}
@Override
public
Post
filterIfEncrypt
(
Post
post
)
{
Assert
.
notNull
(
post
,
"Post must not be null"
);
if
(
StringUtils
.
isNotBlank
(
post
.
getPassword
()))
{
String
tip
=
"The post is encrypted by author"
;
post
.
setSummary
(
tip
);
post
.
setOriginalContent
(
tip
);
post
.
setFormatContent
(
tip
);
}
return
post
;
}
@Override
public
Optional
<
Post
>
getPrePost
(
Date
date
)
{
List
<
Post
>
posts
=
listPrePosts
(
date
,
1
);
return
CollectionUtils
.
isEmpty
(
posts
)
?
Optional
.
empty
()
:
Optional
.
of
(
posts
.
get
(
0
));
}
@Override
public
Optional
<
Post
>
getNextPost
(
Date
date
)
{
List
<
Post
>
posts
=
listNextPosts
(
date
,
1
);
return
CollectionUtils
.
isEmpty
(
posts
)
?
Optional
.
empty
()
:
Optional
.
of
(
posts
.
get
(
0
));
}
@Override
public
List
<
Post
>
listPrePosts
(
Date
date
,
int
size
)
{
Assert
.
notNull
(
date
,
"Date must not be null"
);
return
postRepository
.
findAllByStatusAndCreateTimeAfter
(
PostStatus
.
PUBLISHED
,
date
,
PageRequest
.
of
(
0
,
size
,
Sort
.
by
(
ASC
,
"createTime"
)))
.
getContent
();
}
@Override
public
List
<
Post
>
listNextPosts
(
Date
date
,
int
size
)
{
Assert
.
notNull
(
date
,
"Date must not be null"
);
return
postRepository
.
findAllByStatusAndCreateTimeBefore
(
PostStatus
.
PUBLISHED
,
date
,
PageRequest
.
of
(
0
,
size
,
Sort
.
by
(
DESC
,
"createTime"
)))
.
getContent
();
}
/**
* Converts to post minimal output dto.
*
...
...
src/main/java/run/halo/app/service/impl/SheetCommentServiceImpl.java
浏览文件 @
4a46132f
...
...
@@ -7,7 +7,6 @@ import run.halo.app.repository.PostRepository;
import
run.halo.app.repository.SheetCommentRepository
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.SheetCommentService
;
import
run.halo.app.service.base.BaseCommentServiceImpl
;
/**
* Sheet comment service implementation.
...
...
src/main/java/run/halo/app/service/impl/SheetServiceImpl.java
浏览文件 @
4a46132f
...
...
@@ -3,11 +3,8 @@ package run.halo.app.service.impl;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.lang.NonNull
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.Assert
;
import
run.halo.app.exception.AlreadyExistsException
;
import
run.halo.app.exception.NotFoundException
;
import
run.halo.app.model.dto.post.SheetDetailDTO
;
import
run.halo.app.model.dto.post.SheetListDTO
;
import
run.halo.app.model.entity.Sheet
;
...
...
@@ -15,14 +12,10 @@ import run.halo.app.model.enums.PostStatus;
import
run.halo.app.repository.SheetRepository
;
import
run.halo.app.service.SheetCommentService
;
import
run.halo.app.service.SheetService
;
import
run.halo.app.service.base.AbstractCrudService
;
import
run.halo.app.utils.DateUtils
;
import
run.halo.app.utils.MarkdownUtils
;
import
run.halo.app.utils.ServiceUtils
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.Set
;
/**
...
...
@@ -32,7 +25,7 @@ import java.util.Set;
* @date 19-4-24
*/
@Service
public
class
SheetServiceImpl
extends
AbstractCrudService
<
Sheet
,
Integer
>
implements
SheetService
{
public
class
SheetServiceImpl
extends
BasePostServiceImpl
<
Sheet
>
implements
SheetService
{
private
final
SheetRepository
sheetRepository
;
...
...
@@ -75,12 +68,7 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
*/
@Override
public
Sheet
getBy
(
PostStatus
status
,
String
url
)
{
Assert
.
notNull
(
status
,
"Post status must not be null"
);
Assert
.
hasText
(
url
,
"Sheet url must not be blank"
);
Optional
<
Sheet
>
sheetOptional
=
sheetRepository
.
getByUrlAndStatus
(
url
,
status
);
Sheet
sheet
=
sheetOptional
.
orElseThrow
(()
->
new
NotFoundException
(
"The sheet with status "
+
status
+
" and url "
+
url
+
"was not existed"
).
setErrorData
(
url
));
Sheet
sheet
=
super
.
getBy
(
status
,
url
);
if
(
PostStatus
.
PUBLISHED
.
equals
(
status
))
{
// Log it
...
...
@@ -118,52 +106,4 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
});
}
@NonNull
private
Sheet
createOrUpdateBy
(
@NonNull
Sheet
sheet
)
{
Assert
.
notNull
(
sheet
,
"Sheet must not be null"
);
// Check url
urlMustNotExist
(
sheet
);
// Render content
sheet
.
setFormatContent
(
MarkdownUtils
.
renderMarkdown
(
sheet
.
getOriginalContent
()));
// Create or update post
if
(
ServiceUtils
.
isEmptyId
(
sheet
.
getId
()))
{
// The sheet will be created
return
create
(
sheet
);
}
// The sheet will be updated
// Set edit time
sheet
.
setEditTime
(
DateUtils
.
now
());
// Update it
return
update
(
sheet
);
}
/**
* Check if the url is exist.
*
* @param sheet sheet must not be null
*/
private
void
urlMustNotExist
(
@NonNull
Sheet
sheet
)
{
Assert
.
notNull
(
sheet
,
"Sheet must not be null"
);
// TODO Refactor this method with BasePostService
// TODO May refactor these queries
// Get url count
long
count
;
if
(
ServiceUtils
.
isEmptyId
(
sheet
.
getId
()))
{
// The sheet will be created
count
=
sheetRepository
.
countByUrl
(
sheet
.
getUrl
());
}
else
{
// The sheet will be updated
count
=
sheetRepository
.
countByIdNotAndUrl
(
sheet
.
getId
(),
sheet
.
getUrl
());
}
if
(
count
>
0
)
{
throw
new
AlreadyExistsException
(
"The sheet url has been exist"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录