parent
b9f0abf617
commit
b22d994d75
@ -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…
Reference in new issue