Beispiel #1
0
        public async Task <Entity> ReadEntity(string tenantCode, int entityId)
        {
            if (entityId <= 0)
            {
                throw new ArgumentException("Invalid entity ID", nameof(entityId));
            }

            var entityCollection = GetEntityCollection(tenantCode);

            var dtNow  = DateTime.Now.Date;
            var entity = await entityCollection.Aggregate()
                         .Match(new BsonDocument
            {
                { "_id", entityId },
                { "IsDeleted", false }
            })
                         .Project <Entity>(new BsonDocument
            {
                { "ParentId", 1 },
                { "TemplateId", 1 },
                { "Mutations", BsonQuery.CreateMutationsFilter(dtNow) },
                {
                    "ChildEntities", BsonQuery.CreateChildEntitiesMapper(dtNow, true)
                }
            }
                                           ).SingleOrDefaultAsync();

            return(entity);
        }
Beispiel #2
0
        public async Task <IList <Entity> > ReadEntities(string tenantCode, int parentEntityId, int templateId = 21)
        {
            if (parentEntityId <= 0)
            {
                throw new ArgumentException("Invalid parent entity ID", nameof(parentEntityId));
            }

            var entityCollection = GetEntityCollection(tenantCode);

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var dtNow     = DateTime.Now.Date;
            var aggregate = entityCollection.Aggregate()
                            .Match(new BsonDocument
            {
                { "ParentId", parentEntityId },
                { "TemplateId", templateId },
                { "IsDeleted", false }
            })
                            .Project <Entity>(new BsonDocument
            {
                { "ParentId", 1 },
                { "TemplateId", 1 },
                { "Mutations", BsonQuery.CreateMutationsFilter(dtNow) },
                {
                    "ChildEntities", BsonQuery.CreateChildEntitiesMapper(dtNow, true)
                }
            }
                                              );

            var entities = await aggregate.ToListAsync();

            stopwatch.Stop();
            _logger.LogInformation($"Executed MongoDB query in {stopwatch.ElapsedMilliseconds} milliseconds.");

            return(entities);
        }