Пример #1
0
        public ActionResult <BookDto> CreateBookForAuthor(
            Guid authorId, BookForCreationForAuthorDto book)
        {
            var checkAuthorExists = _repositoryWrapper.Authors.Get(authorId);

            if (checkAuthorExists == null)
            {
                return(NotFound());
            }

            var bookEntity = _mapper.Map <Book>(book);

            var authorEntity = _repositoryWrapper.Authors.Get(authorId);

            _repositoryWrapper.Books.Add(bookEntity);

            BookAuthor bookAuthor = new BookAuthor()
            {
                BookId   = bookEntity.BookId,
                Book     = bookEntity,
                AuthorId = authorEntity.AuthorId,
                Author   = authorEntity
            };

            _repositoryWrapper.BookAuthors.Add(bookAuthor);

            var bookToReturn = _mapper.Map <BookDto>(bookEntity);

            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = authorId, bookId = bookToReturn.BookId },
                                  bookToReturn));
        }
Пример #2
0
        public async Task AddABookOfAnAuthorTest()
        {
            BookForCreationForAuthorDto newBook = new BookForCreationForAuthorDto()
            {
                Title       = "Fear: Trump in the White House",
                Description = "With authoritative reporting honed through eight presidencies from Nixon to Obama, author Bob Woodward reveals in unprecedented detail the harrowing life inside President Donald Trump’s White House and precisely how he makes decisions on major foreign and domestic policies. Woodward draws from hundreds of hours of interviews with firsthand sources, meeting notes, personal diaries, files and documents. The focus is on the explosive debates and the decision-making in the Oval Office, the Situation Room, Air Force One and the White House residence.",
                Publisher   = "Simon & Schuster UK",
                ISBN        = "978-1471181290",
                Genres      = new List <string>()
                {
                    "Political Science"
                },
                Language = "English"
            };

            var json          = JsonConvert.SerializeObject(newBook);
            var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            _client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", await GetJwtAsync());

            string authorId = SeedData.author2.AuthorId.ToString();

            var response = await _client.PostAsync($"/api/authors/{authorId}/books", stringContent);

            response.EnsureSuccessStatusCode();
            var stringResponse = await response.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <BookDto>(stringResponse);

            Assert.NotNull(result);
            Assert.Equal(newBook.Title, result.Title);
            Assert.True(result.Genres.Count() == newBook.Genres.Count());
            Assert.True(result.Language == newBook.Language);
        }