コード例 #1
0
        public async Task <BookResponseDto> PublishBook(Guid authorId, PublishBookRequestDto request)
        {
            Devon4NetLogger.Debug("Entering Publish book on AuhtorService");

            var author = await _authorRepository.GetFirstOrDefault(aut => aut.Id == authorId).ConfigureAwait(false);

            if (author == null)
            {
                throw new AuthorNotFoundException();
            }

            Book book;

            // using (var transaction = await UoW.BeginTransaction().ConfigureAwait(false))
            // {
            book = await _bookRepository.Create(request.ToBook()).ConfigureAwait(false);

            var authorBook = new AuthorBook
            {
                Author       = author.Id,
                Book         = book.Id,
                PublishDate  = DateTime.Now,
                ValidityDate = DateTime.Now.AddYears(1)
            };
            await _authorBookRepository.Create(authorBook).ConfigureAwait(false);

            //     await transaction.CommitAsync().ConfigureAwait(false);
            // }

            return(book.ToBookResponse());
        }
コード例 #2
0
 public static Book ToBook(this PublishBookRequestDto request) => new Book
 {
     Id      = Guid.NewGuid(),
     Title   = request.Title,
     Genre   = request.Genre,
     Summary = request.Summary
 };
コード例 #3
0
        public async void NonExistingAuthorPublishBook()
        {
            // Arrange
            var authorId = Guid.NewGuid();
            var request  = new PublishBookRequestDto
            {
                Title   = "New Title",
                Summary = "Summary",
                Genre   = "Action"
            };

            _mockAuthorRepository
            .Setup(repo => repo.GetFirstOrDefault(aut => aut.Id == authorId))
            .ReturnsAsync((Author)null);
            _mockBookRepository
            .Setup(repo => repo.Create(It.IsAny <Book>(), true))
            .ReturnsAsync(new Book());
            _mockAuthorBookRepository
            .Setup(repo => repo.Create(It.IsAny <AuthorBook>(), true))
            .ReturnsAsync(new AuthorBook());
            var authorService = SetupService();

            // Act
            AuthorNotFoundException exception = null;

            try
            {
                await authorService.PublishBook(authorId, request);
            }
            catch (AuthorNotFoundException ex)
            {
                exception = ex;
            }
            finally
            {
                Assert.NotNull(exception);
            }
        }
コード例 #4
0
        public async void ExistingAuthorPublishABook()
        {
            // Arrange
            var authorId = Guid.NewGuid();
            var request  = new PublishBookRequestDto
            {
                Title   = "New Title",
                Summary = "Summary",
                Genre   = "Action"
            };
            var expected = new BookResponseDto
            {
                Title   = "New Title",
                Summary = "Summary",
                Genre   = "Action",
                Id      = Guid.NewGuid()
            };

            _mockAuthorRepository
            .Setup(repo => repo.GetFirstOrDefault(aut => aut.Id == authorId))
            .ReturnsAsync(new Author());
            _mockBookRepository
            .Setup(repo => repo.Create(It.IsAny <Book>(), true))
            .ReturnsAsync(new Book {
                Title = expected.Title, Summary = expected.Summary, Genre = expected.Genre, Id = expected.Id
            });
            _mockAuthorBookRepository
            .Setup(repo => repo.Create(It.IsAny <AuthorBook>(), true))
            .ReturnsAsync(new AuthorBook());
            var authorService = SetupService();

            // Act
            var actual = await authorService.PublishBook(authorId, request);

            // Assert
            Assert.True(BookResponseEquals(expected, actual));
        }
コード例 #5
0
 public async Task <IActionResult> Publish([FromQuery] string authorId, [FromBody] PublishBookRequestDto requestDto)
 {
     Devon4NetLogger.Debug("Entering PublishBook on AuthorManagmentController");
     return(Ok(await _authorService.PublishBook(Guid.Parse(authorId), requestDto).ConfigureAwait(false)));
 }