Merge pull request 'Endpoints' (#7) from endpoints into main
Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using PostFundManagement.Application.Extensions;
|
||||
using PostFundManagement.Application.Services.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Domain.Security;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public sealed class GetEvidenceEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/evidence/{evidenceId}", async (string evidenceId, EvidenceService service,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var result = await service.GetEvidenceAsync(evidenceId, cancellationToken);
|
||||
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.NotFound();
|
||||
|
||||
})
|
||||
.WithDescription("Get a specific evidence record and assiciated file URLs")
|
||||
.WithName(typeof(GetEvidenceEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Domain.Models.Evidence>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence)
|
||||
.CacheOutput();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -8,7 +8,7 @@ using PostFundManagement.Domain.Security;
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public sealed class GetIndicatorByIdEndpoint : IEndpoint
|
||||
public sealed class GetIndicatorEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
@@ -22,7 +22,7 @@ public sealed class GetIndicatorByIdEndpoint : IEndpoint
|
||||
: Results.NotFound();
|
||||
})
|
||||
.WithDescription("Get a specific indicator.")
|
||||
.WithName(typeof(GetIndicatorByIdEndpoint).ToEndpointName())
|
||||
.WithName(typeof(GetIndicatorEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Indicator>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
@@ -0,0 +1,33 @@
|
||||
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 class GetMilestoneEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/evidence/milestones/{milestoneId}", async (string milestoneId, EvidenceService service,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var result = await service.GetMilestoneAsync(milestoneId, cancellationToken);
|
||||
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.NotFound();
|
||||
})
|
||||
.WithDescription("Gets a specific milestone")
|
||||
.WithName(typeof(GetMilestoneEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Milestone>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence)
|
||||
.CacheOutput();
|
||||
}
|
||||
}
|
||||
@@ -253,4 +253,51 @@ public sealed class EvidenceService(IDbContextFactory<ApplicationDbContext> cont
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<Milestone>> GetMilestoneAsync(string milestoneId, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var decodeResult = hashService.DecodeLongIdHash(milestoneId);
|
||||
|
||||
if (decodeResult.IsFailed)
|
||||
return Result.Fail(decodeResult.Errors);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var milestone = await context.Milestones.AsNoTracking().FirstOrDefaultAsync(m => m.Id == decodeResult.Value, cancellationToken);
|
||||
|
||||
return milestone is not null
|
||||
? Result.Ok(milestone.Map())
|
||||
: Result.Fail("Failed to get specific milestone");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<Domain.Models.Evidence>> GetEvidenceAsync(string evidenceId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var decodeResult = hashService.DecodeLongIdHash(evidenceId);
|
||||
|
||||
if (decodeResult.IsFailed)
|
||||
return Result.Fail(decodeResult.Errors);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var evidence = await context.Evidences.AsNoTracking().FirstOrDefaultAsync(e => e.Id == decodeResult.Value, cancellationToken);
|
||||
|
||||
return evidence is not null
|
||||
? Result.Ok(evidence.Map())
|
||||
: Result.Fail("Failed to get a specific evidence record.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user