Refactored Communications and Evidence endpoints
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using PostFundManagement.Application.Communications;
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
using PostFundManagement.Domain.Models;
|
||||
using static PostFundManagement.Domain.Extensions.General;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Communications;
|
||||
|
||||
@@ -10,31 +14,20 @@ public class GetInvitesEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/communications/invites", async (IDbContextFactory<ApplicationDbContext> contextFactory,
|
||||
int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) =>
|
||||
builder.MapGet("api/communications/invites/{page:int}/{pageSize:int}", async (CommunicationsService service, int page = 1, int pageSize = 100,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 10;
|
||||
if (pageSize > 100) pageSize = 100;
|
||||
var result = await service.GetInvitesAsync(new Pagination { Page = page, PageSize = pageSize }, cancellationToken);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var query = context.Invites.AsNoTracking();
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
var items = await query.OrderByDescending(i => i.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Results.Ok(new { Items = items, TotalCount = totalCount, Page = page, PageSize = pageSize });
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to get invites"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Get a list of sent invitations")
|
||||
.WithName(typeof(GetInvitesEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces<Invite[]>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Communications);
|
||||
}
|
||||
|
||||
@@ -1,55 +1,23 @@
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Application.Communications;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Communications;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class SendInviteEndpoint : IEndpoint
|
||||
{
|
||||
public record InviteRequest(long OrganisationId, long InvitedOrganisationId, long AwardId, long ContractId, Guid Recipient, string Message, NotificationPlatform Platform);
|
||||
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/communications/invite", async (InviteRequest request, ClaimsPrincipal principal,
|
||||
IDbContextFactory<ApplicationDbContext> contextFactory, CancellationToken cancellationToken = default) =>
|
||||
CommunicationsService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var sidClaim = principal.FindFirst("sid")?.Value ?? principal.FindFirst(ClaimTypes.Sid)?.Value;
|
||||
var result = await service.SendInviteAsync(request, principal, cancellationToken);
|
||||
|
||||
if (!Guid.TryParse(sidClaim, out var currentUserId))
|
||||
return Results.BadRequest("Missing or invalid 'sid' claim.");
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var invite = new Domain.Entities.Invite
|
||||
{
|
||||
OrganisationId = request.OrganisationId,
|
||||
InvitedOrganisationId = request.InvitedOrganisationId,
|
||||
AwardId = request.AwardId,
|
||||
ContractId = request.ContractId,
|
||||
Recipient = request.Recipient,
|
||||
CreatedBy = currentUserId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
context.Invites.Add(invite);
|
||||
|
||||
context.Notifications.Add(new Domain.Entities.Notification
|
||||
{
|
||||
OrganisationId = request.OrganisationId,
|
||||
Recipient = request.Recipient,
|
||||
Platform = request.Platform,
|
||||
Status = NotificationStatus.Pending,
|
||||
Subject = "Invitation to Onboard",
|
||||
Message = request.Message,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Results.Ok(invite)
|
||||
: Results.BadRequest("Failed to create invite");
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to create invite"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Trigger an invite to an organization or candidate (specifying NotificationPlatform)")
|
||||
|
||||
+5
-19
@@ -1,41 +1,27 @@
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Events.Communications;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Communications;
|
||||
|
||||
public class SyncEvent : EventBase, IEvent
|
||||
{
|
||||
public string Name { get; set; } = nameof(SyncEvent);
|
||||
}
|
||||
|
||||
public class SyncEventHandler : INotificationHandler<SyncEvent>
|
||||
{
|
||||
public ValueTask Handle(SyncEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
Trace.WriteLine($"Integration Sync executed. Correlation ID: {notification.CorrelationId}");
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class SyncEndpoint : IEndpoint
|
||||
public class SyncUserEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/integration/sync", async (IJobOrchestrator jobOrchestrator,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var syncEvent = new SyncEvent();
|
||||
var syncEvent = new SyncUserEvent();
|
||||
|
||||
await jobOrchestrator.SendAsync(syncEvent, cancellationToken);
|
||||
|
||||
return Results.Ok(new { Queued = true, syncEvent.CorrelationId });
|
||||
return Results.Ok();
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Trigger integration and data synchronization in the background")
|
||||
.WithName(typeof(SyncEndpoint).ToEndpointName())
|
||||
.WithName(typeof(SyncUserEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
@@ -1,54 +1,30 @@
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
using PostFundManagement.Domain.Models;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class CreateIndicatorEndpoint : IEndpoint
|
||||
{
|
||||
public record CreateIndicatorRequest(long ProjectId, string Name, UnitOfMeasure UnitOfMeasure, decimal BaselineAmount, decimal TargetAmount);
|
||||
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/evidence/indicators", async (CreateIndicatorRequest request, ClaimsPrincipal principal,
|
||||
IDbContextFactory<ApplicationDbContext> contextFactory, CancellationToken cancellationToken = default) =>
|
||||
EvidenceService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var sidClaim = principal.FindFirst("sid")?.Value ?? principal.FindFirst(ClaimTypes.Sid)?.Value;
|
||||
var result = await service.CreateIndicatorAsync(request, principal, cancellationToken);
|
||||
|
||||
if (!Guid.TryParse(sidClaim, out var userId))
|
||||
return Results.BadRequest("Missing or invalid 'sid' claim.");
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Projects.AnyAsync(p => p.Id == request.ProjectId, cancellationToken))
|
||||
return Results.BadRequest($"Project with ID {request.ProjectId} does not exist.");
|
||||
|
||||
var indicator = new Domain.Entities.Indicator
|
||||
{
|
||||
ProjectId = request.ProjectId,
|
||||
Name = request.Name,
|
||||
UnitOfMeasure = request.UnitOfMeasure,
|
||||
BaselineAmount = request.BaselineAmount,
|
||||
TargetAmount = request.TargetAmount,
|
||||
ActualAmount = request.BaselineAmount,
|
||||
CreatedBy = userId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
context.Indicators.Add(indicator);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Results.Created($"api/evidence/indicators/{indicator.Id}", indicator)
|
||||
: Results.BadRequest("Failed to create indicator");
|
||||
return result.IsSuccess
|
||||
? Results.Created($"api/evidence/indicators/{result.Value.Id}", result.Value.Indicator)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to create indicator"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Define indicators and baselines/targets (M08)")
|
||||
.WithName(typeof(CreateIndicatorEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<PostFundManagement.Domain.Entities.Indicator>(StatusCodes.Status201Created)
|
||||
.Produces<Indicator>(StatusCodes.Status201Created)
|
||||
.Produces(StatusCodes.Status400BadRequest)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
|
||||
@@ -1,53 +1,30 @@
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
using PostFundManagement.Domain.Models;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class CreateMilestoneEndpoint : IEndpoint
|
||||
{
|
||||
public record CreateMilestoneRequest(long ProjectId, DateTime DueAt, string Name, Priority Priority);
|
||||
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/evidence/milestones", async (CreateMilestoneRequest request, ClaimsPrincipal principal,
|
||||
IDbContextFactory<ApplicationDbContext> contextFactory, CancellationToken cancellationToken = default) =>
|
||||
EvidenceService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var sidClaim = principal.FindFirst("sid")?.Value ?? principal.FindFirst(ClaimTypes.Sid)?.Value;
|
||||
var result = await service.CreateMilestoneAsync(request, principal, cancellationToken);
|
||||
|
||||
if (!Guid.TryParse(sidClaim, out var userId))
|
||||
return Results.BadRequest("Missing or invalid 'sid' claim.");
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
if (!await context.Projects.AnyAsync(p => p.Id == request.ProjectId, cancellationToken))
|
||||
return Results.BadRequest($"Project with ID {request.ProjectId} does not exist.");
|
||||
|
||||
var milestone = new Domain.Entities.Milestone
|
||||
{
|
||||
ProjectId = request.ProjectId,
|
||||
DueAt = request.DueAt,
|
||||
Name = request.Name,
|
||||
Priority = request.Priority,
|
||||
Status = ApprovalStatus.Pending,
|
||||
CreatedBy = userId,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
context.Milestones.Add(milestone);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Results.Created($"api/evidence/milestones/{milestone.Id}", milestone)
|
||||
: Results.BadRequest("Failed to create a new milestone");
|
||||
return result.IsSuccess
|
||||
? Results.Created($"api/evidence/milestones/{result.Value.Id}", result.Value.Milestone)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to create a new milestone"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Define contractual milestones (M07)")
|
||||
.WithName(typeof(CreateMilestoneRequest).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Domain.Entities.Milestone>(StatusCodes.Status201Created)
|
||||
.Produces<Milestone>(StatusCodes.Status201Created)
|
||||
.Produces(StatusCodes.Status400BadRequest)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
@@ -10,31 +10,19 @@ 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) =>
|
||||
builder.MapGet("api/evidence/files/{page:int}/{pageSize:int}", async (EvidenceService service, int page = 1, int pageSize = 100, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 10;
|
||||
if (pageSize > 100) pageSize = 100;
|
||||
var result = await service.GetEvidenceFilesAsync(new Domain.Pagination(page, pageSize), cancellationToken);
|
||||
|
||||
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 });
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to get evidence"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Get a list of all uploaded evidence files metadata")
|
||||
.WithName(typeof(GetEvidenceFilesEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces<Domain.Models.Evidence[]>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
|
||||
@@ -1,42 +1,29 @@
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
using PostFundManagement.Domain.Models;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class GetIndicatorsEndpoint : IEndpoint
|
||||
{
|
||||
public record RecordActualRequest(decimal ActualAmount);
|
||||
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/evidence/indicators", async (IDbContextFactory<ApplicationDbContext> contextFactory,
|
||||
int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) =>
|
||||
builder.MapGet("api/evidence/indicators/{page:int}/{pageSize:int}", async (EvidenceService service, int page = 1, int pageSize = 100, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 10;
|
||||
if (pageSize > 100) pageSize = 100;
|
||||
var result = await service.GetIndicatorsAsync(new Domain.Pagination { Page = page, PageSize = pageSize }, cancellationToken);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var query = context.Indicators.AsNoTracking();
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
var items = await query.OrderByDescending(i => i.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Results.Ok(new { Items = items, TotalCount = totalCount, Page = page, PageSize = pageSize });
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to get indicators"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Get a list of indicators")
|
||||
.WithName(typeof(GetIndicatorsEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces<Indicator[]>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Models;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
@@ -10,31 +11,20 @@ public class GetMilestonesEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/evidence/milestones", async (IDbContextFactory<ApplicationDbContext> contextFactory,
|
||||
builder.MapGet("api/evidence/milestones/{page:int}/{pageSize:int}", async (EvidenceService service,
|
||||
int page = 1, int pageSize = 10, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 10;
|
||||
if (pageSize > 100) pageSize = 100;
|
||||
var result = await service.GetMilestonesAsync(new Domain.Pagination { Page = page, PageSize = pageSize }, cancellationToken);
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var query = context.Milestones.AsNoTracking();
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
var items = await query.OrderByDescending(m => m.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Results.Ok(new { Items = items, TotalCount = totalCount, Page = page, PageSize = pageSize });
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.BadRequest(result.Errors.ToProblems("Failed to fetch milestones"));
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Get a list of all milestones")
|
||||
.WithName(typeof(GetMilestonesEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces<Milestone[]>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Configuration.Entities;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
@@ -11,23 +12,21 @@ public class GetProjectMilestonesEndpoint : IEndpoint
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapGet("api/evidence/milestones/project/{projectId:long}", async (long projectId,
|
||||
IDbContextFactory<ApplicationDbContext> contextFactory, CancellationToken cancellationToken = default) =>
|
||||
EvidenceService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var result = await service.GetMilestonesByProjectIdAsync(projectId, cancellationToken);
|
||||
|
||||
var milestones = await context.Milestones.AsNoTracking()
|
||||
.Where(m => m.ProjectId == projectId)
|
||||
.OrderBy(m => m.DueAt)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Results.Ok(milestones);
|
||||
return result.IsSuccess
|
||||
? Results.Ok(result.Value)
|
||||
: Results.NotFound();
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Get a list of milestones for a project")
|
||||
.WithName(typeof(GetProjectMilestonesEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces<Milestone[]>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
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 RecordIndicatorActualAmountEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/evidence/indicators/{id:long}/actuals/{actualAmount:decimal}", async (long id, decimal actualAmount,
|
||||
ClaimsPrincipal principal, IDbContextFactory<ApplicationDbContext> contextFactory, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var indicator = await context.Indicators.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
|
||||
|
||||
if (indicator is null)
|
||||
return Results.NotFound($"Indicator with ID {id} does not exist.");
|
||||
|
||||
indicator.ActualAmount = actualAmount;
|
||||
|
||||
context.Indicators.Update(indicator);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Results.Ok(indicator)
|
||||
: Results.BadRequest("Failed to record actual amount");
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Record actual values for indicators (M08)")
|
||||
.WithName(typeof(RecordIndicatorActualAmountEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Domain.Entities.Indicator>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
[ApiVersionTarget(1)]
|
||||
public class RecordIndicatorAmountEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/evidence/indicators/{id:string}/actuals/{actualAmount:decimal}", async (string id, decimal actualAmount,
|
||||
ClaimsPrincipal principal, EvidenceService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var result = await service.RecordIndicatorActualAmountAsync(id, actualAmount, principal, cancellationToken);
|
||||
|
||||
return result.IsSuccess
|
||||
? Results.Ok()
|
||||
: Results.BadRequest("Failed to record actual amount");
|
||||
})
|
||||
.RequireAuthorization()
|
||||
.WithDescription("Record actual values for indicators (M08)")
|
||||
.WithName(typeof(RecordIndicatorAmountEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<Domain.Entities.Indicator>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status404NotFound)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Application.Evidence;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Api;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
using PostFundManagement.Infrastructure.Database;
|
||||
|
||||
namespace PostFundManagement.Api.Endpoints.Evidence;
|
||||
|
||||
@@ -11,8 +10,7 @@ public class UploadEvidenceFilesEndpoint : IEndpoint
|
||||
{
|
||||
public void Map(IEndpointRouteBuilder builder)
|
||||
{
|
||||
builder.MapPost("api/evidence/files", async (HttpRequest request, ClaimsPrincipal principal, IDbContextFactory<ApplicationDbContext> contextFactory,
|
||||
[FromKeyedServices(Constants.EvidenceS3SettingsSection)] IS3Service s3Service, CancellationToken cancellationToken = default) =>
|
||||
builder.MapPost("api/evidence/files", async (HttpRequest request, ClaimsPrincipal principal, EvidenceService service, CancellationToken cancellationToken = default) =>
|
||||
{
|
||||
var sidClaim = principal.FindFirst("sid")?.Value ?? principal.FindFirst(ClaimTypes.Sid)?.Value;
|
||||
|
||||
@@ -28,41 +26,10 @@ public class UploadEvidenceFilesEndpoint : IEndpoint
|
||||
if (file == null || file.Length == 0)
|
||||
return Results.BadRequest("No file uploaded or file is empty.");
|
||||
|
||||
_ = long.TryParse(form["projectId"], out var projectId);
|
||||
_ = long.TryParse(form["milestoneId"], out var milestoneId);
|
||||
_ = long.TryParse(form["indicatorId"], out var indicatorId);
|
||||
_ = long.TryParse(form["activityId"], out var activityId);
|
||||
_ = Enum.TryParse<EvidenceType>(form["type"], out var type);
|
||||
var result = await service.UploadEvidenceFileAsync(userId, form, file, cancellationToken);
|
||||
|
||||
using var stream = file.OpenReadStream();
|
||||
|
||||
var uploadResult = await s3Service.UploadFileAsync(file.FileName, stream, file.ContentType, cancellationToken);
|
||||
|
||||
if (uploadResult.IsFailed)
|
||||
return Results.BadRequest(uploadResult.Errors.FirstOrDefault()?.Message ?? "File upload failed.");
|
||||
|
||||
var documentUrl = uploadResult.Value;
|
||||
|
||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var evidence = new Domain.Entities.Evidence
|
||||
{
|
||||
ProjectId = projectId,
|
||||
MilestoneId = milestoneId,
|
||||
IndicatorId = indicatorId,
|
||||
ActivityId = activityId,
|
||||
CreatedBy = userId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
Version = 1,
|
||||
Type = type,
|
||||
DocumentUrl = documentUrl,
|
||||
Status = ApprovalStatus.Pending
|
||||
};
|
||||
|
||||
context.Evidences.Add(evidence);
|
||||
|
||||
return await context.SaveChangesAsync(cancellationToken) > 0
|
||||
? Results.Created($"api/evidence/files/{evidence.Id}", evidence)
|
||||
return result.IsSuccess
|
||||
? Results.Created()
|
||||
: Results.BadRequest("Evidence upload failed");
|
||||
})
|
||||
.RequireAuthorization()
|
||||
@@ -70,7 +37,7 @@ public class UploadEvidenceFilesEndpoint : IEndpoint
|
||||
.WithDescription("Upload supporting evidence file to S3 and register metadata (M14)")
|
||||
.WithName(typeof(UploadEvidenceFilesEndpoint).ToEndpointName())
|
||||
.MapToApiVersion(new ApiVersion(1))
|
||||
.Produces<PostFundManagement.Domain.Entities.Evidence>(StatusCodes.Status201Created)
|
||||
.Produces(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status400BadRequest)
|
||||
.Produces(StatusCodes.Status401Unauthorized)
|
||||
.WithTags(EndpointTags.Evidence);
|
||||
|
||||
Reference in New Issue
Block a user