public async Task GetContent() { var page = await pageService.FindPageByPathAsync("test"); var edit = await pageContentService.BeginEditAsync(page); var contentData = (TestPageContent)await pageContentService.GetContentAsync(edit); Assert.NotNull(contentData); Assert.Equal("test", contentData.Title); }
async Task IAsyncActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { pageService = HttpContext.RequestServices.GetRequiredService <IPageService>(); pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>(); if (!Request.Query.TryGetValue("editId", out Microsoft.Extensions.Primitives.StringValues editIdValue) || !Guid.TryParse(editIdValue[0], out Guid editId)) { context.Result = BadRequest(); return; } editSession = await pageEditingService.FindEditByIdAsync(editId, HttpContext.RequestAborted); if (editSession == null) { context.Result = BadRequest(); return; } page = await pageService.FindPageByIdAsync(editSession.PageId); if (page == null) { context.Result = BadRequest(); return; } var content = await pageEditingService.GetContentAsync(editSession, HttpContext.RequestAborted); rootContentContext = new ContentContext(page, content, HttpContext.RequestServices, true); string modelPath = string.Empty; if (Request.Query.TryGetValue("path", out Microsoft.Extensions.Primitives.StringValues pathValue)) { modelPath = pathValue[0]; } contentContext = rootContentContext.Navigate(modelPath); if (contentContext == null) { context.Result = BadRequest(); return; } if (!Request.Query.TryGetValue("field", out Microsoft.Extensions.Primitives.StringValues fieldNameValue)) { context.Result = BadRequest(); return; } string fieldName = fieldNameValue[0]; if (!contentContext.Explorer.Metadata.TryGetField(fieldName, out field)) { context.Result = BadRequest(); return; } await next(); }
public async Task <IActionResult> GetFormAsync([FromQuery] Guid editId, [FromQuery] string modelPath) { var editSession = await pageContentService.FindEditByIdAsync(editId); if (editSession == null) { return(BadRequest()); } var page = await pageService.FindPageByIdAsync(editSession.PageId); if (page == null) { return(BadRequest()); } if (modelPath == null) { modelPath = string.Empty; } var pageContent = await pageContentService.GetContentAsync(editSession); var pageContentContext = new ContentContext(page, pageContent, HttpContext.RequestServices, true); var contentContext = pageContentContext.Navigate(modelPath); if (contentContext == null) { return(BadRequest()); } var formModel = new Models.PageContentForm { Path = new Models.PageContentPath { Name = contentContext.Explorer.Metadata.Name, Title = contentContext.Explorer.Metadata.Title, Index = contentContext.Explorer.Index, ModelPath = contentContext.Explorer.ModelPath } }; var path = formModel.Path; var explorer = contentContext.Explorer.Parent; while (explorer != null) { path.Parent = new Models.PageContentPath { Name = explorer.Metadata.Name, Title = explorer.Metadata.Title, Index = explorer.Index, ModelPath = explorer.ModelPath }; explorer = explorer.Parent; path = path.Parent; } var fields = contentContext.Explorer.Metadata.Fields.ToList(); foreach (var field in fields) { formModel.Fields.Add(new Models.ContentFieldModel { Type = field.Type, Name = field.Name, Title = field.Title, Options = field.GetFormOptions(contentContext.Services) }); var modelValue = field.GetModelValue(contentContext.Content); var formValue = await field.GetFormValueAsync(modelValue, contentContext.Services); formModel.Values.Add(field.Name, formValue); } return(Ok(formModel)); }