Exemplo n.º 1
0
        public async Task <IActionResult> CreatePost(ShortcodeTemplateViewModel model, string submit)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageShortcodeTemplates))
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(model.Name))
                {
                    ModelState.AddModelError(nameof(ShortcodeTemplateViewModel.Name), S["The name is mandatory."]);
                }
                else if (String.IsNullOrEmpty(model.Content))
                {
                    ModelState.AddModelError(nameof(ShortcodeTemplateViewModel.Content), S["The template content is mandatory."]);
                }
                else if (!_liquidTemplateManager.Validate(model.Content, out var errors))
                {
                    ModelState.AddModelError(nameof(ShortcodeTemplateViewModel.Content), S["The template doesn't contain a valid Liquid expression. Details: {0}", string.Join(" ", errors)]);
                }
                else
                {
                    var shortcodeTemplatesDocument = await _shortcodeTemplatesManager.GetShortcodeTemplatesDocumentAsync();

                    if (shortcodeTemplatesDocument.ShortcodeTemplates.ContainsKey(model.Name))
                    {
                        ModelState.AddModelError(nameof(ShortcodeTemplateViewModel.Name), S["A template with the same name already exists."]);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var template = new ShortcodeTemplate
                {
                    Content      = model.Content,
                    Hint         = model.Hint,
                    Usage        = model.Usage,
                    DefaultValue = model.DefaultValue,
                    Categories   = JsonConvert.DeserializeObject <string[]>(model.SelectedCategories)
                };

                await _shortcodeTemplatesManager.UpdateShortcodeTemplateAsync(model.Name, template);

                if (submit == "SaveAndContinue")
                {
                    return(RedirectToAction(nameof(Edit), new { name = model.Name }));
                }
                else
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task ExecuteAsync(RecipeExecutionContext context)
        {
            if (!String.Equals(context.Name, "ShortcodeTemplates", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (context.Step.Property("ShortcodeTemplates").Value is JObject templates)
            {
                foreach (var property in templates.Properties())
                {
                    var name  = property.Name;
                    var value = property.Value.ToObject <ShortcodeTemplate>();

                    await _templatesManager.UpdateShortcodeTemplateAsync(name, value);
                }
            }
        }