first commit
This commit is contained in:
21
BookHive/Endpoints/Authors/CreateAuthorEndpoint.cs
Normal file
21
BookHive/Endpoints/Authors/CreateAuthorEndpoint.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using BookHive.DTO.Author;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class CreateAuthorEndpoint(AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<CreateAuthorDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/authors/");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateAuthorDto req, CancellationToken ct)
|
||||
{
|
||||
await authorRepository.AddAsync(mapper.Map<Author>(req), ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
34
BookHive/Endpoints/Authors/DeleteAuthorEndpoint.cs
Normal file
34
BookHive/Endpoints/Authors/DeleteAuthorEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Authors;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class DeleteAuthorRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteAuthorEndpoint(AuthorRepository authorRepository) : Endpoint<DeleteAuthorRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/authors/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteAuthorRequest req, CancellationToken ct)
|
||||
{
|
||||
Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.Id), ct);
|
||||
|
||||
if (author is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await authorRepository.DeleteAsync(author, ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
34
BookHive/Endpoints/Authors/GetAuthorEndpoint.cs
Normal file
34
BookHive/Endpoints/Authors/GetAuthorEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using BookHive.DTO.Author;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Authors;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class AuthorRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetAuthorEndpoint(AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<AuthorRequest, GetAuthorDetailsDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/authors/{@Id}/", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(AuthorRequest req, CancellationToken ct)
|
||||
{
|
||||
Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.Id), ct);
|
||||
|
||||
if (author is null)
|
||||
{
|
||||
await Send.NoContentAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetAuthorDetailsDto>(author), ct);
|
||||
}
|
||||
}
|
||||
19
BookHive/Endpoints/Authors/GetAuthorsEndpoint.cs
Normal file
19
BookHive/Endpoints/Authors/GetAuthorsEndpoint.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using BookHive.DTO.Author;
|
||||
using BookHive.Repositories;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class GetAuthorsEndpoint(AuthorRepository authorRepository) : EndpointWithoutRequest<List<GetAuthorDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/authors/");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
await Send.OkAsync(await authorRepository.ProjectToListAsync<GetAuthorDto>(ct), ct);
|
||||
}
|
||||
}
|
||||
30
BookHive/Endpoints/Authors/UpdateAuthorEndpoint.cs
Normal file
30
BookHive/Endpoints/Authors/UpdateAuthorEndpoint.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using BookHive.DTO.Author;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Authors;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Authors;
|
||||
|
||||
public class UpdateAuthorEndpoint(AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateAuthorDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/authors/{@Id}/", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateAuthorDto req, CancellationToken ct)
|
||||
{
|
||||
Author? author = await authorRepository.SingleOrDefaultAsync(new GetAuthorByIdSpec(req.Id), ct);
|
||||
if (author is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
mapper.Map(req, author);
|
||||
await authorRepository.SaveChangesAsync(ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user