From 6b151f0106f019ec1c47fecdf60e65b4c5cb3b40 Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Wed, 13 Dec 2023 09:43:23 +0800 Subject: [PATCH] update user command get --- internal/rpc/user/user.go | 6 ++---- pkg/common/db/controller/user.go | 6 +++--- pkg/common/db/mgo/user.go | 2 +- pkg/common/db/table/relation/user.go | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 2ac2790ae..70e3aebe6 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -378,10 +378,8 @@ func (s *userServer) ProcessUserCommandGet(ctx context.Context, req *pbuser.Proc // Initialize commandInfoSlice as an empty slice commandInfoSlice := make([]*pbuser.CommandInfoResp, 0, len(commands)) - for i := range commands { - // Access each command using index to avoid copying the struct - command := &commands[i] // Change here: use a pointer to the struct to avoid copying - + for _, command := range commands { + // No need to use index since command is already a pointer commandInfoSlice = append(commandInfoSlice, &pbuser.CommandInfoResp{ Uuid: command.Uuid, Value: command.Value, diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 6ecec3dcb..433fb7ad3 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -73,7 +73,7 @@ type UserDatabase interface { AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error - GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) + GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) } type userDatabase struct { @@ -242,7 +242,7 @@ func (u *userDatabase) DeleteUserCommand(ctx context.Context, userID string, Typ func (u *userDatabase) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error { return u.userDB.UpdateUserCommand(ctx, userID, Type, UUID, value) } -func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) { - commands, err := u.userDB.GetUserCommands(ctx, userID, Type) +func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) { + commands, err := u.userDB.GetUserCommand(ctx, userID, Type) return commands, err } diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 9756e8e9a..0323a87aa 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -106,7 +106,7 @@ func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int _, err := collection.UpdateOne(ctx, filter, update) return err } -func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) { +func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) { collection := u.coll.Database().Collection("userCommands") filter := bson.M{"userID": userID, "type": Type} diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index 09a218e45..213a6b774 100644 --- a/pkg/common/db/table/relation/user.go +++ b/pkg/common/db/table/relation/user.go @@ -65,5 +65,5 @@ type UserModelInterface interface { AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error - GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) + GetUserCommand(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) }