30 lines
865 B
C#
30 lines
865 B
C#
using BookHive.DTO.Book;
|
|
using BookHive.Models;
|
|
using BookHive.Repositories;
|
|
using BookHive.Specifications.Authors;
|
|
using FastEndpoints;
|
|
|
|
namespace BookHive.Endpoints.Books;
|
|
|
|
public class CreateBookEndpoint(BookRepository bookRepository, AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<CreateBookDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/books/");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateBookDto req, CancellationToken ct)
|
|
{
|
|
Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.AuthorId), ct);
|
|
if (author is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
await bookRepository.AddAsync(mapper.Map<Book>(req), ct);
|
|
await Send.NoContentAsync(ct);
|
|
}
|
|
|
|
} |