public async Task <ActionResult> ContentPageUpdate(ContentPage contentPage) { var userId = User.Identity.GetUserId(); if (contentPage.ID == 0) { contentPage.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added; contentPage.Created = DateTime.Now; contentPage.LastUpdated = DateTime.Now; var query = await _contentPageService.Query(x => x.Slug.ToLower() == contentPage.Slug.ToLower()).SelectAsync(); if (query.Any()) { TempData[TempDataKeys.UserMessageAlertState] = "bg-danger"; TempData[TempDataKeys.UserMessage] = string.Format("[[[Slug {0} already exists]]]", contentPage.Slug); return(View(contentPage)); } if (_contentPageService.Queryable().Any()) { contentPage.Ordering = _contentPageService.Queryable().Max(x => x.Ordering); } _contentPageService.Insert(contentPage); } else { var contentPageExisting = await _contentPageService.FindAsync(contentPage.ID); contentPageExisting.Title = contentPage.Title; contentPageExisting.Description = contentPage.Description; contentPageExisting.Slug = contentPage.Slug; contentPageExisting.Html = contentPage.Html; contentPageExisting.UserID = userId; contentPageExisting.Author = contentPage.Author; contentPageExisting.Published = contentPage.Published; contentPageExisting.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Modified; contentPageExisting.LastUpdated = DateTime.Now; _contentPageService.Update(contentPageExisting); } await _unitOfWorkAsync.SaveChangesAsync(); _dataCacheService.RemoveCachedItem(CacheKeys.ContentPages); return(RedirectToAction("ContentPages")); }
public async Task <ActionResult> ContentPage(string id) { if (string.IsNullOrEmpty(id)) { return(RedirectToAction("Index", "Home")); } var slug = id.ToLowerInvariant(); var contentPageQuery = await _contentPageService.Query(x => x.Slug == slug && x.Published).SelectAsync(); var contentPage = contentPageQuery.FirstOrDefault(); if (contentPage == null) { return(new HttpNotFoundResult()); } return(View(contentPage)); }
public async Task<ActionResult> ContentPageUpdate(ContentPage contentPage, string[] selectedRoles = null) { var userId = User.Identity.GetUserId(); ViewBag.Roles = RoleManager.Roles.OrderBy(ob => ob.Name).ToList(); if (contentPage.ID == 0) { contentPage.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added; contentPage.Created = DateTime.Now; contentPage.LastUpdated = DateTime.Now; var query = await _contentPageService.Query(x => x.Slug.ToLower() == contentPage.Slug.ToLower()).SelectAsync(); if (query.Any()) { TempData[TempDataKeys.UserMessageAlertState] = "bg-danger"; TempData[TempDataKeys.UserMessage] = string.Format("[[[Slug {0} already exists]]]", contentPage.Slug); return View(contentPage); } if (_contentPageService.Queryable().Any()) { contentPage.Ordering = _contentPageService.Queryable().Max(x => x.Ordering); } _contentPageService.Insert(contentPage); } else { var contentPageExisting = await _contentPageService.FindAsync(contentPage.ID); contentPageExisting.Title = contentPage.Title; contentPageExisting.Description = contentPage.Description; contentPageExisting.Slug = contentPage.Slug; contentPageExisting.Html = contentPage.Html; contentPageExisting.UserID = userId; contentPageExisting.Author = contentPage.Author; contentPageExisting.Published = contentPage.Published; contentPageExisting.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Modified; contentPageExisting.LastUpdated = DateTime.Now; _contentPageService.Update(contentPageExisting); // Delete existing content page roles var contentPageRoles = _contentPageRoleService.Queryable().Where(x => x.ContentPageID == contentPageExisting.ID).Select(x => x.ID).ToList(); if (contentPageRoles.Any()) { foreach (var contentPageRole in contentPageRoles) { await _contentPageRoleService.DeleteAsync(contentPageRole); } } } if (selectedRoles != null) { for (int roleCount = 0; roleCount < selectedRoles.Length; roleCount++) { var contentPageRole = new ContentPageRole() { ContentPageID = contentPage.ID, AspNetRoleID = selectedRoles[roleCount], ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added }; _contentPageRoleService.Insert(contentPageRole); } } await _unitOfWorkAsync.SaveChangesAsync(); _dataCacheService.RemoveCachedItem(CacheKeys.ContentPages); return RedirectToAction("ContentPages"); }