package permission import ( "context" "fmt" "log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo/options" "git.selly.red/Selly-Modules/usermngmt/database" "git.selly.red/Selly-Modules/usermngmt/model" ) func findByID(ctx context.Context, id primitive.ObjectID) (model.DBPermission, error) { var ( doc model.DBPermission col = database.GetPermissionCol() ) err := col.FindOne(ctx, bson.M{"_id": id}).Decode(&doc) return doc, err } func create(ctx context.Context, doc model.DBPermission) error { var ( col = database.GetPermissionCol() ) _, err := col.InsertOne(ctx, doc) if err != nil { return fmt.Errorf("error when create permission: %s", err.Error()) } return nil } func updateOneByCondition(ctx context.Context, cond interface{}, payload interface{}) error { var ( col = database.GetPermissionCol() ) _, err := col.UpdateOne(ctx, cond, payload) if err != nil { return fmt.Errorf("error when update permission: %s", err.Error()) } return err } func deleteOneByCondition(ctx context.Context, cond interface{}) error { var ( col = database.GetPermissionCol() ) _, err := col.DeleteOne(ctx, cond) if err != nil { return fmt.Errorf("error when delete permission: %s", err.Error()) } return err } func findOneAndDelete(ctx context.Context, cond interface{}) (doc model.DBPermission, err error) { var ( col = database.GetPermissionCol() ) err = col.FindOneAndDelete(ctx, cond).Decode(&doc) if err != nil { return doc, fmt.Errorf("error when findOneAndDelete permission: %v", err) } return doc, nil } func findByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) { var ( col = database.GetPermissionCol() ) docs = make([]model.DBPermission, 0) cursor, err := col.Find(ctx, cond, opts...) if err != nil { return } defer cursor.Close(ctx) if err = cursor.All(ctx, &docs); err != nil { return } return } // countByCondition ... func countByCondition(ctx context.Context, cond interface{}) int64 { var ( col = database.GetPermissionCol() ) total, err := col.CountDocuments(ctx, cond) if err != nil { log.Println("permission - countByCondition", err) } return total } func isPermissionIDExisted(ctx context.Context, permissionID primitive.ObjectID) bool { var ( col = database.GetPermissionCol() ) // Find cond := bson.M{ "_id": permissionID, } total, err := col.CountDocuments(ctx, cond) if err != nil { return false } return total != 0 }