public async Task <SystemPageWriteResponse> UpdateSystemPage(SystemPageWriteViewModel page, CancellationToken cancellationToken) { var response = new SystemPageWriteResponse(); try { var result = _context.SystemPage.FirstOrDefault(x => x.Id == page.Id && x.IsDeleted == false); if (result != null) { if (_context.SystemPage.Any(x => x.Slug == page.Slug && x.IsDeleted == false && x.Id != page.Id)) { response.Response = ResponseType.AlreadyExists; return(response); } result.Title = page.Title; result.Content = page.Content; result.Slug = page.Slug.ToLower(); await _context.SaveChangesAsync(cancellationToken); response.Response = ResponseType.Success; return(response); } response.Response = ResponseType.DoesntExist; return(response); } catch (DbEntityValidationException) { response.Response = ResponseType.Error; } return(response); }
public async Task <SystemPageWriteResponse> CreateSystemPage(SystemPageWriteViewModel page, CancellationToken cancellationToken) { var response = new SystemPageWriteResponse(); var systemPage = new SystemPage() { Title = page.Title, Content = page.Content, Slug = page.Slug, IsDeleted = false, }; try { if (_context.SystemPage.Any(x => x.Slug == page.Slug && x.IsDeleted == false)) { response.Response = ResponseType.AlreadyExists; return(response); } _context.SystemPage.Add(systemPage); await _context.SaveChangesAsync(cancellationToken); response.Id = systemPage.Id; response.Response = ResponseType.Success; } catch (DbEntityValidationException) { response.Response = ResponseType.Error; } return(response); }
public async Task <ActionResult> EditAsync(SystemPageWriteViewModel pageModel, CancellationToken cancellationToken) { if (!ModelState.IsValid) { return(View(pageModel)); } if (!_systemPagesService.IsValidSlug(pageModel.Slug)) { ModelState.AddModelError("Slug", "The slug provided is not valid, it must not contain any special characters or spaces, only letters or hyphens"); return(View(pageModel)); } var result = await _systemPagesService.UpdateSystemPage(pageModel, cancellationToken); if (result.Response == ResponseType.Success) { return(RedirectToAction("Index")); } switch (result.Response) { case ResponseType.DoesntExist: ModelState.AddModelError("", "We couldn't find the page to update"); break; case ResponseType.AlreadyExists: ModelState.AddModelError("Slug", "The slug provided already exists, it must be unique"); break; case ResponseType.Error: ModelState.AddModelError("", "Sorry there was an error, please try again"); break; case ResponseType.PermissionDenied: ModelState.AddModelError("", "You do not have permission to do this"); break; default: ModelState.AddModelError("", "Sorry there was an error, please try again"); break; } return(View(pageModel)); }
public async Task <ActionResult> EditAsync(Guid id, CancellationToken cancellationToken) { var page = await _systemPagesService.GetSystemPageById(id, cancellationToken); if (page == null) { return(new HttpNotFoundResult("Page not found")); } var writeModel = new SystemPageWriteViewModel { Id = page.Id, Title = page.Title, Slug = page.Slug, Content = page.Content }; return(View(writeModel)); }
public async Task <ActionResult> DeleteAsync(Guid id, CancellationToken cancellationToken) { var result = await _systemPagesService.DeleteSystemPage(id, cancellationToken); if (result.Response == ResponseType.Success) { return(RedirectToAction("Index")); } switch (result.Response) { case ResponseType.DoesntExist: ModelState.AddModelError("", "We couldn't find the page to delete"); break; case ResponseType.Error: ModelState.AddModelError("", "Sorry there was an error, please try again"); break; case ResponseType.PermissionDenied: ModelState.AddModelError("", "You do not have permission to do this"); break; default: ModelState.AddModelError("", "Sorry there was an error, please try again"); break; } var page = await _systemPagesService.GetSystemPageById(id, cancellationToken); var writeModel = new SystemPageWriteViewModel { Id = page.Id, Title = page.Title, Slug = page.Slug, Content = page.Content }; return(View("Edit", writeModel)); }
public async Task <SystemPageWriteResponse> UpdateSystemPage(SystemPageWriteViewModel model, CancellationToken cancellationToken) { var response = await _systemPagesCommand.UpdateSystemPage(model, cancellationToken); return(response); }