Files
ApiEfCoreLibrary/ApiEfCoreLibrary/Endpoints/Book/GetBookEndpoint.cs

43 lines
1.1 KiB
C#

using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Book;
public class GetBookRequest
{
public int Id { get; set; }
}
public class GetBookEndpoint(LibraryDbContext database) : Endpoint<GetBookRequest, GetBookDto>
{
public override void Configure()
{
Get("/api/books/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(GetBookRequest req, CancellationToken ct)
{
var book = await database.Books.Include(b => b.Author).SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (book == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetBookDto responseDto = new()
{
Id = book.Id,
Title = book.Title,
BookAuthorId = book.AuthorId,
BookAuthorName = book.Author.Name,
BookAuthorFirstName = book.Author.FirstName,
ReleaseYear = book.ReleaseYear,
Isbn = book.Isbn
};
await Send.OkAsync(responseDto, ct);
}
}