diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index 937cf07..321d577 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -132,6 +132,10 @@ func (s *ContentServerImpl) CreatePost(ctx context.Context, req *content.CreateP return s.PostService.CreatePost(ctx, req) } +func (s *ContentServerImpl) GetPostsByPostIds(ctx context.Context, req *content.GetPostsByPostIdsReq) (res *content.GetPostsByPostIdsResp, err error) { + return s.PostService.GetPostsByPostIds(ctx, req) +} + func (s *ContentServerImpl) DeletePost(ctx context.Context, req *content.DeletePostReq) (res *content.DeletePostResp, err error) { return s.PostService.DeletePost(ctx, req) } diff --git a/biz/application/service/post.go b/biz/application/service/post.go index fbcc54e..902f29f 100644 --- a/biz/application/service/post.go +++ b/biz/application/service/post.go @@ -21,8 +21,14 @@ type IPostService interface { GetPosts(ctx context.Context, req *gencontent.GetPostsReq) (resp *gencontent.GetPostsResp, err error) UpdatePost(ctx context.Context, req *gencontent.UpdatePostReq) (resp *gencontent.UpdatePostResp, err error) DeletePost(ctx context.Context, req *gencontent.DeletePostReq) (resp *gencontent.DeletePostResp, err error) + GetPostsByPostIds(ctx context.Context, req *gencontent.GetPostsByPostIdsReq) (resp *gencontent.GetPostsByPostIdsResp, err error) } +var PostSet = wire.NewSet( + wire.Struct(new(PostService), "*"), + wire.Bind(new(IPostService), new(*PostService)), +) + type PostService struct { Config *config.Config PostMongoMapper postmapper.IPostMongoMapper @@ -30,10 +36,18 @@ type PostService struct { Redis *redis.Redis } -var PostSet = wire.NewSet( - wire.Struct(new(PostService), "*"), - wire.Bind(new(IPostService), new(*PostService)), -) +func (s *PostService) GetPostsByPostIds(ctx context.Context, req *gencontent.GetPostsByPostIdsReq) (resp *gencontent.GetPostsByPostIdsResp, err error) { + resp = new(gencontent.GetPostsByPostIdsResp) + posts, err := s.PostMongoMapper.FindManyByIds(ctx, req.PostIds) + if err != nil { + return resp, err + } + + resp.Posts = lo.Map[*postmapper.Post, *gencontent.Post](posts, func(item *postmapper.Post, _ int) *gencontent.Post { + return convertor.PostMapperToPost(item) + }) + return resp, nil +} func (s *PostService) CreatePost(ctx context.Context, req *gencontent.CreatePostReq) (resp *gencontent.CreatePostResp, err error) { resp = new(gencontent.CreatePostResp) diff --git a/biz/infrastructure/convertor/convertor.go b/biz/infrastructure/convertor/convertor.go index 7789a5f..51cd553 100644 --- a/biz/infrastructure/convertor/convertor.go +++ b/biz/infrastructure/convertor/convertor.go @@ -2,7 +2,6 @@ package convertor import ( "github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts" - "github.com/CloudStriver/cloudmind-content/biz/infrastructure/gorse" couponmapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/coupon" "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/file" ordermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/order" @@ -191,9 +190,9 @@ func PostFilterOptionsToFilterOptions(in *gencontent.PostFilterOptions) *postmap return &postmapper.FilterOptions{} } return &postmapper.FilterOptions{ - OnlyUserId: in.OnlyUserId, - OnlyPostId: in.OnlyPostId, - OnlyPostIds: in.OnlyPostIds, + OnlyUserId: in.OnlyUserId, + //OnlyPostId: in.OnlyPostId, + //OnlyPostIds: in.OnlyPostIds, OnlyTitle: in.OnlyTitle, OnlyText: in.OnlyText, OnlyTags: in.OnlyTags, @@ -607,26 +606,6 @@ func ConvertUserMultiFieldsSearchQuery(in *gencontent.SearchOptions_MultiFieldsK return q } -func ItemToGorseItem(in *gencontent.Item) gorse.Item { - return gorse.Item{ - ItemId: in.ItemId, - IsHidden: in.IsHidden, - Labels: in.Labels, - Categories: []string{in.Category}, - Timestamp: time.Now().String(), - Comment: in.Comment, - } -} - -func FeedBackToGorseFeedBack(in *gencontent.FeedBack) gorse.Feedback { - return gorse.Feedback{ - FeedbackType: in.FeedbackType, - UserId: in.UserId, - ItemId: in.ItemId, - Timestamp: time.Now().String(), - } -} - func UserFilterToUserFilterMapper(in *gencontent.UserFilterOptions) *usermapper.FilterOptions { if in == nil { return &usermapper.FilterOptions{} diff --git a/biz/infrastructure/mapper/order/mongo.go b/biz/infrastructure/mapper/order/mongo.go index 58054f1..5dd85f4 100644 --- a/biz/infrastructure/mapper/order/mongo.go +++ b/biz/infrastructure/mapper/order/mongo.go @@ -24,6 +24,7 @@ var _ IOrderMongoMapper = (*MongoMapper)(nil) type ( IOrderMongoMapper interface { + FindManyByIds(ctx context.Context, ids []string) ([]*Order, error) Insert(ctx context.Context, data *Order) error FindOne(ctx context.Context, fopts *FilterOptions) (*Order, error) Update(ctx context.Context, data *Order) error @@ -56,6 +57,22 @@ func NewMongoMapper(config *config.Config) IOrderMongoMapper { } } +func (m *MongoMapper) FindManyByIds(ctx context.Context, ids []string) ([]*Order, error) { + var ( + orders []*Order + err error + ) + + fopts := &FilterOptions{OnlyOrderIds: ids} + filter := MakeBsonFilter(fopts) + if err = m.conn.Find(ctx, &orders, filter, &options.FindOptions{ + Limit: lo.ToPtr(int64(len(ids))), + }); err != nil { + return nil, err + } + return orders, nil +} + func (m *MongoMapper) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Order, error) { p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) diff --git a/biz/infrastructure/mapper/post/mongo.go b/biz/infrastructure/mapper/post/mongo.go index 662c050..253a4bf 100644 --- a/biz/infrastructure/mapper/post/mongo.go +++ b/biz/infrastructure/mapper/post/mongo.go @@ -30,6 +30,7 @@ type ( FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Post, error) Count(ctx context.Context, fopts *FilterOptions) (int64, error) FindManyAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Post, int64, error) + FindManyByIds(ctx context.Context, ids []string) ([]*Post, error) } MongoMapper struct { @@ -58,6 +59,22 @@ func NewMongoMapper(config *config.Config) IPostMongoMapper { } } +func (m *MongoMapper) FindManyByIds(ctx context.Context, ids []string) ([]*Post, error) { + var ( + posts []*Post + err error + ) + + fopts := &FilterOptions{OnlyPostIds: ids} + filter := MakeBsonFilter(fopts) + if err = m.conn.Find(ctx, &posts, filter, &options.FindOptions{ + Limit: lo.ToPtr(int64(len(ids))), + }); err != nil { + return nil, err + } + return posts, nil +} + func (m *MongoMapper) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Post, error) { p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) diff --git a/biz/infrastructure/mapper/product/mongo.go b/biz/infrastructure/mapper/product/mongo.go index 39465f3..a91fac2 100644 --- a/biz/infrastructure/mapper/product/mongo.go +++ b/biz/infrastructure/mapper/product/mongo.go @@ -28,6 +28,7 @@ type ( FindOne(ctx context.Context, id string) (*Product, error) Update(ctx context.Context, data *Product) error Delete(ctx context.Context, id string) error + FindManyByIds(ctx context.Context, ids []string) ([]*Product, error) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Product, error) Count(ctx context.Context, fopts *FilterOptions) (int64, error) FindManyAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Product, int64, error) @@ -60,6 +61,21 @@ func NewMongoMapper(config *config.Config) IProductMongoMapper { } } +func (m *MongoMapper) FindManyByIds(ctx context.Context, ids []string) ([]*Product, error) { + var ( + products []*Product + err error + ) + + fopts := &FilterOptions{OnlyProductIds: ids} + filter := MakeBsonFilter(fopts) + if err = m.conn.Find(ctx, &products, filter, &options.FindOptions{ + Limit: lo.ToPtr(int64(len(ids))), + }); err != nil { + return nil, err + } + return products, nil +} func (m *MongoMapper) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Product, error) { p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) diff --git a/biz/infrastructure/mapper/user/mongo.go b/biz/infrastructure/mapper/user/mongo.go index bf4efc1..9e00b79 100644 --- a/biz/infrastructure/mapper/user/mongo.go +++ b/biz/infrastructure/mapper/user/mongo.go @@ -29,6 +29,7 @@ type ( Update(ctx context.Context, data *User) (*mongo.UpdateResult, error) Delete(ctx context.Context, id string) (int64, error) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*User, error) + FindManyByIds(ctx context.Context, ids []string) ([]*User, error) } User struct { ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` @@ -56,6 +57,22 @@ func NewMongoMapper(config *config.Config) IUserMongoMapper { } } +func (m *MongoMapper) FindManyByIds(ctx context.Context, ids []string) ([]*User, error) { + var ( + users []*User + err error + ) + + fopts := &FilterOptions{OnlyUserIds: ids} + filter := MakeBsonFilter(fopts) + if err = m.conn.Find(ctx, &users, filter, &options.FindOptions{ + Limit: lo.ToPtr(int64(len(ids))), + }); err != nil { + return nil, err + } + return users, nil +} + func (m *MongoMapper) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*User, error) { p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts) filter := MakeBsonFilter(fopts) diff --git a/go.mod b/go.mod index 1461fe7..0d15966 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217020757-af1570b246dc + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217031415-a5bd5496cc27 github.com/bytedance/sonic v1.10.2 github.com/cloudwego/kitex v0.8.0 github.com/elastic/go-elasticsearch/v8 v8.11.1 diff --git a/go.sum b/go.sum index b901607..93e02e5 100644 --- a/go.sum +++ b/go.sum @@ -29,7 +29,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240216034153-d1b5d05e4bc4 h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240216034153-d1b5d05e4bc4/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217020757-af1570b246dc h1:dNH83A+pRHSQNV7xfROtcFSuNJqxylhpPAmHw9abuTA= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217020757-af1570b246dc/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= - +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217031415-a5bd5496cc27 h1:l87gE+ctothfoCCtiS9DKNtcMTPUAUhIDsxwa/WS6BI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240217031415-a5bd5496cc27/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY= github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=