created all endpoints

This commit is contained in:
2026-03-09 22:31:49 +01:00
parent 22014f6878
commit 679c552a3f
5 changed files with 177 additions and 2 deletions

View File

@@ -0,0 +1,30 @@
using BookHive.DTO.Book;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Books;
using FastEndpoints;
namespace BookHive.Endpoints.Books;
public class UpdateBookEndpoit(BookRepository bookRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateBookDto>
{
public override void Configure()
{
Put("/books/{@Id}/", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateBookDto req, CancellationToken ct)
{
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.Id), ct);
if (book is null)
{
await Send.NotFoundAsync(ct);
return;
}
mapper.Map(req, book);
await bookRepository.SaveChangesAsync(ct);
await Send.OkAsync(cancellation: ct);
}
}

View File

@@ -0,0 +1,35 @@
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Loans;
using FastEndpoints;
namespace BookHive.Endpoints.Loans;
public class PatchReturnLoanRequest
{
public int Id { get; set; }
public DateOnly ReturnDate { get; set; }
}
public class PatchReturnLoanEndpoint(LoanRepository loanRepository) : Endpoint<PatchReturnLoanRequest>
{
public override void Configure()
{
Patch("/loans/{@Id}/return/", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(PatchReturnLoanRequest req, CancellationToken ct)
{
Loan? loan = await loanRepository.SingleOrDefaultAsync(new GetLoanByIdSpec(req.Id), ct);
if (loan is null)
{
await Send.NotFoundAsync(ct);
await Send.OkAsync(cancellation: ct);
}
loan?.ReturnDate = req.ReturnDate;
await loanRepository.SaveChangesAsync(ct);
await Send.OkAsync(cancellation: ct);
}
}

View File

@@ -0,0 +1,30 @@
using BookHive.DTO.Member;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Members;
using FastEndpoints;
namespace BookHive.Endpoints.Members;
public class UpdateMemberEndpoint(MemberRepository memberRepository, AutoMapper.IMapper mapper) : Endpoint<UpdateMemberDto>
{
public override void Configure()
{
Put("/members/{@Id}/", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateMemberDto req, CancellationToken ct)
{
Member? member = await memberRepository.SingleOrDefaultAsync(new GetMemberByIdSpec(req.Id), ct);
if (member is null)
{
await Send.NotFoundAsync(ct);
return;
}
mapper.Map(req, member);
await memberRepository.SaveChangesAsync(ct);
await Send.OkAsync(cancellation: ct);
}
}

View File

@@ -0,0 +1,42 @@
using BookHive.DTO.Review;
using BookHive.Models;
using BookHive.Repositories;
using BookHive.Specifications.Books;
using BookHive.Specifications.Members;
using FastEndpoints;
namespace BookHive.Endpoints.Reviews;
public class CreateReviewEndpoint(
ReviewRepository reviewRepository,
MemberRepository memberRepository,
BookRepository bookRepository,
AutoMapper.IMapper mapper)
: Endpoint<CreateReviewDto>
{
public override void Configure()
{
Post("/books/{@BookId}/reviews/");
AllowAnonymous();
}
public override async Task HandleAsync(CreateReviewDto req, CancellationToken ct)
{
Book? book = await bookRepository.SingleOrDefaultAsync(new GetBookByIdSpec(req.BookId), ct);
if (book is null)
{
await Send.NotFoundAsync(ct);
return;
}
Member? member = await memberRepository.SingleOrDefaultAsync(new GetMemberByIdSpec(req.MemberId), ct);
if (member is null)
{
await Send.NotFoundAsync(ct);
return;
}
await reviewRepository.AddAsync(mapper.Map<Review>(req), ct);
await Send.OkAsync(cancellation: ct);
}
}