feat(comment thumbup&&unThumbup)

main
wangqing 10 months ago
parent b9f0abf617
commit b22d994d75

@ -6,6 +6,7 @@ import api.Result;
import api.StatusCode;
import com.luoo.comment.pojo.*;
import com.luoo.comment.service.CommentService;
import com.luoo.comment.service.LikeService;
import com.luoo.comment.util.IpUtil;
import constants.Constants;
import controller.BaseController;
@ -37,6 +38,9 @@ public class CommentController extends BaseController {
@Autowired
private CommentService commentService;
@Autowired
private LikeService likeService;
@Autowired
private RedisTemplate redisTemplate;
@ -137,7 +141,7 @@ public class CommentController extends BaseController {
@PutMapping("/thumbup/{commentId}")
public Result thumbup(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = true) String authorization) {
// 判断当前用户是否已经点赞
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
@ -145,15 +149,25 @@ public class CommentController extends BaseController {
}
String userId = userLoginDto.getUserId();
if(redisTemplate.opsForValue().get("thumbup_"+commentId+"_"+userId)!=null){
// if(redisTemplate.opsForValue().get("thumbup_"+commentId+"_"+userId)!=null){
//
// return Result.failed(StatusCode.COMMENT_REPEAT_THUMBUP);
// }
// 判断当前用户是否已经点赞
Integer flag = likeService.likePublication(commentId,userId,2);
if (flag==0) {
//当前用户已经点过赞,执行取消点赞
likeService.unlikePublication(commentId,userId,2);
return Result.success(null,"取消点赞成功");
}
return Result.failed(StatusCode.COMMENT_REPEAT_THUMBUP);
}
commentService.thumbup(commentId,userLoginDto);
redisTemplate.opsForValue().set("thumbup_"+commentId+"_"+userId,1,10, TimeUnit.SECONDS);
return Result.success();
// redisTemplate.opsForValue().set("thumbup_"+commentId+"_"+userId,1,10, TimeUnit.SECONDS);
return Result.success(null,"点赞成功");
}

@ -0,0 +1,65 @@
package com.luoo.comment.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
public class PublicationLike implements Serializable {
private String _id;
private Integer type; // 1 期刊 2 评论
private String likedItemId;
private String userId;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getLikedItemId() {
return likedItemId;
}
public void setLikedItemId(String likedItemId) {
this.likedItemId = likedItemId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}

@ -0,0 +1,66 @@
package com.luoo.comment.service;
import com.luoo.comment.pojo.PublicationLike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class LikeService {
@Autowired
private MongoTemplate mongoTemplate;
public Integer likePublication(String publicationId, String userId, Integer type) {
// 构建查询条件
Criteria criteria = Criteria.where("userId").is(userId)
.and("type").is(type)
.and("likedItemId").is(publicationId);
// 创建查询对象并应用查询条件
Query query = new Query(criteria);
boolean isExists = mongoTemplate.exists(query, PublicationLike.class);
if (isExists) {
// 重复点赞
return 0;
}
// 将点赞记录保存到mongodb
PublicationLike publicationLike = new PublicationLike();
publicationLike.setType(type);
publicationLike.setCreateTime(new Date());
publicationLike.setLikedItemId(publicationId);
publicationLike.setUserId(userId);
mongoTemplate.save(publicationLike);
return 1;
}
public void unlikePublication(String publicationId, String userId, Integer type) {
// 构建查询条件
Criteria criteria = Criteria.where("userId").is(userId)
.and("type").is(type)
.and("likedItemId").is(publicationId);
// 创建查询对象并应用查询条件
Query query = new Query(criteria);
boolean isExists = mongoTemplate.exists(query, PublicationLike.class);
if (isExists) {
// 未点赞过内容,无法取消点赞
}
// 从mongoDB中删除点赞记录
mongoTemplate.remove(query, PublicationLike.class);
Query query1 = new Query();
query.addCriteria(Criteria.where("_id").is(publicationId));
Update update = new Update();
update.inc("thumbupCount",-1);
mongoTemplate.updateFirst(query1,update,"comment");
}
}
Loading…
Cancel
Save