Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Dec 1, 2020
1 parent dccd213 commit 6fb9084
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,10 @@ public BaseCommentDTO comment(@RequestBody JournalCommentParam journalCommentPar
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -31,6 +32,7 @@
* Content post controller.
*
* @author johnniang
* @author ryanwang
* @date 2019-04-02
*/
@RestController("ApiContentPostController")
Expand Down Expand Up @@ -88,6 +90,22 @@ public PostDetailVO getBy(@PathVariable("postId") Integer postId,
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,
Expand All @@ -114,7 +132,6 @@ public PostDetailVO getBy(@RequestParam("slug") String slug,
public Page<CommentWithHasChildrenVO> 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));
}

Expand Down Expand Up @@ -143,8 +160,7 @@ public Page<BaseCommentVO> listCommentsTree(@PathVariable("postId") Integer post
public Page<BaseCommentWithParentVO> listComments(@PathVariable("postId") Integer postId,
@RequestParam(name = "page", required = false, defaultValue = "0") int page,
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
Page<BaseCommentWithParentVO> result = postCommentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
return result;
return postCommentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
}

@PostMapping("comments")
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/run/halo/app/repository/JournalRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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;
Expand All @@ -26,4 +29,15 @@ public interface JournalRepository extends BaseRepository<Journal, Integer>, Jpa
*/
@NonNull
Page<Journal> 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);
}
15 changes: 15 additions & 0 deletions src/main/java/run/halo/app/service/JournalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ public interface JournalService extends CrudService<Journal, Integer> {
*/
@NonNull
Page<JournalWithCmtCountDTO> convertToCmtCountDto(@NonNull Page<Journal> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public void increaseVisit(long visits, Integer postId) {
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 {
Expand All @@ -234,7 +233,7 @@ public void increaseVisit(long visits, Integer postId) {
@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);

Expand Down
22 changes: 22 additions & 0 deletions src/main/java/run/halo/app/service/impl/JournalServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
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;
Expand Down Expand Up @@ -139,6 +141,26 @@ public Page<JournalWithCmtCountDTO> convertToCmtCountDto(Page<Journal> journalPa
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.
Expand Down

0 comments on commit 6fb9084

Please sign in to comment.