63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using ApiEfCoreLibrary.DTO.Loan.Response;
|
|
using ApiEfCoreLibrary.DTO.User.Request;
|
|
using ApiEfCoreLibrary.DTO.User.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ApiEfCoreLibrary.Endpoints.User;
|
|
|
|
public class GetUserRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class GetUserEndpoint(LibraryDbContext database) : Endpoint<GetUserRequest, GetUserDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/users/{@Id}", x => new {x.Id});
|
|
}
|
|
|
|
public override async Task HandleAsync(GetUserRequest req, CancellationToken ct)
|
|
{
|
|
var user = await database.Users
|
|
.Include(x => x.Loans)!
|
|
.ThenInclude(l => l.Book)
|
|
.ThenInclude(b => b!.Author)
|
|
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
|
|
if (user == null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
GetUserDto responseDto = new()
|
|
{
|
|
Id = user.Id,
|
|
Name = user.Name,
|
|
FirstName = user.FirstName,
|
|
Email = user.Email,
|
|
BirthDate = user.BirthDate,
|
|
Loans = user.Loans?.Select(loan => new GetLoanDto
|
|
{
|
|
Id = loan.Id,
|
|
BookId = loan.BookId,
|
|
BookTitle = loan.Book.Title,
|
|
BookAuthorName = loan.Book.Author.Name,
|
|
BookAuthorFirstName = loan.Book.Author.FirstName,
|
|
BookReleaseYear = loan.Book.ReleaseYear,
|
|
BookIsbn = loan.Book.Isbn,
|
|
UserId = loan.UserId,
|
|
UserName = loan.User?.Name,
|
|
UserFirstName = loan.User?.FirstName,
|
|
UserEmail = loan.User?.Email,
|
|
Date = loan.Date,
|
|
PlannedReturningDate = loan.PlannedReturningDate,
|
|
EffectiveReturningDate = loan.EffectiveReturningDate
|
|
}).ToList()
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
} |