public async Task <IActionResult> Edit(BookEditBindingModel model) { var serviceBook = await this.booksService.GetBookByIdAsync <BookWithPublisherServiceModel>(model.Id); if (serviceBook == null || !await this.UserCanEditBookAsync(serviceBook)) { this.ShowErrorMessage(NotificationMessages.BookEditErrorMessage); return(this.RedirectToAction("Index")); } if (!this.ModelState.IsValid) { return(this.View(model)); } var serviceModel = Mapper.Map <BookEditServiceModel>(model); var success = await this.booksService.UpdateBookAsync(serviceModel); if (!success) { this.ShowErrorMessage(NotificationMessages.BookEditErrorMessage); return(this.RedirectToAction("Index")); } this.ShowSuccessMessage(NotificationMessages.BookEditSuccessMessage); return(this.RedirectToAction("Details", new { id = model.Id })); }
public IHttpActionResult EditBook([FromUri] int id, [FromBody] BookEditBindingModel bookBindingModel) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var book = this.context.Books.FirstOrDefault(b => b.Id == id); if (book == null) { return(this.NotFound()); } book.Title = bookBindingModel.Title; book.Description = bookBindingModel.Description; book.Price = bookBindingModel.Price; book.Edition = bookBindingModel.Edition; book.AgeRestriction = bookBindingModel.AgeRestriction; book.Copies = bookBindingModel.Copies; book.AuthorId = bookBindingModel.AuthorId; book.ReleaseDate = bookBindingModel.ReleaseDate; try { this.context.SaveChanges(); return(this.Ok("Book edited successfully")); } catch (Exception) { return(this.BadRequest()); } }