Skip to content

Commit

Permalink
fix: 修复通过帖子id查找的bug (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Feb 17, 2024
1 parent fb24c81 commit 061a3d6
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 30 deletions.
4 changes: 4 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 18 additions & 4 deletions biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,33 @@ 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
PostEsMapper postmapper.IEsMapper
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)
Expand Down
27 changes: 3 additions & 24 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{}
Expand Down
17 changes: 17 additions & 0 deletions biz/infrastructure/mapper/order/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions biz/infrastructure/mapper/post/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions biz/infrastructure/mapper/product/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions biz/infrastructure/mapper/user/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 061a3d6

Please sign in to comment.