public void EditBook(EditBookBindingModel bindingModel) { Book editedBook = Mapper.Map <EditBookBindingModel, Book>(bindingModel); this.Context.Entry(editedBook).State = EntityState.Modified; this.Context.SaveChanges(); }
public ActionResult Edit([Bind(Include = "Id,Title,ImageUrl,Language,Description,Price,Quantity,NumberOfPages,IssueDate,ISBN")] EditBookBindingModel bindingModel) { if (ModelState.IsValid) { this.bookService.GetEditBook(bindingModel); return(RedirectToAction("Details", "Books", new { id = bindingModel.Id })); } return(View(bindingModel)); }
public async Task <IActionResult> EditBook(EditBookBindingModel model) { if (!this.ModelState.IsValid) { SetErrorMessage(CommonConstants.DangerMessage); return(this.EditBook(model.Id)); } int generatedId = await this.bookService.EditBookAsync(model); if (generatedId < 1) { return(RedirectToAction(RedirectConstants.IndexSuffix)); } return(Redirect(string.Format(RedirectConstants.AdministratorAreaBookDetailsPage, generatedId))); }
public IHttpActionResult EditBook(string id, EditBookBindingModel model) { Guid bookId = new Guid(id); var bookInDb = this.context.Books .FirstOrDefault(b => b.Id == bookId); if (bookInDb == null) { return(this.BadRequest("This book does not extists!")); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } bookInDb.Title = model.Title; bookInDb.Description = model.Description; bookInDb.Price = model.Price; bookInDb.Copies = model.Copies; bookInDb.EditionType = model.Edition; bookInDb.AgeRestriction = model.AgeRestriction; bookInDb.ReleaseDate = model.ReleaseDate; var authorId = new Guid(model.AuthorId); var author = this.context.Authors .FirstOrDefault(a => a.Id == authorId); if (author == null) { return(this.BadRequest("Invalid author id")); } bookInDb.AuthorId = authorId; this.context.Books.AddOrUpdate(bookInDb); this.context.SaveChanges(); var bookToReturn = this.context.Books .Where(b => b.Id == bookInDb.Id) .Select(BookDataModel.DataModel) .FirstOrDefault(); return(this.Ok(bookToReturn)); }
public async Task <int> EditBookAsync(EditBookBindingModel model) { var book = this.DbContext .Books .FirstOrDefault(x => x.Id == model.Id); if (book == null) { return(ErrorId); } book.Description = model.Description; book.PhotoURL = model.PhotoURL; book.AdditionalInfoURL = model.AdditionalInfoURL; await this.DbContext.SaveChangesAsync(); return(book.Id); }
public async Task <IActionResult> Put(int id, [FromBody] EditBookBindingModel model) { var existingBook = await this.booksService.ExistAsync(id); if (!existingBook) { return(this.NotFound()); } var existsAuthor = await this.authorsService.AuthorExistAsync(model.AuthorId); if (!existsAuthor) { return(this.BadRequest("Author does not exist.")); } var book = this.mapper.Map <EditBookModel>(model); await this.booksService.EditAsync(id, book); return(this.Ok()); }
public IHttpActionResult PutBook(int id, EditBookBindingModel bindingModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != bindingModel.Id) { return(BadRequest()); } try { this.bookService.EditBook(bindingModel); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } // return StatusCode(HttpStatusCode.NoContent); Book book = this.bookService.GetCurrentBook(id); if (book == null) { return(NotFound()); } return(Ok(book)); }