/// <summary> /// Allows administrator to view and edit help page specified by id. /// </summary> /// <param name="hPage"></param> /// <returns></returns> public async Task <IActionResult> EditHelpPage(int hPage, EditHelpPageViewModel viewModel) { //Update page before refresh. if (viewModel.WasEdited && ModelState.IsValid) { int result = await AdministrationServiceLogic.UpdateHelpPage(hPage, viewModel, context); if (result > 0) { ViewBag.Message = "Help page saved."; } else { switch (result) { case 0: ViewBag.Message = "An error occurred while updating help page"; break; case -1: ViewBag.Message = "Help page data is invalid"; break; case -2: ViewBag.Message = "Help page title is invalid (it should be character string shorter than " + IoCContainer.DbSettings.Value.MaxHelpPageTitleSize + ")."; break; case -3: ViewBag.Message = "Help page content is invalid (it should be character string shorter than " + IoCContainer.DbSettings.Value.MaxHelpPageSize + ")."; break; case -4: ViewBag.Message = "One of tags is invalid (it should be character string shorter than " + IoCContainer.DbSettings.Value.MaxTagLength + ")."; break; } } } HelpPage helpPage = await context.HelpPages.FindAsync(hPage); List <HelpTag> helpTags = SearchEngine.FindTagsForHelpPage(hPage, context); string helpTagsString = string.Empty; foreach (HelpTag tag in helpTags) { helpTagsString += (tag.Value + " "); } ViewData["pageTags"] = helpTagsString; ViewData["helpPage"] = helpPage; return(View()); }
/// <summary> /// Updates help page content. /// </summary> /// <param name="pageId"></param> /// <param name="newTitle"></param> /// <param name="newContent"></param> /// <param name="context"></param> /// <returns></returns> public static async Task <int> UpdateHelpPage(int pageId, EditHelpPageViewModel viewModel, AppDbContext context) { // Perform validation if (viewModel == null) { return(-1); } else if (string.IsNullOrEmpty(viewModel.Title) || viewModel.Title.Count() > IoCContainer.DbSettings.Value.MaxHelpPageTitleSize) { return(-2); } else if (string.IsNullOrEmpty(viewModel.Content) || viewModel.Content.Count() > IoCContainer.DbSettings.Value.MaxHelpPageSize) { return(-3); } HelpPage helpPage = await context.HelpPages.FindAsync(pageId); helpPage.Title = viewModel.Title; helpPage.Content = viewModel.Content; helpPage.IsLocked = viewModel.IsLocked; // Prepare tags string. List <string> newTagsList = StringExtensions.RemoveDuplicateWords(StringExtensions.RemoveSpecials(viewModel.Tags.ToUpper().Trim())).Split(' ').ToList(); // Find additions and deletions. var deleteRange = (from hpht in context.HelpPageHelpTags where hpht.HelpPageID == pageId select hpht); context.HelpPageHelpTags.RemoveRange(deleteRange); List <HelpPageHelpTag> relationsToAdd = new List <HelpPageHelpTag>(); foreach (string tag in newTagsList) { if (tag.Count() > IoCContainer.DbSettings.Value.MaxTagLength) { return(-4); } HelpTag existingTag = (from ht in context.HelpTags where ht.Value.ToUpper() == tag select ht).SingleOrDefault(); if (existingTag != null) { relationsToAdd.Add(new HelpPageHelpTag { HelpPage = helpPage, HelpTag = existingTag, }); } else { HelpTag newTag = new HelpTag { Value = tag }; await context.HelpTags.AddAsync(newTag); relationsToAdd.Add(new HelpPageHelpTag { HelpPage = helpPage, HelpTag = newTag, }); } } // Add new many-to-many await context.HelpPageHelpTags.AddRangeAsync(relationsToAdd); // Commit changes. int updateResult = await context.SaveChangesAsync(); // Simple false return instead throwing database exception // because then it would be necessary to check if title or // context has been changed. Note that content may be very large // and comparing could have huge performance impact. return(updateResult); }