public async Task <bool> UpdateAsync(BookPutDto bookDto)
        {
            var book    = _mapper.Map <Book>(bookDto);
            var oldBook = await _bookRepository.GetAll().AsNoTracking().FirstOrDefaultAsync(a => a.Id == book.Id);

            if (oldBook == null)
            {
                return(false);
            }
            if (bookDto.FieldMasks.Contains("Image"))
            {
                string imagePath;
                bookDto.FieldMasks.Remove("Image");
                bookDto.FieldMasks.Add("ImagePath");
                if (oldBook.ImagePath != null)
                {
                    _imageService.DeleteImage(oldBook.ImagePath);
                }
                if (bookDto.Image != null)
                {
                    imagePath = await _imageService.UploadImage(bookDto.Image);
                }
                else
                {
                    imagePath = null;
                }
                book.ImagePath = imagePath;
            }
            await _bookRepository.Update(book, bookDto.FieldMasks);

            if (bookDto.UserId != oldBook.UserId)
            {
                var user = await _userLocationRepository.FindByIdAsync(oldBook.UserId.Value);

                string emailMessageForUser = $" Administrator has successfully received your book '{oldBook.Name}'";
                SendMailForOwnership(book, user, emailMessageForUser);
                SendNotificationToUser(oldBook.UserId.Value, book.Id, emailMessageForUser);

                var userId = _userResolverService.GetUserId();
                var admin  = await _userLocationRepository.FindByIdAsync(userId);

                string emailMessageForAdmin = $"You became the current owner of the book '{oldBook.Name}'";
                SendMailForOwnership(book, admin, emailMessageForAdmin);
                SendNotificationToUser(userId, book.Id, emailMessageForAdmin);
            }
            var affectedRows = await _bookRepository.SaveChangesAsync();

            var isDatabaseUpdated = affectedRows > 0;

            if (isDatabaseUpdated &&
                bookDto.FieldMasks.Contains("State") &&
                bookDto.State == BookState.Available)
            {
                await _wishListService.NotifyAboutAvailableBookAsync(book.Id);
            }
            return(true);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutBookAsync([FromRoute] int id, [FromForm] BookPutDto bookDto)
        {
            if (id != bookDto.Id)
            {
                return(BadRequest());
            }

            var isBookUpdated = await _bookService.UpdateAsync(bookDto);

            if (!isBookUpdated)
            {
                return(BadRequest());
            }

            return(NoContent());
        }
        public async Task UpdateBook_BookExists_Returns_True()
        {
            var booksMock = GetTestBooks().AsQueryable().BuildMock();

            _bookRepositoryMock.Setup(s => s.GetAll()).Returns(booksMock.Object);
            _bookRepositoryMock.Setup(s => s.Update(It.IsAny <Book>(), It.IsAny <List <string> >()));
            _bookRepositoryMock.Setup(x => x.SaveChangesAsync()).ReturnsAsync(1);
            var bookDto = new BookPutDto {
                Id = 1, FieldMasks = new List <string> {
                    "Name"
                }
            };

            var result = await _bookService.UpdateAsync(bookDto);

            result.Should().BeTrue();
        }