update openim server remove duplicate code

pull/1532/head
AndrewZuo01 2 years ago
parent 31e8054cd8
commit c014ad173c

@ -378,14 +378,17 @@ func (s *userServer) ProcessUserCommandGet(ctx context.Context, req *pbuser.Proc
// Initialize commandInfoSlice as an empty slice // Initialize commandInfoSlice as an empty slice
commandInfoSlice := make([]*pbuser.CommandInfoResp, 0, len(commands)) commandInfoSlice := make([]*pbuser.CommandInfoResp, 0, len(commands))
for _, v := range 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
commandInfoSlice = append(commandInfoSlice, &pbuser.CommandInfoResp{ commandInfoSlice = append(commandInfoSlice, &pbuser.CommandInfoResp{
Uuid: v.Uuid, Uuid: command.Uuid,
Value: v.Value, Value: command.Value,
CreateTime: v.CreateTime, CreateTime: command.CreateTime,
}) })
} }
// Return the response with the slice, which is empty but not nil if there are no commands // Return the response with the slice
return &pbuser.ProcessUserCommandGetResp{KVArray: commandInfoSlice}, nil return &pbuser.ProcessUserCommandGetResp{KVArray: commandInfoSlice}, nil
} }

@ -106,7 +106,7 @@ func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int
_, err := collection.UpdateOne(ctx, filter, update) _, err := collection.UpdateOne(ctx, filter, update)
return err return err
} }
func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]user.CommandInfoResp, error) { func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) {
collection := u.coll.Database().Collection("userCommands") collection := u.coll.Database().Collection("userCommands")
filter := bson.M{"userID": userID, "type": Type} filter := bson.M{"userID": userID, "type": Type}
@ -116,8 +116,8 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32
} }
defer cursor.Close(ctx) defer cursor.Close(ctx)
// Initialize commands as an empty slice // Initialize commands as a slice of pointers
commands := []user.CommandInfoResp{} commands := []*user.CommandInfoResp{}
for cursor.Next(ctx) { for cursor.Next(ctx) {
var document struct { var document struct {
@ -130,7 +130,7 @@ func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32
return nil, err return nil, err
} }
commandInfo := user.CommandInfoResp{ commandInfo := &user.CommandInfoResp{ // Change here: use a pointer to the struct
Uuid: document.UUID, Uuid: document.UUID,
Value: document.Value, Value: document.Value,
CreateTime: document.CreateTime, CreateTime: document.CreateTime,

Loading…
Cancel
Save