public async Task <IResultList <IAssetEntity> > QueryAsync(DomainId appId, DomainId?parentId, ClrQuery query)
        {
            using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery"))
            {
                try
                {
                    query = query.AdjustToModel();

                    var filter = query.BuildFilter(appId, parentId);

                    var assetCount = Collection.Find(filter).CountDocumentsAsync();
                    var assetItems =
                        Collection.Find(filter)
                        .QueryLimit(query)
                        .QuerySkip(query)
                        .QuerySort(query)
                        .ToListAsync();

                    var(items, total) = await AsyncHelper.WhenAll(assetItems, assetCount);

                    return(ResultList.Create <IAssetEntity>(total, items));
                }
                catch (MongoQueryException ex) when(ex.Message.Contains("17406"))
                {
                    throw new DomainException(T.Get("common.resultTooLarge"));
                }
            }
        }
Example #2
0
        public static ValidationContext CreateContext(
            Schema?schema                 = null,
            ValidationMode mode           = ValidationMode.Default,
            ValidationUpdater?updater     = null,
            ValidationAction action       = ValidationAction.Upsert,
            ResolvedComponents?components = null,
            DomainId?contentId            = null)
        {
            var context = new ValidationContext(
                TestUtils.DefaultSerializer,
                AppId,
                SchemaId,
                schema ?? new Schema(SchemaId.Name),
                components ?? ResolvedComponents.Empty,
                contentId ?? DomainId.NewGuid());

            context = context.WithMode(mode).WithAction(action);

            if (updater != null)
            {
                context = updater(context);
            }

            return(context);
        }
Example #3
0
        public async Task <IActionResult> GetEvents(string app, [FromQuery] DomainId?ruleId = null, [FromQuery] int skip = 0, [FromQuery] int take = 20)
        {
            var ruleEvents = await ruleEventsRepository.QueryByAppAsync(AppId, ruleId, skip, take);

            var response = RuleEventsDto.FromRuleEvents(ruleEvents, Resources);

            return(Ok(response));
        }
        public async Task Should_query_assets_by_default(DomainId?parentId)
        {
            var query = new ClrQuery();

            var assets = await QueryAsync(parentId, query);

            Assert.NotNull(assets);
        }
Example #5
0
        private ContentEntity CreateContentCore(ContentEntity content, DomainId?id = null)
        {
            content.Id        = id ?? DomainId.NewGuid();
            content.AppId     = appId;
            content.Created   = default;
            content.CreatedBy = actor;
            content.SchemaId  = schemaId;

            return(content);
        }
Example #6
0
        public static RulesDto FromRules(IEnumerable <IEnrichedRuleEntity> items, DomainId?runningRuleId, Resources resources)
        {
            var result = new RulesDto
            {
                Items = items.Select(x => RuleDto.FromRule(x, runningRuleId, resources)).ToArray()
            };

            result.RunningRuleId = runningRuleId;

            return(result.CreateLinks(resources, runningRuleId));
        }
        public async Task <IResultList <IAssetEntity> > QueryAsync(DomainId appId, DomainId?parentId, Q q)
        {
            using (Profiler.TraceMethod <MongoAssetRepository>("QueryAsyncByQuery"))
            {
                try
                {
                    if (q.Ids != null && q.Ids.Count > 0)
                    {
                        var filter = BuildFilter(appId, q.Ids.ToHashSet());

                        var assetEntities =
                            await Collection.Find(filter).SortByDescending(x => x.LastModified)
                            .QueryLimit(q.Query)
                            .QuerySkip(q.Query)
                            .ToListAsync();

                        long assetTotal = assetEntities.Count;

                        if (assetTotal >= q.Query.Take || q.Query.Skip > 0)
                        {
                            assetTotal = await Collection.Find(filter).CountDocumentsAsync();
                        }

                        return(ResultList.Create(assetTotal, assetEntities.OfType <IAssetEntity>()));
                    }
                    else
                    {
                        var query = q.Query.AdjustToModel();

                        var filter = query.BuildFilter(appId, parentId);

                        var assetEntities =
                            await Collection.Find(filter)
                            .QueryLimit(query)
                            .QuerySkip(query)
                            .QuerySort(query)
                            .ToListAsync();

                        long assetTotal = assetEntities.Count;

                        if (assetTotal >= q.Query.Take || q.Query.Skip > 0)
                        {
                            assetTotal = await Collection.Find(filter).CountDocumentsAsync();
                        }

                        return(ResultList.Create <IAssetEntity>(assetTotal, assetEntities));
                    }
                }
                catch (MongoQueryException ex) when(ex.Message.Contains("17406"))
                {
                    throw new DomainException(T.Get("common.resultTooLarge"));
                }
            }
        }
