app-cloudep-tweeting-service/internal/model/mongo/post_likes_model.go

196 lines
5.6 KiB
Go
Raw Permalink Normal View History

2024-08-28 14:29:42 +00:00
package model
2024-08-29 13:26:01 +00:00
import (
"app-cloudep-tweeting-service/internal/domain"
"context"
"errors"
2024-08-29 14:50:09 +00:00
"time"
2024-08-29 13:26:01 +00:00
"github.com/zeromicro/go-zero/core/stores/mon"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
2024-08-28 14:29:42 +00:00
var _ Post_likesModel = (*customPost_likesModel)(nil)
type (
// Post_likesModel is an interface to be customized, add more methods here,
// and implement the added methods in customPost_likesModel.
Post_likesModel interface {
post_likesModel
2024-08-29 14:49:42 +00:00
LikeDislike(ctx context.Context, postLike *PostLikes) (*PostReactionAction, error)
2024-08-29 13:26:01 +00:00
FindLikeUsers(ctx context.Context, param *QueryPostLikeReq) ([]*PostLikes, int64, error)
2024-08-29 13:49:47 +00:00
FindUIDPostLikeStatus(ctx context.Context, param *QueryUIDPostLikeStatusReq) ([]UIDPostLikeStatusResp, error)
2024-08-29 14:49:42 +00:00
Count(ctx context.Context, target string, likeType domain.LikeType) (int64, error)
}
PostReactionAction struct {
PostID string // 貼文的 ID
ReactionType domain.LikeType // 用戶的反應類型,可能是讚或不讚
IsIncrement bool // 表示是否增加true 表示增加false 表示減少)
2024-08-29 13:26:01 +00:00
}
QueryPostLikeReq struct {
Target string `bson:"target"`
LikeType domain.LikeType `bson:"like_type"`
PageIndex int64 `bson:"page_index"`
PageSize int64 `bson:"page_size"`
2024-08-28 14:29:42 +00:00
}
2024-08-29 13:49:47 +00:00
QueryUIDPostLikeStatusReq struct {
Targets []string `bson:"target"`
LikeType domain.LikeType `bson:"like_type"`
UID string
}
UIDPostLikeStatusResp struct {
TargetID string `json:"target_id"`
LikeStatus bool `json:"like_status"`
}
2024-08-28 14:29:42 +00:00
customPost_likesModel struct {
*defaultPost_likesModel
}
)
// NewPost_likesModel returns a model for the mongo.
func NewPost_likesModel(url, db, collection string) Post_likesModel {
conn := mon.MustNewModel(url, db, collection)
return &customPost_likesModel{
defaultPost_likesModel: newDefaultPost_likesModel(conn),
}
}
2024-08-29 13:26:01 +00:00
2024-08-29 14:49:42 +00:00
func (m *defaultPost_likesModel) LikeDislike(ctx context.Context, postLike *PostLikes) (*PostReactionAction, error) {
result := &PostReactionAction{
PostID: postLike.TargetID,
ReactionType: domain.LikeType(postLike.Type),
}
2024-08-29 13:26:01 +00:00
// 使用 target_id、uid、type 來查詢資料是否存在
filter := bson.M{
"target_id": postLike.TargetID,
"uid": postLike.UID,
"type": postLike.Type,
}
// 查詢資料是否存在
var existingPostLike PostLikes
err := m.conn.FindOne(ctx, &existingPostLike, filter)
if err == nil {
// 資料存在,進行刪除操作
2024-08-29 14:49:42 +00:00
result.IsIncrement = false
2024-08-29 13:26:01 +00:00
_, err = m.conn.DeleteOne(ctx, filter)
if err != nil {
2024-08-29 14:49:42 +00:00
return nil, err // 刪除失敗
2024-08-29 13:26:01 +00:00
}
2024-08-29 14:49:42 +00:00
return result, nil // 刪除成功
2024-08-29 13:26:01 +00:00
} else if errors.Is(mongo.ErrNoDocuments, err) {
// 資料不存在,進行插入操作
2024-08-29 14:49:42 +00:00
result.IsIncrement = true
2024-08-29 13:26:01 +00:00
postLike.ID = primitive.NewObjectID() // 設置新的 ObjectID
postLike.CreateAt = time.Now().UTC().UnixNano()
_, err = m.conn.InsertOne(ctx, postLike)
if err != nil {
2024-08-29 14:49:42 +00:00
return nil, err // 插入失敗
2024-08-29 13:26:01 +00:00
}
2024-08-29 14:49:42 +00:00
return result, nil // 插入成功
2024-08-29 13:26:01 +00:00
} else {
// 其他錯誤
2024-08-29 14:49:42 +00:00
return nil, err
2024-08-29 13:26:01 +00:00
}
}
func (m *defaultPost_likesModel) FindLikeUsers(ctx context.Context, param *QueryPostLikeReq) ([]*PostLikes, int64, error) {
// 建立篩選條件
filter := bson.M{}
// 如果指定了 Target 條件,將其添加到篩選條件中
if param.Target != "" {
filter["target_id"] = param.Target
}
// 如果指定了 LikeType 條件,將其添加到篩選條件中
if param.LikeType != 0 {
filter["type"] = param.LikeType
}
// 計算符合條件的文檔總數
totalCount, err := m.conn.CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}
// 設置分頁和排序選項
opts := options.Find()
opts.SetSort(bson.D{{"createAt", -1}}) // 按照創建時間倒序排序
if param.PageSize > 0 {
opts.SetLimit(param.PageSize)
}
if param.PageIndex > 0 && param.PageSize > 0 {
opts.SetSkip((param.PageIndex - 1) * param.PageSize)
}
// 查詢符合條件的文檔
var results []*PostLikes
err = m.conn.Find(ctx, &results, filter, opts)
if err != nil {
return nil, 0, err
}
// 返回結果集、總數和錯誤信息
return results, totalCount, nil
}
2024-08-29 13:49:47 +00:00
func (m *defaultPost_likesModel) FindUIDPostLikeStatus(ctx context.Context, param *QueryUIDPostLikeStatusReq) ([]UIDPostLikeStatusResp, error) {
// 初始化返回結果的切片
var results []UIDPostLikeStatusResp
// 建立篩選條件
filter := bson.M{
"uid": param.UID, // 篩選指定的 UID
"target_id": bson.M{"$in": param.Targets}, // 篩選多個目標 ID
"type": param.LikeType, // 篩選指定的 LikeType
}
// 查詢符合條件的點讚記錄
var postLikes []PostLikes
err := m.conn.Find(ctx, &postLikes, filter)
if err != nil {
return nil, err
}
// 構建一個 map 來保存查詢到的點讚記錄
targetLikeMap := make(map[string]bool)
for _, like := range postLikes {
targetLikeMap[like.TargetID] = true
}
// 構建每個目標的點讚狀態
for _, targetID := range param.Targets {
likeStatus := UIDPostLikeStatusResp{
TargetID: targetID,
LikeStatus: targetLikeMap[targetID], // 如果 map 中有該 targetID返回 true否則返回 false
}
// 如果該 targetID 沒有點讚記錄,默認 LikeStatus 為 false
if _, found := targetLikeMap[targetID]; !found {
likeStatus.LikeStatus = false
}
results = append(results, likeStatus)
}
return results, nil
}
2024-08-29 14:49:42 +00:00
func (c customPost_likesModel) Count(ctx context.Context, target string, likeType domain.LikeType) (int64, error) {
// TODO implement me
panic("implement me")
}