Implemented GetIndicatorByIdEndpoint
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Evidence;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Models;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetIndicatorByIdEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/evidence/indicators/{indicatorId}", async (string indicatorId, EvidenceService service,
|
||||||
|
CancellationToken cancellationToken = default) =>
|
||||||
|
{
|
||||||
|
var result = await service.GetIndicatorAsync(indicatorId, cancellationToken);
|
||||||
|
|
||||||
|
return result.IsSuccess
|
||||||
|
? Results.Ok(result.Value)
|
||||||
|
: Results.NotFound();
|
||||||
|
})
|
||||||
|
.WithDescription("Get a specific indicator.")
|
||||||
|
.WithName(typeof(GetIndicatorByIdEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<Indicator>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.WithTags(EndpointTags.Evidence);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -230,4 +230,27 @@ public sealed class EvidenceService(IDbContextFactory<ApplicationDbContext> cont
|
|||||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Result<Indicator>> GetIndicatorAsync(string indicatorId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var decodeResult = hashService.DecodeLongIdHash(indicatorId);
|
||||||
|
|
||||||
|
if (decodeResult.IsFailed)
|
||||||
|
return Result.Fail(decodeResult.Errors);
|
||||||
|
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var indicator = await context.Indicators.AsNoTracking().FirstOrDefaultAsync(i => i.Id == decodeResult.Value, cancellationToken);
|
||||||
|
|
||||||
|
return indicator is not null
|
||||||
|
? Result.Ok(indicator.Map())
|
||||||
|
: Result.Fail("Failed to find specific indicator.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user