Merge branch 'main' into feature/tag240205

main
zhangjiabao 10 months ago
commit 25eecf77bf

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

@ -60,9 +60,9 @@
</dependency> </dependency>
<!-- 解析音乐文件获取音乐时长 --> <!-- 解析音乐文件获取音乐时长 -->
<dependency> <dependency>
<groupId>org.apache.tika</groupId> <groupId>ws.schild</groupId>
<artifactId>tika-core</artifactId> <artifactId>jave-all-deps</artifactId>
<version>1.27</version> <version>3.4.0</version>
</dependency> </dependency>
<!-- s3 sdk --> <!-- s3 sdk -->
<dependency> <dependency>

@ -1,7 +1,9 @@
package com.luoo.music.pojo; package com.luoo.music.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data; import lombok.Data;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -15,6 +17,8 @@ public class Comment implements Serializable {
// 评论内容 // 评论内容
private String content; 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 Date publishTime;
private String userId; private String userId;

Loading…
Cancel
Save