first commit
This commit is contained in:
34
BookHive/Endpoints/Reviews/DeleteReviewEndpoint.cs
Normal file
34
BookHive/Endpoints/Reviews/DeleteReviewEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Reviews;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Reviews;
|
||||
|
||||
public class DeleteReviewsRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteReviewEndpoint(ReviewRepository reviewRepository) : Endpoint<DeleteReviewsRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/reviews/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteReviewsRequest req, CancellationToken ct)
|
||||
{
|
||||
Review? review = await reviewRepository.SingleOrDefaultAsync(new GetReviewByIdSpec(req.Id), ct);
|
||||
|
||||
if (review is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await reviewRepository.DeleteAsync(review, ct);
|
||||
await Send.OkAsync(cancellation: ct);
|
||||
}
|
||||
}
|
||||
35
BookHive/Endpoints/Reviews/GetReviewEndpoint.cs
Normal file
35
BookHive/Endpoints/Reviews/GetReviewEndpoint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using BookHive.DTO.Review;
|
||||
using BookHive.Models;
|
||||
using BookHive.Repositories;
|
||||
using BookHive.Specifications.Books;
|
||||
using BookHive.Specifications.Reviews;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BookHive.Endpoints.Reviews;
|
||||
|
||||
public class ReviewRequest
|
||||
{
|
||||
public int BookId { get; set; }
|
||||
}
|
||||
|
||||
public class GetReviewEndpoint(BookRepository bookRepository, ReviewRepository reviewRepository) : Endpoint<ReviewRequest, List<GetReviewDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/books/{@BookId}/reviews", x => new { x.BookId });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(ReviewRequest req, CancellationToken ct)
|
||||
{
|
||||
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.BookId), ct);
|
||||
|
||||
if (book is null)
|
||||
{
|
||||
await Send.NoContentAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await Send.OkAsync(await reviewRepository.ProjectToListAsync<GetReviewDto>(new GetReviewByBookIdSpec(req.BookId), ct), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user