using BookHive.DTO.Book;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Books;
using FastEndpoints;

namespace BookHive.Endpoints.Books;

public class BookRequest
{
    public int Id { get; set; }
}

public class GetBookEndpoint(BookRepository bookRepository, AutoMapper.IMapper mapper) : Endpoint<BookRequest, GetBookDetailsDto>
{
    public override void Configure()
    {
        Get("/books/{@Id}/", x => new { x.Id });
        AllowAnonymous();
    }

    public override async Task HandleAsync(BookRequest req, CancellationToken ct)
    {
        Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.Id), ct);

        if (book is null)
        {
            await Send.NoContentAsync(ct);
            return;
        }

        await Send.OkAsync(mapper.Map<GetBookDetailsDto>(book), ct);
    }
}