public async Task <ActionResult <BookReadDto> > Post([FromBody] BookCreateDto bookCreateDto) { if (!string.IsNullOrEmpty(bookCreateDto.NewAuthorName)) { bookCreateDto.AuthorId = await _authorService.SaveAsync(bookCreateDto.NewAuthorName); } if (!string.IsNullOrEmpty(bookCreateDto.NewGenreName)) { bookCreateDto.GenreId = await _genreService.SaveAsync(bookCreateDto.NewGenreName); } bookCreateDto.IsAvailable = true; BookReadDto newBook = _mapper.Map <BookReadDto>( await _bookService.AddAsync(_mapper.Map <Book>(bookCreateDto)) ); await _notificationsService.AddBook(new BookDto { Author = newBook.Author, Title = newBook.Title }); return(CreatedAtAction("Get", new { id = newBook.Id }, newBook)); }
public BookReadDto GetBookReadDto(Book book) { BookReadDto bookDto = _mapper.Map <BookReadDto>(book); bookDto.Authors = book.AuthorsBooks .Select(ab => new AuthorForReadBookDto(ab.AuthorId, ab.Author.Name, ab.Author.BirthDate, ab.Author.ShortBio)).ToArray(); return(bookDto); }
public async Task <ActionResult> Create([FromBody] BookCreateDto bookCreateDto) { Book book = await _services.FilledBookOnCreateAsync(bookCreateDto); await _bookRepository.CreateAsync(book); BookReadDto bookReadDto = _services.GetBookReadDto(book); return(CreatedAtRoute("GetBookById", new { bookReadDto.Id }, bookReadDto)); }
public async Task <ActionResult <BookReadDto> > Get([FromRoute] int id) { BookReadDto book = _mapper.Map <BookReadDto>(await _bookService.GetByIdAsync(id)); if (book == null) { throw new ApiException(MessagesResource.BOOK_NOT_FOUND, 404); } return(Ok(book)); }
public async Task <ActionResult <BookReadDto> > GetById(int id) { Book bookFromDb = await _bookRepository.FindAByIdWithoutTrackingAsync(id); if (bookFromDb == null) { return(NotFound()); } BookReadDto bookReadDto = _services.GetBookReadDto(bookFromDb); return(Ok(bookReadDto)); }
public ActionResult <BookReadDto> CreateBook(BookCreateDto bookCreateDto) { if (_repository.IsbnExists(bookCreateDto.ISBN)) { return(Conflict(new { message = $"An existing record with the ISBN '{bookCreateDto.ISBN}' was already found.", type = "https://tools.ietf.org/html/rfc7231#section-6.5.8", status = 409 })); } Book bookModel = _mapper.Map <Book>(bookCreateDto); _repository.CreateBook(bookModel); _repository.SaveChanges(); BookReadDto bookReadDto = _mapper.Map <BookReadDto>(_repository.GetBookById(bookModel.BookId)); return(CreatedAtRoute(nameof(GetBookById), new { id = bookModel.BookId }, bookReadDto)); }
public void GetBookByIdTest() { var myProfile = new BooksProfile(); var configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile)); IMapper mapper = new Mapper(configuration); Mock <IPodzielSieKsiazkaRepo> userRepo = new Mock <IPodzielSieKsiazkaRepo>(); var items = new List <Book>() { new Book { Author = "test 1 author", Category = CategoryOfBook.Fantasy, Description = "asdbsdfsdfsdfs", Id = 1, Isbn = "1234567890", Latitude = 34, Longitude = 34, Owner = new User(), Title = "test 1 title", AddedDate = DateTime.Now, ImgUrl = "cos/url", IsAvaible = true, UserId = 1 }, new Book { Author = "test 1 author", Category = CategoryOfBook.Fantasy, Description = "asdbsdfsdfsdfs", Id = 2, Isbn = "1234567890", Latitude = 34, Longitude = 34, Owner = new User(), Title = "test 1 title", AddedDate = DateTime.Now, ImgUrl = "cos/url", IsAvaible = true, UserId = 1 }, new Book { Author = "test 1 author", Category = CategoryOfBook.Fantasy, Description = "asdbsdfsdfsdfs", Id = 3, Isbn = "1234567890", Latitude = 34, Longitude = 34, Owner = new User(), Title = "test 1 title", AddedDate = DateTime.Now, ImgUrl = "cos/url", IsAvaible = true, UserId = 1 } }; userRepo.Setup(m => m.GetBookById(1)).Returns(items[1]); userRepo.Setup(m => m.GetBookById(2)).Returns(items[2]); userRepo.Setup(m => m.GetBookById(0)).Returns(items[0]); BooksController controller = new BooksController( userRepo.Object, mapper); var result = controller.GetBookById(1); var okResult = result.Result as OkObjectResult; BookReadDto book = (BookReadDto)okResult?.Value; Debug.Assert(book != null, nameof(book) + " != null"); Assert.AreEqual(items[1].Id, book.Id); Assert.AreNotEqual(items[2].Id, book.Id); Assert.AreEqual(items[1].Category, book.Category); }