Example #8
0
        public async Task Should_query_assets_by_fileName(DomainId?parentId)
        {
            var query = new ClrQuery
            {
                Filter = F.Contains("FileName", _.RandomValue())
            };

            var assets = await QueryAsync(parentId, query);

            Assert.NotNull(assets);
        }
Example #9
0
        public async Task Should_query_assets_by_tags(DomainId?parentId)
        {
            var query = new ClrQuery
            {
                Filter = F.Eq("Tags", _.RandomValue())
            };

            var assets = await QueryAsync(parentId, query);

            Assert.NotNull(assets);
        }
Example #10
0
        public async Task <IActionResult> GetAssets(string app, [FromQuery] DomainId?parentId, [FromQuery] string?ids = null, [FromQuery] string?q = null)
        {
            var assets = await assetQuery.QueryAsync(Context, parentId, CreateQuery(ids, q), HttpContext.RequestAborted);

            var response = Deferred.Response(() =>
            {
                return(AssetsDto.FromAssets(assets, Resources));
            });

            return(Ok(response));
        }
Example #11
0
        public static ValueTask ValidateAsync(this IValidator validator, object?value, IList <string> errors,
                                              Schema?schema                 = null,
                                              ValidationMode mode           = ValidationMode.Default,
                                              ValidationUpdater?updater     = null,
                                              ValidationAction action       = ValidationAction.Upsert,
                                              ResolvedComponents?components = null,
                                              DomainId?contentId            = null)
        {
            var context = CreateContext(schema, mode, updater, action, components, contentId);

            return(validator.ValidateAsync(value, context, CreateFormatter(errors)));
        }
Example #12
0
        public async Task <IActionResult> PostUpsertAsset(string app, DomainId id, [FromQuery] DomainId?parentId, IFormFile file)
        {
            var assetFile = await CheckAssetFileAsync(file);

            var command = new UpsertAsset {
                File = assetFile, ParentId = parentId, AssetId = id
            };

            var response = await InvokeCommandAsync(command);

            return(Ok(response));
        }
        public async Task Should_query_assets_by_tags_and_name(DomainId?parentId)
        {
            var random = _.RandomValue();

            var query = new ClrQuery
            {
                Filter = F.And(F.Eq("Tags", random), F.Contains("FileName", random))
            };

            var assets = await QueryAsync(parentId, query);

            Assert.NotNull(assets);
        }
Example #14
0
        public static RuleDto FromRule(IEnrichedRuleEntity rule, DomainId?runningRuleId, Resources resources)
        {
            var result = new RuleDto();

            SimpleMapper.Map(rule, result);
            SimpleMapper.Map(rule.RuleDef, result);

            if (rule.RuleDef.Trigger != null)
            {
                result.Trigger = RuleTriggerDtoFactory.Create(rule.RuleDef.Trigger);
            }

            return(result.CreateLinks(resources, runningRuleId));
        }
        private async Task <IResultList <IAssetEntity> > QueryAsync(DomainId?parentId, ClrQuery query)
        {
            query.Top = 1000;

            query.Skip = 100;

            query.Sort = new List <SortNode>
            {
                new SortNode("LastModified", SortOrder.Descending)
            };

            var assets = await _.AssetRepository.QueryAsync(_.RandomAppId(), parentId, Q.Empty.WithQuery(query));

            return(assets);
        }
