Files
ApiEfCoreLibrary/ApiEfCoreLibrary/Endpoints/User/GetAllUsersEndpoint.cs
2025-11-17 22:23:53 +01:00

51 lines
1.9 KiB
C#

using ApiEfCoreLibrary.DTO.Loan.Response;
using ApiEfCoreLibrary.DTO.User.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.User;
public class GetAllUsersEndpoint(LibraryDbContext database) : EndpointWithoutRequest<List<GetUserDto>>
{
public override void Configure()
{
Get("/api/users");
Roles("viewer", "admin", "librarian"); }
public override async Task HandleAsync(CancellationToken ct)
{
List<GetUserDto> user = await database.Users
.Include(x => x.Loans)!
.ThenInclude(l => l.Book)
.ThenInclude(b => b!.Author)
.Select(user => new GetUserDto()
{
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,
BookAuthorId = loan.Book.AuthorId,
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()
})
.ToListAsync(ct);
await Send.OkAsync(user, ct);
}
}