public ActionResult SubverseStylesheetEditor(string subverse)
        {
            var subverseObject = DataCache.Subverse.Retrieve(subverse);

            if (subverseObject == null)
            {
                ViewBag.SelectedSubverse = "404";
                return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound)));
            }
            if (!ModeratorPermission.HasPermission(User, subverse, Domain.Models.ModeratorAction.ModifyCSS))
            {
                return(RedirectToAction("Index", "Home"));
            }

            // map existing data to view model for editing and pass it to frontend
            var viewModel = new SubverseStylesheetViewModel
            {
                Name       = subverseObject.Name,
                Stylesheet = subverseObject.Stylesheet
            };

            ViewBag.SelectedSubverse = string.Empty;
            ViewBag.SubverseName     = subverseObject.Name;
            SetNavigationViewModel(subverseObject.Name);

            return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml", viewModel));
        }
        public async Task <ActionResult> SubverseStylesheetEditor(SubverseStylesheetViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    SetNavigationViewModel(model.Name);
                    return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml"));
                }
                var existingSubverse = _db.Subverse.FirstOrDefault(x => x.Name.ToUpper() == model.Name.ToUpper());

                // check if subverse exists before attempting to edit it
                if (existingSubverse != null)
                {
                    SetNavigationViewModel(model.Name);
                    // check if user requesting edit is authorized to do so for current subverse
                    // check that the user requesting to edit subverse settings is subverse owner!
                    if (!ModeratorPermission.HasPermission(User, existingSubverse.Name, Domain.Models.ModeratorAction.ModifyCSS))
                    {
                        return(new EmptyResult());
                    }

                    if (!String.IsNullOrEmpty(model.Stylesheet))
                    {
                        if (model.Stylesheet.Length < 50001)
                        {
                            existingSubverse.Stylesheet = model.Stylesheet;
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, "Sorry, custom CSS limit is set to 50000 characters.");
                            return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml"));
                        }
                    }
                    else
                    {
                        existingSubverse.Stylesheet = model.Stylesheet;
                    }

                    await _db.SaveChangesAsync();

                    //purge new minified CSS
                    CacheHandler.Instance.Remove(CachingKey.SubverseStylesheet(existingSubverse.Name));
                    CacheHandler.Instance.Remove(CachingKey.Subverse(existingSubverse.Name));

                    // go back to this subverse
                    return(RedirectToRoute(Models.ROUTE_NAMES.SUBVERSE_INDEX, new { subverse = model.Name }));
                }

                ModelState.AddModelError(string.Empty, "Sorry, The subverse you are trying to edit does not exist.");
                return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml", model));
            }
            catch (Exception ex)
            {
                EventLogger.Instance.Log(ex);
                ModelState.AddModelError(string.Empty, "Something bad happened.");
                return(View("~/Views/Subverses/Admin/SubverseStylesheetEditor.cshtml", model));
            }
        }