mirror of https://github.com/halo-dev/halo
parent
7de339571d
commit
7c0be43c0d
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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));
|
||||
}
|
||||
|
||||
|
@ -143,8 +160,7 @@ public class PostController {
|
|||
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")
|
||||
|
|
|
@ -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<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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -216,8 +216,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost> 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<POST extends BasePost> 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);
|
||||
|
||||
|
|
|
@ -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<Journal, Integer> 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.
|
||||
|
|
Loading…
Reference in New Issue