Пример #1
0
        public async Task <IActionResult> CreateOrEdit(CustomPageEditViewModel model, [FromServices] IMemoryCache cache)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (InvalidPageRouteNames.Contains(model.Slug.ToLower()))
                    {
                        ModelState.AddModelError(nameof(model.Slug), "Reserved Slug.");
                        return(View("CreateOrEdit", model));
                    }

                    var req = new EditCustomPageRequest(model.Id)
                    {
                        HtmlContent     = model.RawHtmlContent,
                        CssContent      = model.CssContent,
                        HideSidebar     = model.HideSidebar,
                        Slug            = model.Slug,
                        MetaDescription = model.MetaDescription,
                        Title           = model.Title
                    };

                    var response = model.Id == Guid.Empty ?
                                   await _customPageService.CreatePageAsync(req) :
                                   await _customPageService.EditPageAsync(req);

                    if (response.IsSuccess)
                    {
                        Logger.LogInformation($"User '{User.Identity.Name}' updated custom page id '{response.Item}'");

                        var cacheKey = $"page-{req.Slug.ToLower()}";
                        cache.Remove(cacheKey);

                        return(Json(new { PageId = response.Item }));
                    }

                    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    return(Json(new FailedResponse(response.Message)));
                }

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new FailedResponse("Invalid ModelState")));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error Create or Edit CustomPage.");
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json(new FailedResponse(e.Message)));
            }
        }
Пример #2
0
        public async Task <IActionResult> Edit(CustomPageEditViewModel model, [FromServices] IMemoryCache cache)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (InvalidPageRouteNames.Contains(model.RouteName.ToLower()))
                    {
                        ModelState.AddModelError(nameof(model.RouteName), "Reserved Route Name.");
                        return(View("CreateOrEdit", model));
                    }

                    var req = new EditCustomPageRequest(model.Id)
                    {
                        HtmlContent = model.RawHtmlContent,
                        CssContent  = model.CssContent,
                        HideSidebar = model.HideSidebar,
                        RouteName   = model.RouteName,
                        Title       = model.Title
                    };

                    var response = await _customPageService.EditPageAsync(req);

                    if (response.IsSuccess)
                    {
                        var cacheKey = $"page-{req.RouteName.ToLower()}";
                        cache.Remove(cacheKey);

                        return(RedirectToAction("Manage"));
                    }

                    ModelState.AddModelError(string.Empty, response.Message);
                    return(View("CreateOrEdit", model));
                }
                return(View("CreateOrEdit", model));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error Editing CustomPage.");
                ModelState.AddModelError(string.Empty, e.Message);
                return(View("CreateOrEdit", model));
            }
        }