// GET: Edit public ActionResult Edit(int id) { var service = CreateBookshelfService(); var shelfToBeEdited = service.GetBookshelfById(id); var updatedBookshelf = new BookshelfEdit { ShelfId = shelfToBeEdited.ShelfId, ShelfName = shelfToBeEdited.ShelfName, }; return(View(updatedBookshelf)); }
public IHttpActionResult UpdateBookshelf(BookshelfEdit model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } BookshelfService service = CreateBookshelfService(); if (!service.UpdateBookshelf(model)) { return(InternalServerError()); } return(Ok()); }
public bool UpdateBookshelf(BookshelfEdit model) { using (var ctx = new ApplicationDbContext()) { var shelfToUpdate = ctx .Bookshelves .Single(e => e.ShelfId == model.ShelfId && e.UserId == _userId); shelfToUpdate.ShelfName = model.ShelfName; shelfToUpdate.ModifiedUtc = DateTimeOffset.UtcNow; return(ctx.SaveChanges() == 1); } }
public bool UpdateBookshelf(BookshelfEdit model) { Bookshelf shelfToEdit = _context.Bookshelves.Single(b => b.BookshelfId == model.BookshelfId); shelfToEdit.Title = model.Title; shelfToEdit.BookIds = model.BookIds; shelfToEdit.Books = _context.Books.Where(b => model.BookIds.Contains(b.BookId)).ToList(); // Removes books that should no longer be on shelf after update List <Book> booksToRemove = shelfToEdit.Books.Where(b => !model.BookIds.Contains(b.BookId)).ToList(); foreach (Book book in booksToRemove) { shelfToEdit.Books.Remove(book); } return(_context.SaveChanges() > 0); }
public ActionResult Edit(int id, BookshelfEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.ShelfId != id) { ModelState.AddModelError("", "ID Mismatch, please try again."); return(View(model)); } var service = CreateBookshelfService(); if (service.UpdateBookshelf(model)) { TempData["SaveResult"] = "Your bookshelf was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "Your bookshelf could not be updated."); return(View(model)); }