Example #16
0
        private RuleEventsDto CreateLinks(Resources resources, DomainId?ruleId)
        {
            var values = new { app = resources.App };

            AddSelfLink(resources.Url <RulesController>(x => nameof(x.GetEvents), values));

            if (ruleId != null)
            {
                var routeValeus = new { values.app, id = ruleId };

                AddDeleteLink("cancel", resources.Url <RulesController>(x => nameof(x.DeleteRuleEvents), routeValeus));
            }
            else
            {
                AddDeleteLink("cancel", resources.Url <RulesController>(x => nameof(x.DeleteEvents), values));
            }

            return(this);
        }
Example #17
0
        public static async Task ValidateAsync(this ContentData data, PartitionResolver partitionResolver, IList <ValidationError> errors,
                                               Schema?schema                 = null,
                                               ValidationMode mode           = ValidationMode.Default,
                                               ValidationUpdater?updater     = null,
                                               IValidatorsFactory?factory    = null,
                                               ValidationAction action       = ValidationAction.Upsert,
                                               ResolvedComponents?components = null,
                                               DomainId?contentId            = null)
        {
            var context = CreateContext(schema, mode, updater, action, components, contentId);

            var validator = new ValidatorBuilder(factory, context).ContentValidator(partitionResolver);

            await validator.ValidateInputAsync(data);

            foreach (var error in validator.Errors)
            {
                errors.Add(error);
            }
        }
Example #18
0
        private RuleDto CreateLinks(Resources resources, DomainId?runningRuleId)
        {
            var values = new { app = resources.App, id = Id };

            if (resources.CanDisableRule)
            {
                if (IsEnabled)
                {
                    AddPutLink("disable", resources.Url <RulesController>(x => nameof(x.DisableRule), values));
                }
                else
                {
                    AddPutLink("enable", resources.Url <RulesController>(x => nameof(x.EnableRule), values));
                }
            }

            if (resources.CanUpdateRule)
            {
                AddPutLink("update", resources.Url <RulesController>(x => nameof(x.PutRule), values));
            }

            if (resources.CanReadRuleEvents)
            {
                AddPutLink("trigger", resources.Url <RulesController>(x => nameof(x.TriggerRule), values));

                if (runningRuleId == null)
                {
                    AddPutLink("run", resources.Url <RulesController>(x => nameof(x.PutRuleRun), values));
                }

                AddGetLink("logs", resources.Url <RulesController>(x => nameof(x.GetEvents), values));
            }

            if (resources.CanDeleteRule)
            {
                AddDeleteLink("delete", resources.Url <RulesController>(x => nameof(x.DeleteRule), values));
            }

            return(this);
        }
Example #19
0
        private RulesDto CreateLinks(Resources resources, DomainId?runningRuleId)
        {
            var values = new { app = resources.App };

            AddSelfLink(resources.Url <RulesController>(x => nameof(x.GetRules), values));

            if (resources.CanCreateRule)
            {
                AddPostLink("create", resources.Url <RulesController>(x => nameof(x.PostRule), values));
            }

            if (resources.CanReadRuleEvents)
            {
                AddGetLink("events", resources.Url <RulesController>(x => nameof(x.GetEvents), values));

                if (runningRuleId != null)
                {
                    AddDeleteLink("run/cancel", resources.Url <RulesController>(x => nameof(x.DeleteRuleRun), values));
                }
            }

            return(this);
        }
        private async Task <IResultList <IAssetEntity> > QueryAsync(DomainId?parentId, ClrQuery clrQuery)
        {
            clrQuery.Top = 1000;

            clrQuery.Skip = 100;

            if (clrQuery.Sort.Count == 0)
            {
                clrQuery.Sort = new List <SortNode>
                {
                    new SortNode("LastModified", SortOrder.Descending),
                    new SortNode("Id", SortOrder.Descending)
                };
            }

            var q =
                Q.Empty
                .WithoutTotal()
                .WithQuery(clrQuery);

            var assets = await _.AssetRepository.QueryAsync(_.RandomAppId(), parentId, q);

            return(assets);
        }
