У меня есть следующий код: если я отправляю сообщение Call to CreateBook, он возвращается успешно, но со следующим ответом
{
"id": "a0add985-60f1-4adc-e15e-08d7d51b721a",
"title": "string",
"description": "string",
"amountOfPages": null,
"authorId": "2902b665-1190-4c70-9915-b9c2d7680450",
"author": null
}
Если я правильно понимаю, он должен вызвать функцию getBook, передав authorId и bookID, и он будет возвращать значение для этой книги и автора, а не книги из bookToAdd? Пожалуйста, дайте мне знать, спасибо, ребята.
/// <summary>
/// Get a book by id for a specific author
/// </summary>
/// <param name="authorId">The id of the book author</param>
/// <param name="bookId">The id of the book</param>
/// <returns>An ActionResult of type Book</returns>
/// <response code="200">Returns the requested book</response>
[HttpGet("{bookId}",Name = "GetBook")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<Book>> GetBook(
Guid authorId,
Guid bookId)
{
if (! await _authorRepository.AuthorExistsAsync(authorId))
{
return NotFound();
}
var bookFromRepo = await _bookRepository.GetBookAsync(authorId, bookId);
if (bookFromRepo == null)
{
return NotFound();
}
return Ok(_mapper.Map<Book>(bookFromRepo));
}
/// <summary>
/// Create a book for a specific author
/// </summary>
/// <remarks> default 2902B665-1190-4C70-9915-B9C2D7680450</remarks>
/// <param name="authorId">The id of the book author</param>
/// <param name="bookForCreation">The book to create</param>
/// <returns>An ActionResult of type Book</returns>
/// <response code="422">Validation error</response>
[HttpPost()]
[Consumes("application/json")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status422UnprocessableEntity,
Type = typeof(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary))]
public async Task<ActionResult<Book>> CreateBook(
Guid authorId,
[FromBody] BookForCreation bookForCreation)
{
if (!await _authorRepository.AuthorExistsAsync(authorId))
{
return NotFound();
}
var bookToAdd = _mapper.Map<Entities.Book>(bookForCreation);
bookToAdd.AuthorId = authorId;
_bookRepository.AddBook(bookToAdd);
await _bookRepository.SaveChangesAsync();
//return CreatedAtAction(
// nameof(GetBook),
// new { authorId = authorId, bookId = bookToAdd.Id },
// //new { version = HttpContext.GetRequestedApiVersion().ToString(), authorId, bookId = bookToAdd.Id },
// bookToAdd);
return CreatedAtRoute(
routeName: "GetBook",
routeValues: new { authorId = authorId, bookId = bookToAdd.Id },
value: bookToAdd
);
}