42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using PostFundManagement.Domain.Abstractions;
|
|
using PostFundManagement.Domain.Api;
|
|
using PostFundManagement.Domain.Extensions;
|
|
using PostFundManagement.Infrastructure.Database;
|
|
|
|
namespace PostFundManagement.Api.Endpoints.Evidence;
|
|
|
|
[ApiVersionTarget(1)]
|
|
public class GetEvidenceFilesEndpoint : IEndpoint
|
|
{
|
|
public void Map(IEndpointRouteBuilder builder)
|
|
{
|
|
builder.MapGet("api/evidence/files", async (IDbContextFactory<ApplicationDbContext> contextFactory,
|
|
int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) =>
|
|
{
|
|
if (page < 1) page = 1;
|
|
if (pageSize < 1) pageSize = 10;
|
|
if (pageSize > 100) pageSize = 100;
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
|
|
|
var query = context.Evidences.AsNoTracking();
|
|
|
|
var totalCount = await query.CountAsync(cancellationToken);
|
|
|
|
var items = await query.OrderByDescending(e => e.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return Results.Ok(new { Items = items, TotalCount = totalCount, Page = page, PageSize = pageSize });
|
|
})
|
|
.RequireAuthorization()
|
|
.WithDescription("Get a list of all uploaded evidence files metadata")
|
|
.WithName(typeof(GetEvidenceFilesEndpoint).ToEndpointName())
|
|
.MapToApiVersion(new ApiVersion(1))
|
|
.Produces(StatusCodes.Status200OK)
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.WithTags(EndpointTags.Evidence);
|
|
}
|
|
}
|