Example #21
0
 private ContentEntity CreateContent(Status status, DomainId?id = null)
 {
     return(CreateContentCore(new ContentEntity {
         Status = status
     }, id));
 }
        public async Task <IResultList <IRuleEventEntity> > QueryByAppAsync(DomainId appId, DomainId?ruleId = null, int skip = 0, int take = 20)
        {
            var filter = Filter.Eq(x => x.AppId, appId);

            if (ruleId.HasValue && ruleId.Value != DomainId.Empty)
            {
                filter = Filter.And(filter, Filter.Eq(x => x.RuleId, ruleId.Value));
            }

            var taskForItems = Collection.Find(filter).Skip(skip).Limit(take).SortByDescending(x => x.Created).ToListAsync();
            var taskForCount = Collection.Find(filter).CountDocumentsAsync();

            var(items, total) = await AsyncHelper.WhenAll(taskForItems, taskForCount);

            return(ResultList.Create(total, items));
        }
        public async Task <IActionResult> PostContent(string app, string name, [FromBody] NamedContentData request, [FromQuery] bool publish = false, [FromQuery] DomainId?id = null)
        {
            var command = new CreateContent {
                Data = request.ToCleaned(), Publish = publish
            };

            if (id != null && id.Value != default && !string.IsNullOrWhiteSpace(id.Value.ToString()))
            {
                command.ContentId = id.Value;
            }

            var response = await InvokeCommandAsync(command);

            return(CreatedAtAction(nameof(GetContent), new { app, name, id = command.ContentId }, response));
        }
Example #24
0
 public async Task <IResultList <IContentEntity> > QueryAsync(IAppEntity app, ISchemaEntity schema, ClrQuery query, DomainId?referenced)
 {
     using (Profiler.TraceMethod <MongoContentRepository>("QueryAsyncByQuery"))
     {
         return(await queryContentsByQuery.DoAsync(app, schema, query, referenced, SearchScope.Published));
     }
 }
        private static FilterDefinition <MongoAssetFolderEntity> BuildFilter(DomainId appId, DomainId?parentId)
        {
            var filters = new List <FilterDefinition <MongoAssetFolderEntity> >
            {
                Filter.Eq(x => x.IndexedAppId, appId),
                Filter.Eq(x => x.IsDeleted, false)
            };

            if (parentId != null)
            {
                if (parentId == DomainId.Empty)
                {
                    filters.Add(
                        Filter.Or(
                            Filter.Exists(x => x.ParentId, false),
                            Filter.Eq(x => x.ParentId, DomainId.Empty)));
                }
                else
                {
                    filters.Add(Filter.Eq(x => x.ParentId, parentId.Value));
                }
            }

            return(Filter.And(filters));
        }
Example #26
0
        public async Task <IResultList <IContentEntity> > DoAsync(IAppEntity app, ISchemaEntity schema, ClrQuery query, DomainId?referenced, SearchScope scope)
        {
            Guard.NotNull(app, nameof(app));
            Guard.NotNull(schema, nameof(schema));
            Guard.NotNull(query, nameof(query));

            try
            {
                query = query.AdjustToModel(schema.SchemaDef);

                List <DomainId>?fullTextIds = null;

                if (!string.IsNullOrWhiteSpace(query.FullText))
                {
                    var searchFilter = SearchFilter.ShouldHaveSchemas(schema.Id);

                    fullTextIds = await indexer.SearchAsync(query.FullText, app, searchFilter, scope);

                    if (fullTextIds?.Count == 0)
                    {
                        return(ResultList.CreateFrom <IContentEntity>(0));
                    }
                }

                var filter = CreateFilter(schema.AppId.Id, schema.Id, fullTextIds, query, referenced);

                var contentCount = Collection.Find(filter).CountDocumentsAsync();
                var contentItems = FindContentsAsync(query, filter);

                var(items, total) = await AsyncHelper.WhenAll(contentItems, contentCount);

                foreach (var entity in items)
                {
                    entity.ParseData(schema.SchemaDef, converter);
                }

                return(ResultList.Create <IContentEntity>(total, items));
            }
            catch (MongoCommandException ex) when(ex.Code == 96)
            {
                throw new DomainException(T.Get("common.resultTooLarge"));
            }
            catch (MongoQueryException ex) when(ex.Message.Contains("17406"))
            {
                throw new DomainException(T.Get("common.resultTooLarge"));
            }
        }
