first commit
This commit is contained in:
22
BookHive/Endpoints/Books/CreateBookEndpoint.cs
Normal file
22
BookHive/Endpoints/Books/CreateBookEndpoint.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using BookHive.DTO.Book;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Books;
|
||||
|
||||
public class CreateBookEndpoint(BookRepository bookRepository, AutoMapper.IMapper mapper) : Endpoint<CreateBookDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/books/");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateBookDto req, CancellationToken ct)
|
||||
{
|
||||
await bookRepository.AddAsync(mapper.Map<Book>(req), ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
|
||||
}
|
||||
34
BookHive/Endpoints/Books/DeleteBookEndpoint.cs
Normal file
34
BookHive/Endpoints/Books/DeleteBookEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Books;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Books;
|
||||
|
||||
public class DeleteBookRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<DeleteBookRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/books/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteBookRequest req, CancellationToken ct)
|
||||
{
|
||||
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.Id), ct);
|
||||
|
||||
if (book is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await bookRepository.DeleteAsync(book, ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
34
BookHive/Endpoints/Books/GetBookEndpoint.cs
Normal file
34
BookHive/Endpoints/Books/GetBookEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
19
BookHive/Endpoints/Books/GetBooksEndpoint.cs
Normal file
19
BookHive/Endpoints/Books/GetBooksEndpoint.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using BookHive.DTO.Book;
|
||||
using BookHive.Repositories;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Books;
|
||||
|
||||
public class GetBooksEndpoint(BookRepository bookRepository) : EndpointWithoutRequest<List<GetBookDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/books/");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
await Send.OkAsync(await bookRepository.ProjectToListAsync<GetBookDto>(ct), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user