update user command

pull/1532/head
AndrewZuo01 2 years ago
parent 48c7984e5c
commit 72ef3d1a7f

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

@ -73,7 +73,7 @@ type UserDatabase interface {
AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error
DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID 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 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 { 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 { 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) 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) commands, err := u.userDB.GetUserCommands(ctx, userID, Type)
return commands, err return commands, err
} }

@ -2,6 +2,7 @@ package mgo
import ( import (
"context" "context"
"github.com/OpenIMSDK/protocol/user"
"time" "time"
"github.com/OpenIMSDK/tools/mgoutil" "github.com/OpenIMSDK/tools/mgoutil"
@ -76,22 +77,20 @@ func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int6
type UserCommand struct { type UserCommand struct {
UserID string `bson:"userID"` UserID string `bson:"userID"`
Type int32 `bson:"type"` Type int32 `bson:"type"`
Commands map[string]string `bson:"commands"` Commands map[string]user.CommandInfo `bson:"commands"`
} }
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error { func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) 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}
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) options := options.Update().SetUpsert(true)
_, err := collection.UpdateOne(ctx, filter, update, options) _, err := collection.UpdateOne(ctx, filter, update, options)
if err != nil {
return err return err
}
return nil
} }
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error { func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
collection := u.coll.Database().Collection("userCommands") 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: ""}} update := bson.M{"$unset": bson.M{"commands." + UUID: ""}}
_, err := collection.UpdateOne(ctx, filter, update) _, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err return err
}
return nil
} }
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error { func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
collection := u.coll.Database().Collection("userCommands") collection := u.coll.Database().Collection("userCommands")
// Create a filter to identify the document
filter := bson.M{"userID": userID, "type": Type} 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) _, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return err return err
}
return nil
} }
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) (map[string]string, 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}

@ -16,6 +16,7 @@ package relation
import ( import (
"context" "context"
"github.com/OpenIMSDK/protocol/user"
"time" "time"
"github.com/OpenIMSDK/tools/pagination" "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 AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error
DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID 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 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)
} }

Loading…
Cancel
Save