fixed errors and finish endpoints
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
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, AutoMapper.IMapper mapper) : Endpoint<CreateBookDto>
|
||||
public class CreateBookEndpoint(BookRepository bookRepository, AuthorRepository authorRepository, AutoMapper.IMapper mapper) : Endpoint<CreateBookDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -15,6 +16,13 @@ public class CreateBookEndpoint(BookRepository bookRepository, AutoMapper.IMappe
|
||||
|
||||
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.OkAsync(cancellation: ct);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Books;
|
||||
using BookHive.Specifications.Loans;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Books;
|
||||
@@ -10,14 +11,14 @@ public class DeleteBookRequest
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<DeleteBookRequest>
|
||||
public class DeleteBookEndpoint(BookRepository bookRepository, LoanRepository loanRepository) : 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);
|
||||
@@ -27,7 +28,14 @@ public class DeleteBookEndpoint(BookRepository bookRepository) : Endpoint<Delete
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Loan? loan = await loanRepository.FirstOrDefaultAsync(new GetAvailableBookByIdSpec(req.Id), ct);
|
||||
if (loan is not null)
|
||||
{
|
||||
await Send.StringAsync("Le livre est encore emprunté", 409, cancellation: ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await bookRepository.DeleteAsync(book, ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user