public async Task <ActionResult <BookResource> > CreateBook(SaveBookResource saveBookResource) { var validator = new SaveBookResourceValidator(); var validationResult = await validator.ValidateAsync(saveBookResource); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } var bookToCreate = _mapper.Map <SaveBookResource, Book>(saveBookResource); var newBook = await _bookService.CreateBook(bookToCreate); foreach (var author in saveBookResource.Authors) { BookAuthorResource bookAuthorResource = new BookAuthorResource(); bookAuthorResource.AuthorId = author.Id; bookAuthorResource.BookId = newBook.Id; var bookAuthorToCreate = _mapper.Map <BookAuthorResource, BookAuthor>(bookAuthorResource); var newBookAuhtor = await _bookAuhtorService.CreateBookAuthor(bookAuthorToCreate); } foreach (var category in saveBookResource.Categories) { BookCategoryResource bookCategoryResource = new BookCategoryResource(); bookCategoryResource.CategoryId = category.Id; bookCategoryResource.BookId = newBook.Id; var bookCategoryToCreate = _mapper.Map <BookCategoryResource, BookCategory>(bookCategoryResource); var newBookCategory = await _bookCategoryService.CreateBookCategory(bookCategoryToCreate); } var databaseBooks = await _bookService.GetWithCategoriesAndAuthorById(newBook.Id); return(Ok(_mapper.Map <Book, BookResource>(databaseBooks))); }