feature/post #1

Open
daniel.w wants to merge 7 commits from feature/post into main
3 changed files with 100 additions and 4 deletions
Showing only changes of commit 30fb3b3721 - Show all commits

View File

@ -1,6 +1,9 @@
package postservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
ers "code.30cm.net/digimon/library-go/errs"
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
@ -24,10 +27,49 @@ func NewGetLikeStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get
}
// 這個人按讚的文章列表輸入UID 以及文章id返回這個人有沒有對這些文章按讚
type getLikeStatusReq struct {
Targets []string `json:"targets" validate:"required"`
LikeType domain.LikeType `json:"like_type" validate:"required,oneof=1 2"`
UID string `json:"uid" validate:"required"`
}
// GetLikeStatus 取得讚/不讚狀態
func (l *GetLikeStatusLogic) GetLikeStatus(in *tweeting.LikeReq) (*tweeting.OKResp, error) {
// todo: add your logic here and delete this line
func (l *GetLikeStatusLogic) GetLikeStatus(in *tweeting.GetLikeStatusReq) (*tweeting.GetLikeStatusResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&getLikeStatusReq{
Targets: in.GetTargetId(),
LikeType: domain.LikeType(in.GetLikeType()),
UID: in.GetUid(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
return &tweeting.OKResp{}, nil
list, err := l.svcCtx.PostLikeModel.FindUIDPostLikeStatus(l.ctx, &model.QueryUIDPostLikeStatusReq{
Targets: in.GetTargetId(),
LikeType: domain.LikeType(in.GetLikeType()),
UID: in.GetUid(),
})
if err != nil {
e := domain.PostMongoErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "PostModel.FindUIDPostLikeStatus"},
{Key: "err", Value: err},
},
"failed to find uid post like status list").Wrap(err)
return nil, e
}
var result = make([]*tweeting.GetLikeStatusItem, 0, len(list))
for _, item := range list {
result = append(result, &tweeting.GetLikeStatusItem{
TargetId: item.TargetID,
Status: item.LikeStatus,
})
}
return &tweeting.GetLikeStatusResp{
Data: result,
}, nil
}

View File

@ -21,6 +21,7 @@ type (
post_likesModel
LikeDislike(ctx context.Context, postLike *PostLikes) error
FindLikeUsers(ctx context.Context, param *QueryPostLikeReq) ([]*PostLikes, int64, error)
FindUIDPostLikeStatus(ctx context.Context, param *QueryUIDPostLikeStatusReq) ([]UIDPostLikeStatusResp, error)
}
QueryPostLikeReq struct {
@ -30,6 +31,17 @@ type (
PageSize int64 `bson:"page_size"`
}
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"`
}
customPost_likesModel struct {
*defaultPost_likesModel
}
@ -117,3 +129,45 @@ func (m *defaultPost_likesModel) FindLikeUsers(ctx context.Context, param *Query
// 返回結果集、總數和錯誤信息
return results, totalCount, nil
}
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
}

View File

@ -53,7 +53,7 @@ func (s *PostServiceServer) Like(ctx context.Context, in *tweeting.LikeReq) (*tw
}
// GetLikeStatus 取得讚/不讚狀態
func (s *PostServiceServer) GetLikeStatus(ctx context.Context, in *tweeting.LikeReq) (*tweeting.OKResp, error) {
func (s *PostServiceServer) GetLikeStatus(ctx context.Context, in *tweeting.GetLikeStatusReq) (*tweeting.GetLikeStatusResp, error) {
l := postservicelogic.NewGetLikeStatusLogic(ctx, s.svcCtx)
return l.GetLikeStatus(in)
}