mirror of https://github.com/halo-dev/halo
Complete post likes feature
parent
1daead943d
commit
cee5ccba69
|
@ -1,38 +0,0 @@
|
||||||
package run.halo.app.event;
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationEvent;
|
|
||||||
import run.halo.app.model.enums.LogType;
|
|
||||||
import run.halo.app.model.params.LogParam;
|
|
||||||
import run.halo.app.utils.ValidationUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author johnniang
|
|
||||||
* @date 19-4-20
|
|
||||||
*/
|
|
||||||
public class LogEvent extends ApplicationEvent {
|
|
||||||
|
|
||||||
private final LogParam logParam;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new ApplicationEvent.
|
|
||||||
*
|
|
||||||
* @param source the object on which the event initially occurred (never {@code null})
|
|
||||||
* @param logParam
|
|
||||||
*/
|
|
||||||
public LogEvent(Object source, LogParam logParam) {
|
|
||||||
super(source);
|
|
||||||
|
|
||||||
// Validate the log param
|
|
||||||
ValidationUtils.validate(logParam);
|
|
||||||
|
|
||||||
this.logParam = logParam;
|
|
||||||
}
|
|
||||||
|
|
||||||
public LogEvent(Object source, String logKey, LogType logType, String content) {
|
|
||||||
this(source, new LogParam(logKey, logType, content));
|
|
||||||
}
|
|
||||||
|
|
||||||
public LogParam getLogParam() {
|
|
||||||
return logParam;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
package run.halo.app.event;
|
|
||||||
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.scheduling.annotation.Async;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import run.halo.app.model.entity.Log;
|
|
||||||
import run.halo.app.service.LogService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log event listener.
|
|
||||||
*
|
|
||||||
* @author johnniang
|
|
||||||
* @date 19-4-21
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class LogEventListener {
|
|
||||||
|
|
||||||
private final LogService logService;
|
|
||||||
|
|
||||||
public LogEventListener(LogService logService) {
|
|
||||||
this.logService = logService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventListener
|
|
||||||
@Async
|
|
||||||
public void onApplicationEvent(LogEvent event) {
|
|
||||||
// Convert to log
|
|
||||||
Log logToCreate = event.getLogParam().convertTo();
|
|
||||||
// Create log
|
|
||||||
logService.create(logToCreate);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.event;
|
package run.halo.app.event.post;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.event;
|
package run.halo.app.event.post;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
|
@ -14,9 +14,10 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import run.halo.app.event.LogEvent;
|
import run.halo.app.event.log.LogEvent;
|
||||||
import run.halo.app.event.VisitEvent;
|
import run.halo.app.event.post.VisitEvent;
|
||||||
import run.halo.app.exception.AlreadyExistsException;
|
import run.halo.app.exception.AlreadyExistsException;
|
||||||
|
import run.halo.app.exception.BadRequestException;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
import run.halo.app.exception.ServiceException;
|
import run.halo.app.exception.ServiceException;
|
||||||
import run.halo.app.model.dto.CategoryOutputDTO;
|
import run.halo.app.model.dto.CategoryOutputDTO;
|
||||||
|
@ -348,7 +349,8 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
long affectedRows = postRepository.updateVisit(visits, postId);
|
long affectedRows = postRepository.updateVisit(visits, postId);
|
||||||
|
|
||||||
if (affectedRows != 1) {
|
if (affectedRows != 1) {
|
||||||
throw new ServiceException("Failed to increase visits " + visits + " for post with id " + postId);
|
log.error("Post with id: [{}] may not be found", postId);
|
||||||
|
throw new BadRequestException("Failed to increase visits " + visits + " for post with id " + postId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,7 +362,8 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
||||||
long affectedRows = postRepository.updateLikes(likes, postId);
|
long affectedRows = postRepository.updateLikes(likes, postId);
|
||||||
|
|
||||||
if (affectedRows != 1) {
|
if (affectedRows != 1) {
|
||||||
throw new ServiceException("Failed to increase likes " + likes + " for post with id " + postId);
|
log.error("Post with id: [{}] may not be found", postId);
|
||||||
|
throw new BadRequestException("Failed to increase likes " + likes + " for post with id " + postId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import run.halo.app.cache.StringCacheStore;
|
import run.halo.app.cache.StringCacheStore;
|
||||||
import run.halo.app.event.LogEvent;
|
import run.halo.app.event.log.LogEvent;
|
||||||
import run.halo.app.exception.BadRequestException;
|
import run.halo.app.exception.BadRequestException;
|
||||||
import run.halo.app.exception.NotFoundException;
|
import run.halo.app.exception.NotFoundException;
|
||||||
import run.halo.app.model.entity.User;
|
import run.halo.app.model.entity.User;
|
||||||
|
|
|
@ -82,6 +82,12 @@ public class PostController {
|
||||||
return postService.getDetailVoBy(postId);
|
return postService.getDetailVoBy(postId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("{postId:\\d+}/likes")
|
||||||
|
@ApiOperation("Likes a post")
|
||||||
|
public void like(@PathVariable("postId") Integer postId) {
|
||||||
|
postService.increaseLike(postId);
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public PostDetailVO createBy(@Valid @RequestBody PostParam postParam) {
|
public PostDetailVO createBy(@Valid @RequestBody PostParam postParam) {
|
||||||
// Convert to
|
// Convert to
|
||||||
|
|
|
@ -92,4 +92,10 @@ public class PostController {
|
||||||
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
|
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
|
||||||
return commentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
|
return commentService.pageWithParentVoBy(postId, PageRequest.of(page, optionService.getCommentPageSize(), sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("{postId:\\d+}/likes")
|
||||||
|
@ApiOperation("Likes a post")
|
||||||
|
public void like(@PathVariable("postId") Integer postId) {
|
||||||
|
postService.increaseLike(postId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import run.halo.app.event.LogEvent;
|
import run.halo.app.event.log.LogEvent;
|
||||||
import run.halo.app.exception.BadRequestException;
|
import run.halo.app.exception.BadRequestException;
|
||||||
import run.halo.app.model.entity.*;
|
import run.halo.app.model.entity.*;
|
||||||
import run.halo.app.model.enums.AttachmentType;
|
import run.halo.app.model.enums.AttachmentType;
|
||||||
|
|
|
@ -3,6 +3,8 @@ package run.halo.app.event;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import run.halo.app.event.log.LogEvent;
|
||||||
|
import run.halo.app.event.log.LogEventListener;
|
||||||
import run.halo.app.utils.ReflectionUtils;
|
import run.halo.app.utils.ReflectionUtils;
|
||||||
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
|
Loading…
Reference in New Issue