From e0d17df2481b71ebb373d12618eb3f9ba02a69a7 Mon Sep 17 00:00:00 2001 From: AndrewZuo01 Date: Tue, 12 Dec 2023 15:27:53 +0800 Subject: [PATCH] update user command --- go.mod | 3 +-- pkg/common/db/controller/user.go | 4 ++-- pkg/common/db/mgo/user.go | 31 +++++++++++++++++++++------- pkg/common/db/table/relation/user.go | 2 +- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 44103819b..5e301727b 100644 --- a/go.mod +++ b/go.mod @@ -155,7 +155,6 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect ) replace ( - github.com/OpenIMSDK/protocol v0.0.32 => github.com/AndrewZuo01/protocol v0.0.0-20231212040759-69dcf194366f - + github.com/OpenIMSDK/protocol v0.0.32 => github.com/AndrewZuo01/protocol v0.0.0-20231212071940-a88da967f0e2 ) diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 1cd2ff741..6ecec3dcb 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) (map[string]user.CommandInfo, 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) (map[string]user.CommandInfo, error) { +func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) { commands, err := u.userDB.GetUserCommands(ctx, userID, Type) return commands, err } diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 3fa893c48..79f77024b 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -109,7 +109,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) (map[string]user.CommandInfo, error) { +func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) { collection := u.coll.Database().Collection("userCommands") filter := bson.M{"userID": userID, "type": Type} @@ -119,17 +119,32 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32 } defer cursor.Close(ctx) - commands := make(map[string]user.CommandInfo) + var commands []user.CommandInfoResp + for cursor.Next(ctx) { - var result UserCommand // Assuming UserCommand includes a map or similar structure - err = cursor.Decode(&result) - if err != nil { - return nil, err + var commandInfo user.CommandInfoResp + + // Define a struct that represents your MongoDB document structure + var document struct { + UUID string `bson:"uuid"` + Value string `bson:"value"` + CreateTime int64 `bson:"createTime"` } - for key, command := range result.Commands { - commands[key] = command + if err := cursor.Decode(&document); err != nil { + return nil, err } + + // Populate the user.CommandInfoResp struct with the required fields + commandInfo.Uuid = document.UUID + commandInfo.Value = document.Value + commandInfo.CreateTime = document.CreateTime + + commands = append(commands, commandInfo) + } + + if err := cursor.Err(); err != nil { + return nil, err } return commands, nil diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index 256b00f8d..09a218e45 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) (map[string]user.CommandInfo, error) + GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) }