feature/post #1

Open
daniel.w wants to merge 7 commits from feature/post into main
4 changed files with 172 additions and 9 deletions
Showing only changes of commit 518e1673fe - Show all commits

View File

@ -51,10 +51,9 @@ message UpdatePostReq {
// //
message QueryPostsReq { message QueryPostsReq {
repeated int64 user_id = 1; // ID篩選貼文 repeated string uid = 1; // ID篩選貼文
repeated int64 id = 2; // ID篩選貼文 repeated string id = 2; // ID篩選貼文
repeated string tags = 3; // optional int32 only_ads = 4; // 0 1 2
optional bool only_ads = 4; //
int32 page_index = 5; // int32 page_index = 5; //
int32 page_size = 6; // int32 page_size = 6; //
} }
@ -62,7 +61,7 @@ message QueryPostsReq {
// //
message PostDetailItem { message PostDetailItem {
string post_id = 1; // ID string post_id = 1; // ID
int64 user_id = 2; // ID string uid = 2; // ID
string content = 3; // string content = 3; //
repeated string tags = 4; // repeated string tags = 4; //
repeated Media media = 5; // URL repeated Media media = 5; // URL

13
internal/domain/const.go Normal file
View File

@ -0,0 +1,13 @@
package domain
type AdType int32
func (a AdType) ToInt32() int32 {
return int32(a)
}
const (
AdTypeAll AdType = iota
AdTypeOnlyAd
AdTypeOnlyNotAd
)

View File

@ -1,10 +1,13 @@
package postservicelogic package postservicelogic
import ( import (
"context"
"app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/domain"
model "app-cloudep-tweeting-service/internal/model/mongo"
"app-cloudep-tweeting-service/internal/svc" "app-cloudep-tweeting-service/internal/svc"
ers "code.30cm.net/digimon/library-go/errs"
"context"
"google.golang.org/protobuf/proto"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@ -23,9 +26,84 @@ func NewListPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPos
} }
} }
type listReq struct {
OnlyAdds int32 `json:"only_adds" validate:"oneof=0 1 2 3"`
PageSize int64 `json:"page_size" validate:"required"`
PageIndex int64 `json:"page_index" validate:"required"`
}
// ListPosts 查詢貼文 // ListPosts 查詢貼文
func (l *ListPostsLogic) ListPosts(in *tweeting.QueryPostsReq) (*tweeting.ListPostsResp, error) { func (l *ListPostsLogic) ListPosts(in *tweeting.QueryPostsReq) (*tweeting.ListPostsResp, error) {
// todo: add your logic here and delete this line // 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&listReq{
PageSize: int64(in.GetPageSize()),
PageIndex: int64(in.GetPageIndex()),
OnlyAdds: in.GetOnlyAds(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
return &tweeting.ListPostsResp{}, nil query := &model.QueryPostModelReq{
UID: in.GetUid(),
Id: in.GetId(),
PageSize: int64(in.GetPageSize()),
PageIndex: int64(in.GetPageIndex()),
}
if in.OnlyAds != nil {
switch in.GetOnlyAds() {
case domain.AdTypeOnlyAd.ToInt32():
query.OnlyAds = proto.Bool(true)
case domain.AdTypeOnlyNotAd.ToInt32():
query.OnlyAds = proto.Bool(false)
default:
}
}
find, count, err := l.svcCtx.PostModel.Find(l.ctx, query)
if err != nil {
e := domain.PostMongoErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "query", Value: query},
{Key: "func", Value: "PostModel.Find"},
{Key: "err", Value: err},
},
"failed to add new post").Wrap(err)
return nil, e
}
result := make([]*tweeting.PostDetailItem, 0, count)
for _, item := range find {
media := make([]*tweeting.Media, 0, len(item.Media))
for _, subItem := range item.Media {
media = append(media, &tweeting.Media{
Type: subItem.Type,
Url: subItem.Links,
})
}
result = append(result, &tweeting.PostDetailItem{
PostId: item.ID.Hex(),
Uid: item.UID,
Content: item.Content,
Tags: item.Tags,
Media: media,
IsAd: item.IsAd,
CreatedAt: item.CreateAt,
UpdateAt: item.UpdateAt,
LikeCount: int64(item.Like),
DislikeCount: int64(item.DisLike),
})
}
return &tweeting.ListPostsResp{
Posts: result,
Page: &tweeting.Pager{
Total: count,
Index: int64(in.GetPageIndex()),
Size: int64(in.GetPageSize()),
},
}, nil
} }

View File

@ -2,6 +2,7 @@ package model
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
@ -9,6 +10,7 @@ import (
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time" "time"
) )
@ -21,11 +23,20 @@ type (
postModel postModel
DeleteMany(ctx context.Context, id ...string) (int64, error) DeleteMany(ctx context.Context, id ...string) (int64, error)
UpdateOptional(ctx context.Context, data *Post) (*mongo.UpdateResult, error) UpdateOptional(ctx context.Context, data *Post) (*mongo.UpdateResult, error)
Find(ctx context.Context, param *QueryPostModelReq) ([]*Post, int64, error)
} }
customPostModel struct { customPostModel struct {
*defaultPostModel *defaultPostModel
} }
QueryPostModelReq struct {
UID []string
Id []string
OnlyAds *bool
PageSize int64
PageIndex int64
}
) )
// NewPostModel returns a model for the mongo. // NewPostModel returns a model for the mongo.
@ -118,3 +129,65 @@ func (m *defaultPostModel) UpdateOptional(ctx context.Context, data *Post) (*mon
return res, err return res, err
} }
// Find 貼文列表
func (m *defaultPostModel) Find(ctx context.Context, param *QueryPostModelReq) ([]*Post, int64, error) {
filter := bson.M{}
// 添加 UID 過濾條件
if len(param.UID) > 0 {
filter["uid"] = bson.M{"$in": param.UID}
}
// 添加 ID 過濾條件
if len(param.Id) > 0 {
var ids []primitive.ObjectID
for _, item := range param.Id {
oid, err := primitive.ObjectIDFromHex(item)
if err != nil {
// log
continue
}
ids = append(ids, oid)
}
filter["_id"] = bson.M{"$in": ids}
}
// 添加 OnlyAds 過濾條件
if param.OnlyAds != nil {
// true 是廣告 false 不是廣告 , 沒寫就是不過率
filter["is_ad"] = *param.OnlyAds
}
// 分頁處理
options := options.Find()
if param.PageSize > 0 {
options.SetLimit(param.PageSize)
}
if param.PageIndex > 0 {
options.SetSkip((param.PageIndex - 1) * param.PageSize)
}
// 計算總數(不考慮分頁)
totalCount, err := m.conn.CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}
result := make([]*Post, 0, param.PageSize)
// 執行查詢
err = m.conn.Find(ctx, &result, filter, options)
if err != nil {
return nil, 0, err
}
switch {
case err == nil:
return result, totalCount, nil
case errors.Is(err, monc.ErrNotFound):
return nil, 0, ErrNotFound
default:
return nil, 0, err
}
}