public async Task<ActionResult> Edit(string name) { if (name == null) { return RedirectToAction("Index", "Home"); } var user = await GetApplicationUser(); if (user == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var document = await db.Documents.FirstOrDefaultAsync(c => c.Name == name); if (document == null) { return new HttpStatusCodeResult(HttpStatusCode.NotFound); } if (user.Id != document.Owner.Id) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var viewModel = new EditDocumentViewModel() { Name = document.Name, Title = document.Title, Index = document.Index, Body = document.Body }; return View(viewModel); }
public async Task<ActionResult> Edit(EditDocumentViewModel viewModel) { var user = await GetApplicationUser(); if (user == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var document = await db.Documents.FirstOrDefaultAsync(c => c.Name == viewModel.Name); if (document == null) { return new HttpStatusCodeResult(HttpStatusCode.NotFound); } if (user.Id != document.Owner.Id) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } document.Title = viewModel.Title; document.Body = viewModel.Body; document.Index = viewModel.Index; document.ModifiedAt = DateTime.Now; await db.SaveChangesAsync(); return RedirectToAction("Details", new {name = document.Name}); }