test-errcode
wangchuxiao 2 years ago
parent 36e82d0a6d
commit 870cc25beb

@ -89,7 +89,7 @@ func (g *GroupDataBase) Create(ctx context.Context, groups []*relation.Group) er
}
func (g *GroupDataBase) Delete(ctx context.Context, groupIDs []string) error {
err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
return g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
if err := g.sqlDB.Delete(ctx, groupIDs, tx); err != nil {
return err
}
@ -98,7 +98,6 @@ func (g *GroupDataBase) Delete(ctx context.Context, groupIDs []string) error {
}
return nil
})
return err
}
func (g *GroupDataBase) Take(ctx context.Context, groupID string) (group *relation.Group, err error) {
@ -106,7 +105,7 @@ func (g *GroupDataBase) Take(ctx context.Context, groupID string) (group *relati
}
func (g *GroupDataBase) Update(ctx context.Context, groups []*relation.Group) error {
err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
return g.sqlDB.DB.Transaction(func(tx *gorm.DB) error {
if err := g.sqlDB.Update(ctx, groups, tx); err != nil {
return err
}
@ -119,7 +118,6 @@ func (g *GroupDataBase) Update(ctx context.Context, groups []*relation.Group) er
}
return nil
})
return err
}
func (g *GroupDataBase) GetSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {

@ -62,10 +62,6 @@ func initMongo() *mongo.Database {
return mongoClient.Database(config.Config.Mongo.DBDatabase)
}
func GetCollection(mongoClient *mongo.Client) {
}
func CreateAllIndex(mongoClient *mongo.Client) {
// mongodb create index
if err := createMongoIndex(mongoClient, cSendLog, false, "send_id", "-send_time"); err != nil {

@ -1,15 +1,12 @@
package unrelation
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/utils"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
"time"
)
const (
@ -66,7 +63,7 @@ func (db *SuperGroupMgo) CreateSuperGroup(ctx context.Context, groupID string, i
return err
}
}
return sCtx.CommitTransaction(context.Background())
return sCtx.CommitTransaction(ctx)
})
}
@ -95,70 +92,51 @@ func (db *SuperGroupMgo) AddUserToSuperGroup(ctx context.Context, groupID string
return utils.Wrap(err, "transaction failed")
}
}
return sCtx.CommitTransaction(context.Background())
return sCtx.CommitTransaction(ctx)
})
}
func (d *SuperGroupMgo) RemoverUserFromSuperGroup(groupID string, userIDList []string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
_, err = c.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}})
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
err = d.RemoveGroupFromUser(ctx, sCtx, groupID, userIDList)
if err != nil {
_ = session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
_ = session.CommitTransaction(ctx)
return err
func (db *SuperGroupMgo) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDList []string) error {
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
return db.mgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}})
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
err = db.RemoveGroupFromUser(sCtx, groupID, userIDList)
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
return sCtx.CommitTransaction(ctx)
})
}
func (d *SuperGroupMgo) GetSuperGroupByUserID(userID string) (UserToSuperGroup, error) {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
func (db *SuperGroupMgo) GetSuperGroupByUserID(ctx context.Context, userID string) (*UserToSuperGroup, error) {
var user UserToSuperGroup
_ = c.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return user, nil
_ = db.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return &user, nil
}
func (d *SuperGroupMgo) DeleteSuperGroup(groupID string) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup)
session, err := d.mongoClient.StartSession()
if err != nil {
return utils.Wrap(err, "start session failed")
}
defer session.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, session)
superGroup := &SuperGroup{}
result := c.FindOneAndDelete(sCtx, bson.M{"group_id": groupID})
err = result.Decode(superGroup)
if err != nil {
session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
if err = d.RemoveGroupFromUser(ctx, sCtx, groupID, superGroup.MemberIDList); err != nil {
session.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
session.CommitTransaction(ctx)
return nil
func (db *SuperGroupMgo) DeleteSuperGroup(ctx context.Context, groupID string) error {
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
return db.mgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
superGroup := &SuperGroup{}
_, err := db.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
if err = db.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDList); err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
return sCtx.CommitTransaction(ctx)
})
}
func (d *SuperGroupMgo) RemoveGroupFromUser(ctx, sCtx context.Context, groupID string, userIDList []string) error {
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup)
_, err := c.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDList}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
if err != nil {
return utils.Wrap(err, "UpdateOne transaction failed")
}
func (db *SuperGroupMgo) RemoveGroupFromUser(sCtx context.Context, groupID string, userIDList []string) error {
_, err := db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDList}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
return err
}

Loading…
Cancel
Save