update user command

This commit is contained in:
AndrewZuo01 2023-12-12 12:21:26 +08:00
parent 48c7984e5c
commit 72ef3d1a7f
4 changed files with 18 additions and 27 deletions

3
go.mod
View File

@ -154,5 +154,8 @@ require (
golang.org/x/crypto v0.14.0 // indirect
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
)

View File

@ -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]string, error)
GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, 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]string, error) {
func (u *userDatabase) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) {
commands, err := u.userDB.GetUserCommands(ctx, userID, Type)
return commands, err
}

View File

@ -2,6 +2,7 @@ package mgo
import (
"context"
"github.com/OpenIMSDK/protocol/user"
"time"
"github.com/OpenIMSDK/tools/mgoutil"
@ -74,24 +75,22 @@ func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int6
}
type UserCommand struct {
UserID string `bson:"userID"`
Type int32 `bson:"type"`
Commands map[string]string `bson:"commands"`
UserID string `bson:"userID"`
Type int32 `bson:"type"`
Commands map[string]user.CommandInfo `bson:"commands"`
}
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
collection := u.coll.Database().Collection("userCommands")
filter := bson.M{"userID": userID, "type": Type}
update := bson.M{"$set": bson.M{"commands." + UUID: value}}
update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}}
options := options.Update().SetUpsert(true)
_, err := collection.UpdateOne(ctx, filter, update, options)
if err != nil {
return err
}
return nil
return err
}
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
collection := u.coll.Database().Collection("userCommands")
@ -99,30 +98,18 @@ func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int
update := bson.M{"$unset": bson.M{"commands." + UUID: ""}}
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
return err
}
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
collection := u.coll.Database().Collection("userCommands")
// Create a filter to identify the document
filter := bson.M{"userID": userID, "type": Type}
update := bson.M{"$set": bson.M{"commands." + UUID: user.CommandInfo{Time: time.Now().Unix(), Value: value}}}
// Create an update statement to set the new value for the command
update := bson.M{"$set": bson.M{"commands." + UUID: value}}
// Perform the update operation
_, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
return nil
return err
}
func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]string, error) {
func (u *UserMgo) GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error) {
collection := u.coll.Database().Collection("userCommands")
filter := bson.M{"userID": userID, "type": Type}

View File

@ -16,6 +16,7 @@ package relation
import (
"context"
"github.com/OpenIMSDK/protocol/user"
"time"
"github.com/OpenIMSDK/tools/pagination"
@ -64,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]string, error)
GetUserCommands(ctx context.Context, userID string, Type int32) (map[string]user.CommandInfo, error)
}