Example #27
0
        private static FilterDefinition <MongoContentEntity> CreateFilter(DomainId appId, DomainId schemaId, ICollection <DomainId>?ids, ClrQuery?query, DomainId?referenced)
        {
            var filters = new List <FilterDefinition <MongoContentEntity> >
            {
                Filter.Eq(x => x.IndexedAppId, appId),
                Filter.Eq(x => x.IndexedSchemaId, schemaId),
                Filter.Ne(x => x.IsDeleted, true)
            };

            if (ids != null && ids.Count > 0)
            {
                var documentIds = ids.Select(x => DomainId.Combine(appId, x)).ToList();

                filters.Add(
                    Filter.Or(
                        Filter.AnyIn(x => x.ReferencedIds, documentIds),
                        Filter.In(x => x.DocumentId, documentIds)));
            }

            if (query?.Filter != null)
            {
                filters.Add(query.Filter.BuildFilter <MongoContentEntity>());
            }

            if (referenced != null)
            {
                filters.Add(Filter.AnyEq(x => x.ReferencedIds, referenced.Value));
            }

            return(Filter.And(filters));
        }
Example #28
0
        public async Task <IResultList <IRuleEventEntity> > QueryByAppAsync(DomainId appId, DomainId?ruleId = null, int skip = 0, int take = 20,
                                                                            CancellationToken ct            = default)
        {
            var filter = Filter.Eq(x => x.AppId, appId);

            if (ruleId != null && ruleId.Value != DomainId.Empty)
            {
                filter = Filter.And(filter, Filter.Eq(x => x.RuleId, ruleId.Value));
            }

            var ruleEventEntities = await Collection.Find(filter).Skip(skip).Limit(take).SortByDescending(x => x.Created).ToListAsync(ct);

            var ruleEventTotal = (long)ruleEventEntities.Count;

            if (ruleEventTotal >= take || skip > 0)
            {
                ruleEventTotal = await Collection.Find(filter).CountDocumentsAsync(ct);
            }

            return(ResultList.Create(ruleEventTotal, ruleEventEntities));
        }
Example #29
0
        public static FilterDefinition <MongoAssetEntity> BuildFilter(this ClrQuery query, DomainId appId, DomainId?parentId)
        {
            var filters = new List <FilterDefinition <MongoAssetEntity> >
            {
                Filter.Eq(x => x.IndexedAppId, appId),
                Filter.Eq(x => x.IsDeleted, false)
            };

            if (parentId.HasValue)
            {
                if (parentId == DomainId.Empty)
                {
                    filters.Add(
                        Filter.Or(
                            Filter.Exists(x => x.ParentId, false),
                            Filter.Eq(x => x.ParentId, DomainId.Empty)));
                }
                else
                {
                    filters.Add(Filter.Eq(x => x.ParentId, parentId.Value));
                }
            }

            var(filter, last) = query.BuildFilter <MongoAssetEntity>(false);

            if (filter != null)
            {
                if (last)
                {
                    filters.Add(filter);
                }
                else
                {
                    filters.Insert(0, filter);
                }
            }

            return(Filter.And(filters));
        }
Example #30
0
        public async Task <IResultList <IEnrichedAssetEntity> > QueryAsync(Context context, DomainId?parentId, Q q,
                                                                           CancellationToken ct = default)
        {
            Guard.NotNull(context, nameof(context));

            if (q == null)
            {
                return(EmptyAssets);
            }

            using (Profiler.TraceMethod <AssetQueryService>())
            {
                q = await queryParser.ParseAsync(context, q);

                var assets = await assetRepository.QueryAsync(context.App.Id, parentId, q, ct);

                if (q.Ids != null && q.Ids.Count > 0)
                {
                    assets = assets.SortSet(x => x.Id, q.Ids);
                }

                return(await TransformAsync(context, assets, ct));
            }
        }