Skip to content

Commit

Permalink
fix: 修改searchUser为getUsers (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Feb 10, 2024
1 parent bc705f9 commit 4472a71
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
4 changes: 2 additions & 2 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func (s *ContentServerImpl) GetUser(ctx context.Context, req *content.GetUserReq
return s.UserService.GetUser(ctx, req)
}

func (s *ContentServerImpl) SearchUser(ctx context.Context, req *content.SearchUserReq) (resp *content.SearchUserResp, err error) {
return s.UserService.SearchUser(ctx, req)
func (s *ContentServerImpl) GetUsers(ctx context.Context, req *content.GetUsersReq) (resp *content.GetUsersResp, err error) {
return s.UserService.GetUsers(ctx, req)
}

func (s *ContentServerImpl) CreateUser(ctx context.Context, req *content.CreateUserReq) (resp *content.CreateUserResp, err error) {
Expand Down
22 changes: 14 additions & 8 deletions biz/application/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/convertor"
usermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/user"
"github.com/CloudStriver/go-pkg/utils/pagination/esp"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content"
"github.com/google/wire"
Expand All @@ -19,7 +20,7 @@ type IUserService interface {
GetUser(ctx context.Context, req *gencontent.GetUserReq) (resp *gencontent.GetUserResp, err error)
CreateUser(ctx context.Context, req *gencontent.CreateUserReq) (resp *gencontent.CreateUserResp, err error)
UpdateUser(ctx context.Context, req *gencontent.UpdateUserReq) (resp *gencontent.UpdateUserResp, err error)
SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error)
GetUsers(ctx context.Context, req *gencontent.GetUsersReq) (resp *gencontent.GetUsersResp, err error)
DeleteUser(ctx context.Context, req *gencontent.DeleteUserReq) (resp *gencontent.DeleteUserResp, err error)
}

Expand All @@ -42,19 +43,24 @@ func (s *UserService) DeleteUser(ctx context.Context, req *gencontent.DeleteUser
return resp, nil
}

func (s *UserService) SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error) {
resp = new(gencontent.SearchUserResp)
func (s *UserService) GetUsers(ctx context.Context, req *gencontent.GetUsersReq) (resp *gencontent.GetUsersResp, err error) {
resp = new(gencontent.GetUsersResp)
var (
users []*usermapper.User
total int64
)

p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
switch o := req.SearchOptions.Type.(type) {
case *gencontent.SearchOptions_AllFieldsKey:
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserAllFieldsSearchQuery(o), p, esp.ScoreCursorType)
case *gencontent.SearchOptions_MultiFieldsKey:
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserMultiFieldsSearchQuery(o), p, esp.ScoreCursorType)
if req.SearchOptions != nil {
switch o := req.SearchOptions.Type.(type) {
case *gencontent.SearchOptions_AllFieldsKey:
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserAllFieldsSearchQuery(o), p, esp.ScoreCursorType)
case *gencontent.SearchOptions_MultiFieldsKey:
users, total, err = s.UserEsMapper.Search(ctx, convertor.ConvertUserMultiFieldsSearchQuery(o), p, esp.ScoreCursorType)
}
} else {
users, err = s.UserMongoMapper.FindMany(ctx, convertor.UserFilterToUserFilterMapper(req.UserFilterOptions),
p, mongop.IdCursorType)
}
if err != nil {
return resp, err
Expand Down
9 changes: 9 additions & 0 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,12 @@ func FeedBackToGorseFeedBack(in *gencontent.FeedBack) gorse.Feedback {
Timestamp: time.Now().String(),
}
}

func UserFilterToUserFilterMapper(in *gencontent.UserFilterOptions) *usermapper.FilterOptions {
if in == nil {
return &usermapper.FilterOptions{}
}
return &usermapper.FilterOptions{
OnlyUserIds: in.UserIds,
}
}
40 changes: 40 additions & 0 deletions biz/infrastructure/mapper/user/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package user

import (
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type FilterOptions struct {
OnlyUserIds []string
}

type MongoFilter struct {
m bson.M
*FilterOptions
}

func MakeBsonFilter(options *FilterOptions) bson.M {
return (&MongoFilter{
m: bson.M{},
FilterOptions: options,
}).toBson()
}

func (f *MongoFilter) toBson() bson.M {
f.CheckOnlyUserIds()
return f.m
}

func (f *MongoFilter) CheckOnlyUserIds() {
if f.OnlyUserIds != nil {
f.m[consts.ID] = bson.M{
"$in": lo.Map[string, primitive.ObjectID](f.OnlyUserIds, func(s string, _ int) primitive.ObjectID {
oid, _ := primitive.ObjectIDFromHex(s)
return oid
}),
}
}
}
36 changes: 36 additions & 0 deletions biz/infrastructure/mapper/user/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"errors"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/stores/monc"
"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"
"time"
)

Expand All @@ -24,6 +28,7 @@ type (
FindOne(ctx context.Context, id string) (*User, error)
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)
}
User struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
Expand All @@ -50,6 +55,37 @@ func NewMongoMapper(config *config.Config) IUserMongoMapper {
}
}

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)
sort, err := p.MakeSortOptions(ctx, filter)
if err != nil {
return nil, err
}

var data []*User
if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{
Sort: sort,
Limit: popts.Limit,
Skip: popts.Offset,
}); err != nil {
return nil, err
}

// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(data)
}
if len(data) > 0 {
err = p.StoreCursor(ctx, data[0], data[len(data)-1])
if err != nil {
return nil, err
}
}
return data, nil
}

func (m *MongoMapper) Insert(ctx context.Context, data *User) (string, error) {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
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-20240210022809-ad9936dc5ed8
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210025902-fb577fd05341
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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209105344-32f0bd207ac6 h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209105344-32f0bd207ac6/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210022809-ad9936dc5ed8 h1:aq9QAEH/pgYLBJQEss3NHBGHbpdiWpyJLu8mj2AegIU=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210022809-ad9936dc5ed8/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210025902-fb577fd05341 h1:80Jkhgy/UWRq9uZ2jfxNfjRuPCsh5+uDZa/utPUfHzU=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210025902-fb577fd05341/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 4472a71

Please sign in to comment.