diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 0e4974c..6d8e03e 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -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,"点赞成功"); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/PublicationLike.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/PublicationLike.java new file mode 100644 index 0000000..1857442 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/PublicationLike.java @@ -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; + } +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java b/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java new file mode 100644 index 0000000..9c6c1aa --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java @@ -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"); + } +} diff --git a/luoo_music/pom.xml b/luoo_music/pom.xml index e6fbef3..d4417fd 100644 --- a/luoo_music/pom.xml +++ b/luoo_music/pom.xml @@ -60,9 +60,9 @@ - org.apache.tika - tika-core - 1.27 + ws.schild + jave-all-deps + 3.4.0 diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Comment.java b/luoo_music/src/main/java/com/luoo/music/pojo/Comment.java index 0784883..012c40b 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Comment.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/Comment.java @@ -1,7 +1,9 @@ package com.luoo.music.pojo; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import org.springframework.data.annotation.Id; +import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; @@ -15,6 +17,8 @@ public class Comment implements Serializable { // 评论内容 private String content; // 发布时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date publishTime; private String userId;