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> ContentPageUpdate(int? id) { var userId = User.Identity.GetUserId(); var user = await UserManager.FindByIdAsync(userId); var model = new ContentPage(); if (!id.HasValue || id == 0) { model.Author = user.FullName; } else { model = await _contentPageService.FindAsync(id); } return View(model); }