Ejemplo n.º 1
0
 public async Task <bool> Exists(Expression <Func <T, bool> > predicate)
 {
     using (IAsyncCursor <T> result = await _collection.FindAsync(predicate).ConfigureAwait(false))
     {
         return(await result.AnyAsync().ConfigureAwait(false));
     }
 }
        //Check
        public async Task <Boolean> isExisted(string id)
        {
            IAsyncCursor <Area> cursor = await AreaCollection
                                         .FindAsync(Builders <Area> .Filter.Eq("_id", id));

            return(await cursor.AnyAsync());
        }
Ejemplo n.º 3
0
        public async Task <Role> CreateRole(int realm, string resume_json)
        {
            try
            {
                FilterDefinitionBuilder <Realm> builder = Builders <Realm> .Filter;
                FilterDefinition <Realm>        filter  = builder.Eq(nameof(Realm.id), realm);
                IAsyncCursor <Realm>            result  = await RealmCollection.FindAsync(filter);

                if (!(await result.AnyAsync()))
                {
                    _Logger.LogError($"realm {realm} is not exist, cant create role");
                    return(null);
                }

                Role role = new Role();
                role.origin_id   = IdGenerator.NewIdentity();
                role.user_id     = _UserId;
                role.realm_id    = realm;
                role.create_time = TimeUtils.Now;
                role.resume_json = resume_json;

                await RoleCollection.InsertOneAsync(role);

                return(role);
            }
            catch (Exception ex)
            {
                _Logger.LogError(ex, $"CreateRole Failed! {realm} {resume_json}");
            }

            return(null);
        }
Ejemplo n.º 4
0
        //Check
        public async Task <Boolean> isExisted(string id)
        {
            IAsyncCursor <TreeHistory> cursor = await repos
                                                .FindAsync(Builders <TreeHistory> .Filter.Eq("_id", id));

            return(await cursor.AnyAsync());
        }
        //Check
        public async Task <Boolean> isExisted(string id)
        {
            IAsyncCursor <TreeName> cursor = await TreeNameCollection
                                             .FindAsync(Builders <TreeName> .Filter.Eq("_id", new ObjectId(id)));

            return(await cursor.AnyAsync());
        }
Ejemplo n.º 6
0
        public async Task SetNodeType(long origin, NodeType node_type)
        {
            try
            {
                if (node_type == NodeType.None)
                {
                    throw new Exception($"origin {origin} set NodeType.None");
                }

                var filter = Builders <EntityList> .Filter.Eq(nameof(EntityList.origin), origin);

                IAsyncCursor <EntityList> cursor = await Collection.FindAsync(filter);

                if (await cursor.AnyAsync())
                {
                    return;
                }

                EntityList entities = new EntityList();
                entities.node     = node_type;
                entities.origin   = origin;
                entities.entities = new List <EntityChild>();
                await Collection.InsertOneAsync(entities);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "SetNodeType Failed");
            }
        }
        public async Task CreateCollectionsIfNotExist(IMongoDatabase db, string locale)
        {
            foreach (CollectionName collectionName in (CollectionName[])Enum.GetValues(typeof(CollectionName)))
            {
                IAsyncCursor <BsonDocument> collections = await db.ListCollectionsAsync(new ListCollectionsOptions
                {
                    Filter = new BsonDocument("name", collectionName.ToCollectionName())
                })
                                                          .AnyContext();

                if (!await collections.AnyAsync().AnyContext())
                {
                    await db.CreateCollectionAsync(collectionName.ToCollectionName(),
                                                   new CreateCollectionOptions { Collation = new Collation(locale, numericOrdering: true) })
                    .AnyContext();
                }

                IIndexBuilder builder;
                switch (collectionName)
                {
                case CollectionName.Addresses:
                    builder = new AddressIndexBuilder(db);
                    break;

                case CollectionName.AddressTransactions:
                    builder = new AddressTransactionIndexBuilder(db);
                    break;

                case CollectionName.Blocks:
                    builder = new BlockIndexBuilder(db);
                    break;

                case CollectionName.Contracts:
                    builder = new ContractIndexBuilder(db);
                    break;

                case CollectionName.Transactions:
                    builder = new TransactionIndexBuilder(db);
                    break;

                case CollectionName.TransactionLogs:
                    builder = new TransactionLogIndexBuilder(db);
                    break;

                case CollectionName.Promotions:
                    builder = new PromotionIndexBuilder(db);
                    break;

                default:
                    continue;
                }

                builder.EnsureIndexes();
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> GetInvitationsAsync(string user,
                                                              [FromServices] IMongoCollection <ParticipationRegistration> mongoCollection)
        {
            IAsyncCursor <ParticipationRegistration> result =
                await mongoCollection.FindAsync(
                    x => x.User == user && x.HasOwnerConfirmed && !x.HasParticipantConfirmed);

            if (!await result.AnyAsync())
            {
                return(this.Success(new List <object>()));
            }
            return(this.Success(await result.ToListAsync()));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the database collection.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <returns>The asynchronous operation resulting instance of <see cref="IMongoCollection{TDocument}"/>.</returns>
        private async Task <IMongoCollection <TDocument> > GetCollectionAsync <TDocument>()
            where TDocument : IDocument
        {
            Type type = typeof(TDocument);

            DocumentCollectionAttribute collectionAttribute = (type.GetCustomAttributes(typeof(DocumentCollectionAttribute), false)?.FirstOrDefault() as DocumentCollectionAttribute);
            string collectionName = collectionAttribute?.CollectionName ?? typeof(TDocument).Name;

            IAsyncCursor <BsonDocument> collections = await database.ListCollectionsAsync(
                new ListCollectionsOptions
            {
                Filter = new BsonDocument("name", collectionName)
            });

            bool isExists = await collections.AnyAsync();

            if (!isExists)
            {
                await database.CreateCollectionAsync(collectionName);
            }

            return(database.GetCollection <TDocument>(collectionName));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 判断是否存在符合条件的数据
        /// </summary>
        /// <param name="collName">表名</param>
        /// <param name="filter">条件</param>
        /// <returns></returns>
        public async Task <bool> AnyAsync(String collName, FilterDefinition <BsonDocument> filter)
        {
            IAsyncCursor <BsonDocument> find = await Database.GetCollection <BsonDocument>(collName).FindAsync(filter);

            return(await find.AnyAsync());
        }