mirror of https://github.com/halo-dev/halo
Relocate Transactional annotation
parent
d41d3d8517
commit
71b661d440
|
@ -55,17 +55,19 @@ public class ControllerLogAop {
|
|||
return;
|
||||
}
|
||||
|
||||
boolean hasServletArg = false;
|
||||
boolean shouldNotLog = false;
|
||||
for (Object arg : args) {
|
||||
if (arg instanceof HttpServletRequest ||
|
||||
if (arg == null ||
|
||||
arg instanceof HttpServletRequest ||
|
||||
arg instanceof HttpServletResponse ||
|
||||
arg instanceof MultipartFile) {
|
||||
hasServletArg = true;
|
||||
arg instanceof MultipartFile ||
|
||||
arg.getClass().isAssignableFrom(MultipartFile[].class)) {
|
||||
shouldNotLog = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasServletArg) {
|
||||
if (!shouldNotLog) {
|
||||
String requestBody = JsonUtils.objectToJson(args);
|
||||
log.debug("{}.{} Parameters: [{}]", clazzName, methodName, requestBody);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package run.halo.app.service;
|
|||
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.Post;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.params.PostQuery;
|
||||
|
@ -55,7 +54,6 @@ public interface PostService extends BasePostService<Post> {
|
|||
* @return post created
|
||||
*/
|
||||
@NonNull
|
||||
@Transactional
|
||||
PostDetailVO createBy(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave);
|
||||
|
||||
/**
|
||||
|
@ -68,7 +66,6 @@ public interface PostService extends BasePostService<Post> {
|
|||
* @return updated post
|
||||
*/
|
||||
@NonNull
|
||||
@Transactional
|
||||
PostDetailVO updateBy(@NonNull Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave);
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,7 +4,6 @@ import org.springframework.data.domain.Page;
|
|||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
|
@ -154,7 +153,6 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
|
|||
* @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);
|
||||
|
||||
/**
|
||||
|
@ -163,7 +161,6 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
|
|||
* @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);
|
||||
|
||||
/**
|
||||
|
@ -171,7 +168,6 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
|
|||
*
|
||||
* @param postId post id must not be null
|
||||
*/
|
||||
@Transactional
|
||||
void increaseVisit(@NonNull Integer postId);
|
||||
|
||||
/**
|
||||
|
@ -179,7 +175,6 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
|
|||
*
|
||||
* @param postId post id must not be null
|
||||
*/
|
||||
@Transactional
|
||||
void increaseLike(@NonNull Integer postId);
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,6 +7,7 @@ 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.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.exception.AlreadyExistsException;
|
||||
|
@ -173,6 +174,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void increaseVisit(long visits, Integer postId) {
|
||||
Assert.isTrue(visits > 0, "Visits to increase must not be less than 1");
|
||||
Assert.notNull(postId, "Post id must not be null");
|
||||
|
@ -186,6 +188,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
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");
|
||||
|
@ -199,16 +202,19 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void increaseVisit(Integer postId) {
|
||||
increaseVisit(1L, postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void increaseLike(Integer postId) {
|
||||
increaseLike(1L, postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public POST createOrUpdateBy(POST post) {
|
||||
Assert.notNull(post, "Post must not be null");
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.springframework.data.jpa.domain.Specification;
|
|||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import run.halo.app.event.logger.LogEvent;
|
||||
import run.halo.app.event.post.PostVisitEvent;
|
||||
|
@ -35,7 +36,6 @@ import javax.persistence.criteria.Predicate;
|
|||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.Subquery;
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||
|
@ -105,49 +105,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return postRepository.findAll(buildSpecByQuery(postQuery), pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build specification by post query.
|
||||
*
|
||||
* @param postQuery post query must not be null
|
||||
* @return a post specification
|
||||
*/
|
||||
@NonNull
|
||||
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
|
||||
Assert.notNull(postQuery, "Post query must not be null");
|
||||
|
||||
return (Specification<Post>) (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new LinkedList<>();
|
||||
|
||||
if (postQuery.getStatus() != null) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("status"), postQuery.getStatus()));
|
||||
}
|
||||
|
||||
if (postQuery.getCategoryId() != null) {
|
||||
Subquery<Post> postSubquery = query.subquery(Post.class);
|
||||
Root<PostCategory> postCategoryRoot = postSubquery.from(PostCategory.class);
|
||||
postSubquery.select(postCategoryRoot.get("postId"));
|
||||
postSubquery.where(
|
||||
criteriaBuilder.equal(root.get("id"), postCategoryRoot.get("postId")),
|
||||
criteriaBuilder.equal(postCategoryRoot.get("categoryId"), postQuery.getCategoryId()));
|
||||
predicates.add(criteriaBuilder.exists(postSubquery));
|
||||
}
|
||||
|
||||
if (postQuery.getKeyword() != null) {
|
||||
// Format like condition
|
||||
String likeCondition = String.format("%%%s%%", StringUtils.strip(postQuery.getKeyword()));
|
||||
|
||||
// Build like predicate
|
||||
Predicate titleLike = criteriaBuilder.like(root.get("title"), likeCondition);
|
||||
Predicate originalContentLike = criteriaBuilder.like(root.get("originalContent"), likeCondition);
|
||||
|
||||
predicates.add(criteriaBuilder.or(titleLike, originalContentLike));
|
||||
}
|
||||
|
||||
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) {
|
||||
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds);
|
||||
if (!autoSave) {
|
||||
|
@ -159,6 +118,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, boolean autoSave) {
|
||||
// Set edit time
|
||||
postToUpdate.setEditTime(DateUtils.now());
|
||||
|
@ -171,36 +131,6 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return updatedPost;
|
||||
}
|
||||
|
||||
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||
Assert.notNull(post, "Post param must not be null");
|
||||
|
||||
// Create or update post
|
||||
post = super.createOrUpdateBy(post);
|
||||
|
||||
postTagService.removeByPostId(post.getId());
|
||||
|
||||
postCategoryService.removeByPostId(post.getId());
|
||||
|
||||
// List all tags
|
||||
List<Tag> tags = tagService.listAllByIds(tagIds);
|
||||
|
||||
// List all categories
|
||||
List<Category> categories = categoryService.listAllByIds(categoryIds);
|
||||
|
||||
// Create post tags
|
||||
List<PostTag> postTags = postTagService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(tags, Tag::getId));
|
||||
|
||||
log.debug("Created post tags: [{}]", postTags);
|
||||
|
||||
// Create post categories
|
||||
List<PostCategory> postCategories = postCategoryService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(categories, Category::getId));
|
||||
|
||||
log.debug("Created post categories: [{}]", postCategories);
|
||||
|
||||
// Convert to post detail vo
|
||||
return convertTo(post, tags, categories);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getBy(PostStatus status, String url) {
|
||||
Post post = super.getBy(status, url);
|
||||
|
@ -526,4 +456,76 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
|
||||
return postDetailVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build specification by post query.
|
||||
*
|
||||
* @param postQuery post query must not be null
|
||||
* @return a post specification
|
||||
*/
|
||||
@NonNull
|
||||
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
|
||||
Assert.notNull(postQuery, "Post query must not be null");
|
||||
|
||||
return (Specification<Post>) (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new LinkedList<>();
|
||||
|
||||
if (postQuery.getStatus() != null) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("status"), postQuery.getStatus()));
|
||||
}
|
||||
|
||||
if (postQuery.getCategoryId() != null) {
|
||||
Subquery<Post> postSubquery = query.subquery(Post.class);
|
||||
Root<PostCategory> postCategoryRoot = postSubquery.from(PostCategory.class);
|
||||
postSubquery.select(postCategoryRoot.get("postId"));
|
||||
postSubquery.where(
|
||||
criteriaBuilder.equal(root.get("id"), postCategoryRoot.get("postId")),
|
||||
criteriaBuilder.equal(postCategoryRoot.get("categoryId"), postQuery.getCategoryId()));
|
||||
predicates.add(criteriaBuilder.exists(postSubquery));
|
||||
}
|
||||
|
||||
if (postQuery.getKeyword() != null) {
|
||||
// Format like condition
|
||||
String likeCondition = String.format("%%%s%%", StringUtils.strip(postQuery.getKeyword()));
|
||||
|
||||
// Build like predicate
|
||||
Predicate titleLike = criteriaBuilder.like(root.get("title"), likeCondition);
|
||||
Predicate originalContentLike = criteriaBuilder.like(root.get("originalContent"), likeCondition);
|
||||
|
||||
predicates.add(criteriaBuilder.or(titleLike, originalContentLike));
|
||||
}
|
||||
|
||||
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
||||
};
|
||||
}
|
||||
|
||||
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds) {
|
||||
Assert.notNull(post, "Post param must not be null");
|
||||
|
||||
// Create or update post
|
||||
post = super.createOrUpdateBy(post);
|
||||
|
||||
postTagService.removeByPostId(post.getId());
|
||||
|
||||
postCategoryService.removeByPostId(post.getId());
|
||||
|
||||
// List all tags
|
||||
List<Tag> tags = tagService.listAllByIds(tagIds);
|
||||
|
||||
// List all categories
|
||||
List<Category> categories = categoryService.listAllByIds(categoryIds);
|
||||
|
||||
// Create post tags
|
||||
List<PostTag> postTags = postTagService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(tags, Tag::getId));
|
||||
|
||||
log.debug("Created post tags: [{}]", postTags);
|
||||
|
||||
// Create post categories
|
||||
List<PostCategory> postCategories = postCategoryService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(categories, Category::getId));
|
||||
|
||||
log.debug("Created post categories: [{}]", postCategories);
|
||||
|
||||
// Convert to post detail vo
|
||||
return convertTo(post, tags, categories);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue