From 7c0be43c0dbe2d5dc4c69093a1be7a3d7a4ea682 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Wed, 2 Dec 2020 10:24:14 +0800 Subject: [PATCH] feat: #1158 (#1176) --- .../content/api/JournalController.java | 6 +++++ .../content/api/PostController.java | 22 ++++++++++++++++--- .../app/repository/JournalRepository.java | 14 ++++++++++++ .../run/halo/app/service/JournalService.java | 15 +++++++++++++ .../app/service/impl/BasePostServiceImpl.java | 5 ++--- .../app/service/impl/JournalServiceImpl.java | 22 +++++++++++++++++++ 6 files changed, 78 insertions(+), 6 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/api/JournalController.java b/src/main/java/run/halo/app/controller/content/api/JournalController.java index 0c08ea42d..602d8efa4 100644 --- a/src/main/java/run/halo/app/controller/content/api/JournalController.java +++ b/src/main/java/run/halo/app/controller/content/api/JournalController.java @@ -111,4 +111,10 @@ public class JournalController { journalCommentParam.setContent(HtmlUtils.htmlEscape(journalCommentParam.getContent(), StandardCharsets.UTF_8.displayName())); return journalCommentService.convertTo(journalCommentService.createBy(journalCommentParam)); } + + @PostMapping("{id:\\d+}/likes") + @ApiOperation("Likes a journal") + public void like(@PathVariable("id") Integer id) { + journalService.increaseLike(id); + } } diff --git a/src/main/java/run/halo/app/controller/content/api/PostController.java b/src/main/java/run/halo/app/controller/content/api/PostController.java index 5cee406fb..e1570e86e 100644 --- a/src/main/java/run/halo/app/controller/content/api/PostController.java +++ b/src/main/java/run/halo/app/controller/content/api/PostController.java @@ -10,6 +10,7 @@ import org.springframework.data.web.SortDefault; import org.springframework.web.bind.annotation.*; import org.springframework.web.util.HtmlUtils; import run.halo.app.cache.lock.CacheLock; +import run.halo.app.exception.NotFoundException; import run.halo.app.model.dto.BaseCommentDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.entity.Post; @@ -31,6 +32,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * Content post controller. * * @author johnniang + * @author ryanwang * @date 2019-04-02 */ @RestController("ApiContentPostController") @@ -88,6 +90,22 @@ public class PostController { return postDetailVO; } + @GetMapping("{postId:\\d+}/prev") + @ApiOperation("Gets previous post by current post id.") + public PostDetailVO getPrevPostBy(@PathVariable("postId") Integer postId) { + Post post = postService.getById(postId); + Post prevPost = postService.getPrevPost(post).orElseThrow(() -> new NotFoundException("查询不到该文章的信息")); + return postService.convertToDetailVo(prevPost); + } + + @GetMapping("{postId:\\d+}/next") + @ApiOperation("Gets next post by current post id.") + public PostDetailVO getNextPostBy(@PathVariable("postId") Integer postId) { + Post post = postService.getById(postId); + Post nextPost = postService.getNextPost(post).orElseThrow(() -> new NotFoundException("查询不到该文章的信息")); + return postService.convertToDetailVo(nextPost); + } + @GetMapping("/slug") @ApiOperation("Gets a post") public PostDetailVO getBy(@RequestParam("slug") String slug, @@ -114,7 +132,6 @@ public class PostController { public Page listTopComments(@PathVariable("postId") Integer postId, @RequestParam(name = "page", required = false, defaultValue = "0") int page, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { - return postCommentService.pageTopCommentsBy(postId, CommentStatus.PUBLISHED, PageRequest.of(page, optionService.getCommentPageSize(), sort)); } @@ -143,8 +160,7 @@ public class PostController { public Page listComments(@PathVariable("postId") Integer postId, @RequestParam(name = "page", required = false, defaultValue = "0") int page, @SortDefault(sort = "createTime", direction = DESC) Sort sort) { - Page result = postCommentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort)); - return result; + return postCommentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort)); } @PostMapping("comments") diff --git a/src/main/java/run/halo/app/repository/JournalRepository.java b/src/main/java/run/halo/app/repository/JournalRepository.java index 38b73770e..671ae2148 100644 --- a/src/main/java/run/halo/app/repository/JournalRepository.java +++ b/src/main/java/run/halo/app/repository/JournalRepository.java @@ -3,6 +3,9 @@ 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.Journal; import run.halo.app.model.enums.JournalType; @@ -26,4 +29,15 @@ public interface JournalRepository extends BaseRepository, Jpa */ @NonNull Page findAllByType(@NonNull JournalType type, @NonNull Pageable pageable); + + /** + * Updates journal likes. + * + * @param likes likes delta + * @param id id must not be null + * @return updated rows + */ + @Modifying + @Query("update Journal j set j.likes = j.likes + :likes where j.id = :id") + int updateLikes(@Param("likes") long likes, @Param("id") @NonNull Integer id); } diff --git a/src/main/java/run/halo/app/service/JournalService.java b/src/main/java/run/halo/app/service/JournalService.java index 4c21d98f6..c7211f8cc 100644 --- a/src/main/java/run/halo/app/service/JournalService.java +++ b/src/main/java/run/halo/app/service/JournalService.java @@ -94,4 +94,19 @@ public interface JournalService extends CrudService { */ @NonNull Page convertToCmtCountDto(@NonNull Page journalPage); + + /** + * Increases journal likes(1). + * + * @param id id must not be null + */ + void increaseLike(@NonNull Integer id); + + /** + * Increase journal likes. + * + * @param likes likes must not be less than 1 + * @param id id must not be null + */ + void increaseLike(long likes, @NonNull Integer id); } diff --git a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java index 02c786a56..70702d2d7 100644 --- a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java @@ -216,8 +216,7 @@ public abstract class BasePostServiceImpl extends Abstrac Assert.notNull(postId, "Post id must not be null"); boolean finishedIncrease; - if (basePostRepository.getByIdAndStatus(postId, PostStatus.DRAFT).isPresent()) - { + if (basePostRepository.getByIdAndStatus(postId, PostStatus.DRAFT).isPresent()) { finishedIncrease = true; log.info("Post with id: [{}] is a draft and visits will not be updated", postId); } else { @@ -234,7 +233,7 @@ public abstract class BasePostServiceImpl extends Abstrac @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"); + Assert.notNull(postId, "Post id must not be null"); long affectedRows = basePostRepository.updateLikes(likes, postId); diff --git a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java index 1cbdac4ba..00592259d 100644 --- a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java @@ -8,8 +8,10 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import run.halo.app.exception.BadRequestException; import run.halo.app.model.dto.JournalDTO; import run.halo.app.model.dto.JournalWithCmtCountDTO; import run.halo.app.model.entity.Journal; @@ -139,6 +141,26 @@ public class JournalServiceImpl extends AbstractCrudService im return new PageImpl<>(journalWithCmtCountDTOS, journalPage.getPageable(), journalPage.getTotalElements()); } + @Override + @Transactional + public void increaseLike(Integer id) { + increaseLike(1L, id); + } + + + @Override + @Transactional + public void increaseLike(long likes, Integer id) { + Assert.isTrue(likes > 0, "Likes to increase must not be less than 1"); + Assert.notNull(id, "Journal id must not be null"); + + long affectedRows = journalRepository.updateLikes(likes, id); + + if (affectedRows != 1) { + log.error("Journal with id: [{}] may not be found", id); + throw new BadRequestException("Failed to increase likes " + likes + " for journal with id " + id); + } + } /** * Build specification by journal query.