public bool EditBookById(string bookId, EditBookDto model, string libraryId) { var book = RemoveBook(bookId, model); var authors = model.Authors; var publisher = this.publisherService.GetPublisher(model.Publisher); var categories = model.Categories; var pictureUri = book.PictureName; pictureUri = GetTheNewBlobUri(model, pictureUri); var editedBook = new Book { Name = model.Name, Publisher = publisher, Rating = book.Rating, PictureName = pictureUri, IsRented = book.IsRented, IsRemoved = book.IsRemoved, Summary = model.Summary }; publisher.Books.Add(editedBook); SetCategories(categories, editedBook); SetAuthors(authors, editedBook); SetLibraryBooks(libraryId, editedBook); this.db.Add(editedBook); ChangeRents(book, editedBook); int count = this.db.SaveChanges(); return(count != 0); }
public async Task <IActionResult> Edit(int id) { _logger.LogDebug("Attempting to serve Edit View with book Id of @{id}", id); var book = await _bookRepository.GetByIdAsync(id); if (book == null) { _logger.LogError("Failed to serve Edit View because @{book} was null", book); return(View(nameof(NotFound))); } var editBookDto = new EditBookDto() { Id = book.Id, Title = book.Title, Author = book.Author, Rating = book.Rating, DateRead = book.DateRead, Description = book.Description, Image = null, ShouldChangeImage = false }; _logger.LogDebug("Returning Edit View with @{editBookDto} resource", editBookDto); return(View(editBookDto)); }
public IActionResult UpdateBookForAuthor(int authorId, int id, [FromBody] EditBookDto book) { if (book == null) { return(BadRequest()); } if (!repository.AuthorExists(authorId)) { return(NotFound()); } var bookForAuthorFromRepo = repository.GetBookForAuthor(authorId, id); if (bookForAuthorFromRepo == null) { return(NotFound()); } Mapper.Map(book, bookForAuthorFromRepo); repository.UpdateBookForAuthor(bookForAuthorFromRepo); if (!repository.Save()) { throw new Exception($"Updating book {id} for author {authorId} failed on save."); } return(NoContent()); }
public async Task Should_Edit_Book_With_Quantities_In_Offices() { MockEditBook(); var bookDto = new EditBookDto { OrganizationId = 2, Author = "test1", Id = 1, QuantityByOffice = new List <NewBookQuantityDto> { new NewBookQuantityDto { BookQuantity = 0, OfficeId = 1 }, new NewBookQuantityDto { BookQuantity = 50, OfficeId = 2 } } }; await _bookService.EditBookAsync(bookDto); var bookOffices = (await _booksDbSet.FirstAsync()).BookOffices; Assert.AreEqual(0, bookOffices.First(x => x.OfficeId == 1).Quantity); Assert.AreEqual(50, bookOffices.First(x => x.OfficeId == 2).Quantity); Assert.AreEqual("test1", _booksDbSet.First().Author); }
public IActionResult Edit(EditBookDto dto) { var command = new EditBookCommand(dto); commandBus.Send(command); return(Redirect("~/book")); }
private Book RemoveBook(string bookId, EditBookDto model) { var book = this.GetBookById(bookId); if (model.NewPicture != null) { this.blobStorage.DeleteBlobData(book.PictureName); } this.db.Remove(book); return(book); }
private string GetTheNewBlobUri(EditBookDto model, string pictureUri) { if (model.NewPicture != null) { var imageUri = string.Empty; using (var ms = new MemoryStream()) { model.NewPicture.CopyTo(ms); var fileBytes = ms.ToArray(); pictureUri = this.blobStorage.UploadFileToBlob(model.NewPicture.FileName, fileBytes, model.NewPicture.ContentType); } } return(pictureUri); }
private static void ManageQuantitiesInOffices(EditBookDto editedBook, Book existingBook) { foreach (var officeQuantity in editedBook.QuantityByOffice) { var bookOfficeExists = existingBook.BookOffices.Any(x => x.OfficeId == officeQuantity.OfficeId); if (!bookOfficeExists) { existingBook.BookOffices.Add(MapBookDtoToBookOfficeEntity(existingBook, officeQuantity, editedBook.UserId)); } else { var office = existingBook.BookOffices.First(x => x.OfficeId == officeQuantity.OfficeId); office.Quantity = officeQuantity.BookQuantity; } } }
public void EditBook(EditBookDto dto) { //数据验证 if (dto == null) { throw new ArgumentNullException(); } var entity = InMemoryBookStorage.Books.FirstOrDefault(m => m.Id == dto.Id); if (entity == null) { throw new Exception("图书不存在"); } entity.Price = dto.Price; }
public async Task EditBookAsync(EditBookDto editedBook) { var existingBook = await _booksDbSet .Include(book => book.BookOffices) .FirstAsync(book => book.Id == editedBook.Id && book.OrganizationId == editedBook.OrganizationId); existingBook.Modified = DateTime.UtcNow; existingBook.ModifiedBy = editedBook.UserId; existingBook.Title = editedBook.Title; existingBook.Author = editedBook.Author; existingBook.Code = editedBook.Isbn; existingBook.Url = editedBook.Url; existingBook.ApplicationUserId = editedBook.OwnerId; existingBook.Note = editedBook.Note; ValidateQuantitiesValues(editedBook.QuantityByOffice.Select(o => o.BookQuantity)); ManageQuantitiesInOffices(editedBook, existingBook); await _uow.SaveChangesAsync(false); }
public async Task <IActionResult> EditBook([FromForm] EditBookDto inputModel) { if (!ModelState.IsValid) { return(StatusCode(StatusCodes.Status406NotAcceptable)); } var book = await dbContext.Books.FindAsync(inputModel.Id); if (book == null) { return(NotFound()); } var user = await userManager.GetUserAsync(User); if (book.UserId != user.Id && !await userManager.IsInRoleAsync(user, Roles.Admin.ToString())) { return(StatusCode(StatusCodes.Status403Forbidden)); } book.Title = inputModel.Title; book.Description = inputModel.Description; book.Year = inputModel.Year; if (inputModel.NewCover != null) { using (Stream imageFileStream = inputModel.NewCover.OpenReadStream()) { book.CoverUrl = await imagesService.UploadImageAsync(imageFileStream); } } dbContext.Books.Update(book); await dbContext.SaveChangesAsync(); return(Ok()); }
public EditBookCommand(EditBookDto dto) { this.Dto = dto; }
public async Task <IActionResult> Edit(EditBookDto model) { _logger.LogDebug("Attempting to edit book resource from @{model}", model); if (model == null) { _logger.LogError("Could not edit book resource because the @{model} was null", model); return(RedirectToAction("Error", "Errors")); } if (!ModelState.IsValid) { return(View()); } var book = await _bookRepository.GetByIdAsync(model.Id); if (book == null) { _logger.LogError("Failed to serve Edit View because @{book} was null", book); return(View(nameof(NotFound))); } book.Title = model.Title; book.Author = model.Author; book.Rating = model.Rating; book.Description = model.Description; book.DateRead = model.DateRead; if (model.ShouldChangeImage) { _logger.LogDebug("Attempting to change image for @{book}", book); var oldImagePath = book.ImagePath; if (model.Image != null) { book.ImagePath = _imageService.GenerateImagePath(Path.GetFileName(model.Image.FileName)); if (_imageService.TrySaveAndResizeImage(model.Image, BookImageSize, book)) { _logger.LogDebug("The @{model.Image} for @{book} was successfully saved and resized", model.Image, book); } else { _logger.LogError("Failed to create @{book} because the @{model.Image} image was not created", book, model.Image); ViewBag.EditImageError = "We were not able to create the book image due to an error, please try again or submit your book entry without the image"; return(View()); } } else { book.ImagePath = null; _logger.LogDebug("The image for @{book} was successfully changed to null", book); } if (oldImagePath != null) { if (_imageService.TryDeleteImage(oldImagePath)) { _logger.LogDebug("Successfully deleted the @{oldImagePath} image for @{book}", oldImagePath, book); } else { _logger.LogError("Failed to delete the @{oldImagePath} image for the @{book} resource", oldImagePath, book); } } } try { await _bookRepository.UpdateAsync(book); } catch (Exception ex) { _logger.LogError(ex, "Something went wrong while trying to edit @{book} resource from @{model}", book, model); return(RedirectToAction("Error", "Errors")); } _logger.LogDebug("Successfully edited @{book} resource created from @{model}", book, model); return(RedirectToAction(nameof(Index))); }