public async Task <IActionResult> Edit(Guid id)
        {
            var response = await _customPageService.GetPageAsync(id);

            if (response.IsSuccess)
            {
                if (response.Item == null)
                {
                    return(NotFound());
                }

                var model = new CustomPageEditViewModel
                {
                    Id             = response.Item.Id,
                    Title          = response.Item.Title,
                    RouteName      = response.Item.RouteName,
                    CssContent     = response.Item.CssContent,
                    RawHtmlContent = response.Item.RawHtmlContent,
                    HideSidebar    = response.Item.HideSidebar
                };

                return(View("CreateOrEdit", model));
            }
            return(ServerError());
        }
Exemple #2
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,
                        IsPublished     = model.IsPublished
                    };

                    var response = model.Id == Guid.Empty ?
                                   await _customPageService.CreateAsync(req) :
                                   await _customPageService.UpdateAsync(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)));
            }
        }
        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));
            }
        }
        public async Task <IActionResult> Create(CustomPageEditViewModel model)
        {
            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 CreateCustomPageRequest
                    {
                        HtmlContent = model.RawHtmlContent,
                        CssContent  = model.CssContent,
                        HideSidebar = model.HideSidebar,
                        RouteName   = model.RouteName,
                        Title       = model.Title
                    };

                    var response = await _customPageService.CreatePageAsync(req);

                    if (response.IsSuccess)
                    {
                        return(RedirectToAction("Manage"));
                    }

                    ModelState.AddModelError(string.Empty, response.Message);
                    return(View("CreateOrEdit", model));
                }
                return(View("CreateOrEdit", model));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error Creating CustomPage.");
                ModelState.AddModelError(string.Empty, e.Message);
                return(View("CreateOrEdit", model));
            }
        }
        public async Task <IActionResult> Edit(Guid id)
        {
            var page = await _customPageService.GetAsync(id);

            if (page == null)
            {
                return(NotFound());
            }

            var model = new CustomPageEditViewModel
            {
                Id              = page.Id,
                Title           = page.Title,
                Slug            = page.Slug,
                MetaDescription = page.MetaDescription,
                CssContent      = page.CssContent,
                RawHtmlContent  = page.RawHtmlContent,
                HideSidebar     = page.HideSidebar,
                IsPublished     = page.IsPublished
            };

            return(View("CreateOrEdit", model));
        }
        public IActionResult Create()
        {
            var model = new CustomPageEditViewModel();

            return(View("CreateOrEdit", model));
        }