mirror of https://github.com/halo-dev/halo
Refactor post repository
parent
204bc8b4a4
commit
06ad1bf82f
|
@ -65,9 +65,9 @@ public class ContentSheetController {
|
|||
public String sheet(@PathVariable(value = "url") String url,
|
||||
@RequestParam(value = "cp", defaultValue = "1") Integer cp,
|
||||
Model model) {
|
||||
final Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url);
|
||||
Sheet sheet = sheetService.getBy(PostStatus.PUBLISHED, url);
|
||||
|
||||
model.addAttribute("sheet", sheet);
|
||||
model.addAttribute("sheet", sheetService.convertToDetailDto(sheet));
|
||||
|
||||
if (StrUtil.isNotEmpty(sheet.getTemplate())) {
|
||||
return themeService.render(sheet.getTemplate());
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.springframework.util.Assert;
|
|||
* @author johnniang
|
||||
* @date 19-4-22
|
||||
*/
|
||||
public class VisitEvent extends ApplicationEvent {
|
||||
public class PostVisitEvent extends ApplicationEvent {
|
||||
|
||||
private final Integer postId;
|
||||
|
||||
|
@ -20,7 +20,7 @@ public class VisitEvent extends ApplicationEvent {
|
|||
* @param source the object on which the event initially occurred (never {@code null})
|
||||
* @param postId post id
|
||||
*/
|
||||
public VisitEvent(@NonNull Object source, @NonNull Integer postId) {
|
||||
public PostVisitEvent(@NonNull Object source, @NonNull Integer postId) {
|
||||
super(source);
|
||||
|
||||
Assert.notNull(postId, "Post id must not be null");
|
|
@ -18,7 +18,7 @@ import java.util.concurrent.*;
|
|||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class VisitEventListener {
|
||||
public class PostVisitEventListener {
|
||||
|
||||
private final Map<Integer, BlockingQueue<Integer>> postVisitQueueMap;
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class VisitEventListener {
|
|||
|
||||
private final ExecutorService executor;
|
||||
|
||||
public VisitEventListener(PostService postService) {
|
||||
public PostVisitEventListener(PostService postService) {
|
||||
this.postService = postService;
|
||||
|
||||
int initCapacity = 8;
|
||||
|
@ -47,7 +47,7 @@ public class VisitEventListener {
|
|||
|
||||
@Async
|
||||
@EventListener
|
||||
public void onApplicationEvent(VisitEvent event) throws InterruptedException {
|
||||
public void onApplicationEvent(PostVisitEvent event) throws InterruptedException {
|
||||
// Get post id
|
||||
Integer postId = event.getPostId();
|
||||
|
|
@ -1,19 +1,10 @@
|
|||
package run.halo.app.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.lang.NonNull;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.repository.base.BasePostRepository;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Post repository.
|
||||
|
@ -23,26 +14,12 @@ import java.util.Optional;
|
|||
*/
|
||||
public interface PostRepository extends BasePostRepository<Post>, JpaSpecificationExecutor<Post> {
|
||||
|
||||
@Override
|
||||
@Query("select sum(p.visits) from Post p")
|
||||
Long countVisit();
|
||||
|
||||
@Override
|
||||
@Query("select sum(p.likes) from Post p")
|
||||
Long countLike();
|
||||
|
||||
@NonNull
|
||||
Page<Post> findAllByStatusAndCreateTimeBefore(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||
|
||||
@NonNull
|
||||
Page<Post> findAllByStatusAndCreateTimeAfter(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||
|
||||
@NonNull
|
||||
Optional<Post> getByUrlAndStatus(@NonNull String url, @NonNull PostStatus status);
|
||||
|
||||
@Modifying
|
||||
@Query("update Post p set p.visits = p.visits + :visits where p.id = :postId")
|
||||
int updateVisit(@Param("visits") long visits, @Param("postId") @NonNull Integer postId);
|
||||
|
||||
@Modifying
|
||||
@Query("update Post p set p.likes = p.likes + :likes where p.id = :postId")
|
||||
int updateLikes(@Param("likes") long likes, @Param("postId") @NonNull Integer postId);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package run.halo.app.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.lang.NonNull;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.entity.Sheet;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.repository.base.BasePostRepository;
|
||||
|
@ -16,6 +16,14 @@ import java.util.Optional;
|
|||
*/
|
||||
public interface SheetRepository extends BasePostRepository<Sheet> {
|
||||
|
||||
@Override
|
||||
@Query("select sum(p.visits) from Sheet p")
|
||||
Long countVisit();
|
||||
|
||||
@Override
|
||||
@Query("select sum(p.likes) from Sheet p")
|
||||
Long countLike();
|
||||
|
||||
@NonNull
|
||||
Optional<Sheet> getByUrlAndStatus(@NonNull String url, @NonNull PostStatus status);
|
||||
}
|
||||
|
|
|
@ -3,11 +3,14 @@ package run.halo.app.repository.base;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.lang.NonNull;
|
||||
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;
|
||||
|
||||
|
@ -17,8 +20,23 @@ import java.util.Optional;
|
|||
* @author johnniang
|
||||
* @date 3/22/19
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface BasePostRepository<DOMAIN extends BasePost> extends BaseRepository<DOMAIN, Integer> {
|
||||
public interface BasePostRepository<POST extends BasePost> extends BaseRepository<POST, Integer> {
|
||||
|
||||
/**
|
||||
* Counts visits. (Need to be overridden)
|
||||
*
|
||||
* @return total visits
|
||||
*/
|
||||
@Query("select sum(p.visits) from BasePost p")
|
||||
Long countVisit();
|
||||
|
||||
/**
|
||||
* Counts likes. (Need to be overridden)
|
||||
*
|
||||
* @return total likes
|
||||
*/
|
||||
@Query("select sum(p.likes) from BasePost p")
|
||||
Long countLike();
|
||||
|
||||
/**
|
||||
* Finds posts by status and pageable.
|
||||
|
@ -28,7 +46,7 @@ public interface BasePostRepository<DOMAIN extends BasePost> extends BaseReposit
|
|||
* @return a page of post
|
||||
*/
|
||||
@NonNull
|
||||
Page<DOMAIN> findAllByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
|
||||
Page<POST> findAllByStatus(@NonNull PostStatus status, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Finds posts by status.
|
||||
|
@ -37,7 +55,7 @@ public interface BasePostRepository<DOMAIN extends BasePost> extends BaseReposit
|
|||
* @return a list of post
|
||||
*/
|
||||
@NonNull
|
||||
List<DOMAIN> findAllByStatus(@NonNull PostStatus status);
|
||||
List<POST> findAllByStatus(@NonNull PostStatus status);
|
||||
|
||||
/**
|
||||
* Finds posts by status.
|
||||
|
@ -47,7 +65,40 @@ public interface BasePostRepository<DOMAIN extends BasePost> extends BaseReposit
|
|||
* @return a list of post
|
||||
*/
|
||||
@NonNull
|
||||
List<DOMAIN> findAllByStatus(@NonNull PostStatus status, @NonNull Sort sort);
|
||||
List<POST> findAllByStatus(@NonNull PostStatus status, @NonNull Sort sort);
|
||||
|
||||
/**
|
||||
* Finds all post by status and create time before.
|
||||
*
|
||||
* @param status status must not be null
|
||||
* @param createTime create time must not be null
|
||||
* @param pageable page info must not be null
|
||||
* @return a page of post
|
||||
*/
|
||||
@NonNull
|
||||
Page<POST> findAllByStatusAndCreateTimeBefore(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Finds all post by status and create time after.
|
||||
*
|
||||
* @param status status must not be null
|
||||
* @param createTime create time must not be null
|
||||
* @param pageable page info must not be null
|
||||
* @return a page of post
|
||||
*/
|
||||
@NonNull
|
||||
Page<POST> findAllByStatusAndCreateTimeAfter(@NonNull PostStatus status, @NonNull Date createTime, @NonNull Pageable pageable);
|
||||
|
||||
/**
|
||||
* Gets post by url and status.
|
||||
*
|
||||
* @param url url must not be blank
|
||||
* @param status status must not be null
|
||||
* @return an optional post
|
||||
*/
|
||||
@NonNull
|
||||
Optional<POST> getByUrlAndStatus(@NonNull String url, @NonNull PostStatus status);
|
||||
|
||||
|
||||
/**
|
||||
* Counts posts by status and type.
|
||||
|
@ -80,6 +131,28 @@ public interface BasePostRepository<DOMAIN extends BasePost> extends BaseReposit
|
|||
* @param url post url
|
||||
* @return Optional<Post>
|
||||
*/
|
||||
Optional<DOMAIN> getByUrl(@NonNull String url);
|
||||
Optional<POST> getByUrl(@NonNull String url);
|
||||
|
||||
/**
|
||||
* Updates post visits.
|
||||
*
|
||||
* @param visits visit delta
|
||||
* @param postId post id must not be null
|
||||
* @return updated rows
|
||||
*/
|
||||
@Modifying
|
||||
@Query("update BasePost p set p.visits = p.visits + :visits where p.id = :postId")
|
||||
int updateVisit(@Param("visits") long visits, @Param("postId") @NonNull Integer postId);
|
||||
|
||||
/**
|
||||
* Updates post likes.
|
||||
*
|
||||
* @param likes likes delta
|
||||
* @param postId post id must not be null
|
||||
* @return updated rows
|
||||
*/
|
||||
@Modifying
|
||||
@Query("update BasePost p set p.likes = p.likes + :likes where p.id = :postId")
|
||||
int updateLikes(@Param("likes") long likes, @Param("postId") @NonNull Integer postId);
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.event.logger.LogEvent;
|
||||
import run.halo.app.event.post.VisitEvent;
|
||||
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;
|
||||
|
@ -313,7 +313,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
|||
|
||||
if (PostStatus.PUBLISHED.equals(status)) {
|
||||
// Log it
|
||||
eventPublisher.publishEvent(new VisitEvent(this, post.getId()));
|
||||
eventPublisher.publishEvent(new PostVisitEvent(this, post.getId()));
|
||||
}
|
||||
|
||||
return post;
|
||||
|
|
|
@ -6,7 +6,6 @@ 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.event.post.VisitEvent;
|
||||
import run.halo.app.exception.AlreadyExistsException;
|
||||
import run.halo.app.exception.NotFoundException;
|
||||
import run.halo.app.model.dto.post.SheetDetailDTO;
|
||||
|
@ -85,7 +84,8 @@ public class SheetServiceImpl extends AbstractCrudService<Sheet, Integer> implem
|
|||
|
||||
if (PostStatus.PUBLISHED.equals(status)) {
|
||||
// Log it
|
||||
eventPublisher.publishEvent(new VisitEvent(this, sheet.getId()));
|
||||
// TODO Fatal bug here
|
||||
// eventPublisher.publishEvent(new PostVisitEvent(this, sheet.getId()));
|
||||
}
|
||||
|
||||
return sheet;
|
||||
|
|
Loading…
Reference in New Issue