Added program control endpoints
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Projects;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class CreateProgrammeEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapPost("api/projects/programme", async (CreateProgrammeRequest request, HttpContext context,
|
||||||
|
IProjectService service, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.CreateProgrammeAsync(request, userId: context.GetUserId(), cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true } => Results.Ok(),
|
||||||
|
var r when r.HasError<NotFoundError>() => Results.NotFound(),
|
||||||
|
var r when r.HasError<ValidationError>() => Results.BadRequest(r.Errors.ToProblems()),
|
||||||
|
var r when r.HasError<CreateError>() => Results.BadRequest(),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Create programme")
|
||||||
|
.WithName(typeof(CreateProgrammeEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Projects);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,7 +21,6 @@ public class GetMilestonesEndpoint : IEndpoint
|
|||||||
{ IsSuccess: true, Value: var milestones } => Results.Ok(milestones),
|
{ IsSuccess: true, Value: var milestones } => Results.Ok(milestones),
|
||||||
var r => Results.InternalServerError(r.Errors.ToProblems())
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
})
|
})
|
||||||
.RequireAuthorization()
|
|
||||||
.WithDescription("Get a list of all milestones")
|
.WithDescription("Get a list of all milestones")
|
||||||
.WithName(typeof(GetMilestonesEndpoint).ToEndpointName())
|
.WithName(typeof(GetMilestonesEndpoint).ToEndpointName())
|
||||||
.MapToApiVersion(new ApiVersion(1))
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Models;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetProgrammeEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/projects/programme/{programmeId}", async (string programmeId, IProjectService service,
|
||||||
|
CancellationToken cancellationToken = default) =>
|
||||||
|
await service.GetProgrammeAsync(programmeId, cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true, Value: var programme } => Results.Ok(programme),
|
||||||
|
var r when r.HasError<NotFoundError>() => Results.NotFound(),
|
||||||
|
var r when r.HasError<IdDecodeError>() => Results.Forbid(),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Get specific programme")
|
||||||
|
.WithName(typeof(GetProgrammeEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<Programme>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Projects)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Models;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetProgrammeProjectsEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/projects/{programmeId}/programme", async (string programmeId,
|
||||||
|
IProjectService service, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.GetProgrammeProjectsAsync(programmeId, cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true, Value: var projects } => Results.Ok(projects),
|
||||||
|
var r when r.HasError<NotFoundError>() => Results.NotFound(),
|
||||||
|
var r when r.HasError<IdDecodeError>() => Results.Forbid(),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems()),
|
||||||
|
})
|
||||||
|
.WithDescription("Get a list of all projects under a programme")
|
||||||
|
.WithName(typeof(GetProgrammeProjectsEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<Project[]>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Projects)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Domain;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Models;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetProgrammesEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/projects/programme/{page:int}/{pageSize:int}", async (IProjectService service,
|
||||||
|
int page = 1, int pageSize = 100, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.GetProgrammesAsync(Pagination.Create(page, pageSize), cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true, Value: var programmes } => Results.Ok(programmes),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Get a list of all programmes")
|
||||||
|
.WithName(typeof(GetProgrammesEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<Programme[]>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Projects)
|
||||||
|
.CacheOutput();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Projects;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Errors;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
using Problems = Microsoft.AspNetCore.Mvc.ProblemDetails;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Projects;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class UpdateProgrammeEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapPut("api/projects/programme", async (UpdateProgrammeRequest request, HttpContext context,
|
||||||
|
IProjectService service, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.UpdateProgrammeAsync(request, userId: context.GetUserId(), cancellationToken) switch
|
||||||
|
{
|
||||||
|
{ IsSuccess: true } => Results.Ok(),
|
||||||
|
var r when r.HasError<NotFoundError>() => Results.NotFound(),
|
||||||
|
var r when r.HasError<UpdateError>() => Results.BadRequest(r.Errors.ToProblems()),
|
||||||
|
var r => Results.InternalServerError(r.Errors.ToProblems())
|
||||||
|
})
|
||||||
|
.WithDescription("Update existing programme")
|
||||||
|
.WithName(typeof(UpdateProgrammeEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status403Forbidden)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.Produces<Problems>(StatusCodes.Status400BadRequest)
|
||||||
|
.Produces<Problems>(StatusCodes.Status500InternalServerError)
|
||||||
|
.WithTags(EndpointTags.Projects);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,4 +33,14 @@ public interface IProjectService
|
|||||||
ValueTask<Result<Domain.Models.Activity>> GetActivityAsync(string activityIdHash, CancellationToken cancellationToken = default);
|
ValueTask<Result<Domain.Models.Activity>> GetActivityAsync(string activityIdHash, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
ValueTask<Result<Risk[]>> GetProjectRisksAsync(string projectIdHash, CancellationToken cancellationToken = default);
|
ValueTask<Result<Risk[]>> GetProjectRisksAsync(string projectIdHash, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<Result> CreateProgrammeAsync(CreateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<Result> UpdateProgrammeAsync(UpdateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<Result<Programme>> GetProgrammeAsync(string programmeIdHash, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<Result<Programme[]>> GetProgrammesAsync(Pagination pagination, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
ValueTask<Result<Project[]>> GetProgrammeProjectsAsync(string programmeIdHash, CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,14 @@
|
|||||||
<PackageLicenseFile>..\LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>..\LICENSE</PackageLicenseFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<NoWarn>1701;1702;AV0029;AV0030;</NoWarn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<NoWarn>1701;1702;AV0029;AV0030;</NoWarn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\PostFundManagement.Domain\PostFundManagement.Domain.csproj" />
|
<ProjectReference Include="..\PostFundManagement.Domain\PostFundManagement.Domain.csproj" />
|
||||||
<ProjectReference Include="..\PostFundManagement.Infrastructure\PostFundManagement.Infrastructure.csproj" />
|
<ProjectReference Include="..\PostFundManagement.Infrastructure\PostFundManagement.Infrastructure.csproj" />
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ public sealed class ProjectCacheService(ProjectService service, IDistributedCach
|
|||||||
public async ValueTask<Result> CreateActivityAsync(CreateActivityRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> CreateActivityAsync(CreateActivityRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await service.CreateActivityAsync(request, userId, cancellationToken);
|
await service.CreateActivityAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
|
public async ValueTask<Result> CreateProgrammeAsync(CreateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.CreateProgrammeAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
public async ValueTask<Result> CreateProjectAsync(CreateProjectRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> CreateProjectAsync(CreateProjectRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await service.CreateProjectAsync(request, userId, cancellationToken);
|
await service.CreateProjectAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
@@ -99,6 +102,69 @@ public sealed class ProjectCacheService(ProjectService service, IDistributedCach
|
|||||||
return Result.Ok(cached.FromJson<Milestone[]>());
|
return Result.Ok(cached.FromJson<Milestone[]>());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Programme>> GetProgrammeAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Programme>>>)(async () =>
|
||||||
|
{
|
||||||
|
var cacheKey = HashService.ToMd5Hash($"getprogramme-{programmeIdHash}").Value;
|
||||||
|
|
||||||
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
||||||
|
|
||||||
|
if(string.IsNullOrWhiteSpace(cached))
|
||||||
|
{
|
||||||
|
var result = await service.GetProgrammeAsync(programmeIdHash, cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
||||||
|
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Ok(cached!.FromJson<Programme>());
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Project[]>> GetProgrammeProjectsAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Project[]>>>)(async () =>
|
||||||
|
{
|
||||||
|
var cacheKey = HashService.ToMd5Hash($"getprogrammeprojects-{programmeIdHash}").Value;
|
||||||
|
|
||||||
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
||||||
|
|
||||||
|
if(string.IsNullOrWhiteSpace(cached))
|
||||||
|
{
|
||||||
|
var result = await service.GetProgrammeProjectsAsync(programmeIdHash, cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
||||||
|
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Ok(cached.FromJson<Project[]>());
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Programme[]>> GetProgrammesAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Programme[]>>>)(async () =>
|
||||||
|
{
|
||||||
|
var cacheKey = HashService.ToMd5Hash($"getprogrammes-{pagination.Index}-{pagination.PageSize}-{pagination.Page}").Value;
|
||||||
|
|
||||||
|
var cached = await cache.GetStringAsync(cacheKey, cancellationToken);
|
||||||
|
|
||||||
|
if(string.IsNullOrWhiteSpace(cached))
|
||||||
|
{
|
||||||
|
var result = await service.GetProgrammesAsync(pagination, cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsFailed) return result;
|
||||||
|
|
||||||
|
await cache.SetStringAsync(cacheKey, result.Value.ToJson(), cacheOptions, cancellationToken);
|
||||||
|
|
||||||
|
return result.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Ok(cached.FromJson<Programme[]>());
|
||||||
|
}));
|
||||||
|
|
||||||
public async ValueTask<Result<Project>> GetProjectAsync(string projectIdHash, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result<Project>> GetProjectAsync(string projectIdHash, CancellationToken cancellationToken = default) =>
|
||||||
await Result.Try((Func<ValueTask<Result<Project>>>)(async () =>
|
await Result.Try((Func<ValueTask<Result<Project>>>)(async () =>
|
||||||
{
|
{
|
||||||
@@ -207,6 +273,9 @@ public sealed class ProjectCacheService(ProjectService service, IDistributedCach
|
|||||||
public async ValueTask<Result> UpdateMilestoneAsync(UpdateMilestoneRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> UpdateMilestoneAsync(UpdateMilestoneRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await service.UpdateMilestoneAsync(request, userId, cancellationToken);
|
await service.UpdateMilestoneAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
|
public async ValueTask<Result> UpdateProgrammeAsync(UpdateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
|
await service.UpdateProgrammeAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
public async ValueTask<Result> UpdateProjectAsync(UpdateProjectRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> UpdateProjectAsync(UpdateProjectRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await service.UpdateProjectAsync(request, userId, cancellationToken);
|
await service.UpdateProjectAsync(request, userId, cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -284,4 +284,97 @@ public sealed class ProjectService(IDbContextFactory<ApplicationDbContext> conte
|
|||||||
|
|
||||||
return Result.Ok(risks.Select(i => i.Map()).ToArray());
|
return Result.Ok(risks.Select(i => i.Map()).ToArray());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result> CreateProgrammeAsync(CreateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result>>)(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Users.AnyAsync(u => u.Id == userId, cancellationToken))
|
||||||
|
return Result.Fail(NotFoundError.New);
|
||||||
|
|
||||||
|
if (await context.Programmes.AnyAsync(p => p.Name == request.Name, cancellationToken))
|
||||||
|
return Result.Fail(ValidationError.Create("Programme of the same name exists"));
|
||||||
|
|
||||||
|
context.Programmes.Add(new Domain.Entities.Programme
|
||||||
|
{
|
||||||
|
OwnedBy = request.OwnerId,
|
||||||
|
CreatedBy = userId,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
Status = ApprovalStatus.Pending
|
||||||
|
});
|
||||||
|
|
||||||
|
return Result.FailIf(await context.SaveChangesAsync(cancellationToken) <= 0, CreateError.New);
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result> UpdateProgrammeAsync(UpdateProgrammeRequest request, Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result>>)(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Users.AnyAsync(u => u.Id == userId, cancellationToken))
|
||||||
|
return Result.Fail(NotFoundError.New);
|
||||||
|
|
||||||
|
var changedTotal = await context.Programmes.Where(p => p.Id == request.ProgrammeId)
|
||||||
|
.ExecuteUpdateAsync(f => f
|
||||||
|
.SetProperty(u => u.Name, request.Name)
|
||||||
|
.SetProperty(u => u.OwnedBy, request.OwnerId)
|
||||||
|
.SetProperty(u => u.Status, request.Status)
|
||||||
|
.SetProperty(u => u.UpdatedBy, userId)
|
||||||
|
.SetProperty(u => u.UpdatedAt, DateTime.UtcNow), cancellationToken);
|
||||||
|
|
||||||
|
return Result.FailIf(changedTotal <= 0, UpdateError.New);
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Programme>> GetProgrammeAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Programme>>>)(async () =>
|
||||||
|
{
|
||||||
|
var decodeResult = hashService.DecodeLongIdHash(programmeIdHash);
|
||||||
|
|
||||||
|
if (decodeResult.IsFailed) return Result.Fail(IdDecodeError.Create()
|
||||||
|
.CausedBy(decodeResult.Errors));
|
||||||
|
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var programme = await context.Programmes.FirstOrDefaultAsync(p => p.Id == decodeResult.Value, cancellationToken);
|
||||||
|
|
||||||
|
return programme is not null
|
||||||
|
? Result.Ok(programme.Map())
|
||||||
|
: Result.Fail(NotFoundError.New);
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Programme[]>> GetProgrammesAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Programme[]>>>)(async () =>
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var programmes = await context.Programmes.AsNoTracking()
|
||||||
|
.OrderByDescending(m => m.CreatedAt)
|
||||||
|
.Skip((pagination.Page - 1) * pagination.PageSize)
|
||||||
|
.Take(pagination.PageSize)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok(programmes.Select(i => i.Map()).ToArray());
|
||||||
|
}));
|
||||||
|
|
||||||
|
public async ValueTask<Result<Project[]>> GetProgrammeProjectsAsync(string programmeIdHash, CancellationToken cancellationToken = default) =>
|
||||||
|
await Result.Try((Func<ValueTask<Result<Project[]>>>)(async () =>
|
||||||
|
{
|
||||||
|
var decodeResult = hashService.DecodeLongIdHash(programmeIdHash);
|
||||||
|
|
||||||
|
if (decodeResult.IsFailed) return Result.Fail(IdDecodeError.Create()
|
||||||
|
.CausedBy(decodeResult.Errors));
|
||||||
|
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (!await context.Programmes.AnyAsync(p => p.Id == decodeResult.Value, cancellationToken))
|
||||||
|
return Result.Fail(NotFoundError.New);
|
||||||
|
|
||||||
|
var projects = await context.Projects.AsNoTracking()
|
||||||
|
.Where(p => p.ProgrammeId == decodeResult.Value)
|
||||||
|
.OrderByDescending(o => o.CreatedAt)
|
||||||
|
.ToListAsync(cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok(projects.Select(i => i.Map()).ToArray());
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Projects;
|
namespace PostFundManagement.Application.Services.Projects;
|
||||||
|
|
||||||
|
public record UpdateProgrammeRequest(long ProgrammeId, string Name, Guid OwnerId, ApprovalStatus Status);
|
||||||
|
|
||||||
|
public record CreateProgrammeRequest(string Name, Guid OwnerId);
|
||||||
|
|
||||||
public record UpdateProjectRequest(long ProjectId, string Name, string Description, DateTime StartedAt, DateTime? EndedAt, IndustrySector Sector, ProjectType ProjectType);
|
public record UpdateProjectRequest(long ProjectId, string Name, string Description, DateTime StartedAt, DateTime? EndedAt, IndustrySector Sector, ProjectType ProjectType);
|
||||||
|
|
||||||
public record UpdateMilestoneRequest(long MilestoneId, DateTime DueAt, string Name, Priority Priority, ApprovalStatus Status);
|
public record UpdateMilestoneRequest(long MilestoneId, DateTime DueAt, string Name, Priority Priority, ApprovalStatus Status);
|
||||||
|
|||||||
@@ -1,31 +1,10 @@
|
|||||||
using PostFundManagement.Application.Abstractions;
|
using PostFundManagement.Application.Abstractions;
|
||||||
using PostFundManagement.Application.Abstractions.Services;
|
using PostFundManagement.Application.Abstractions.Services;
|
||||||
using PostFundManagement.Domain.Errors;
|
|
||||||
|
|
||||||
namespace PostFundManagement.Application.Services.Shared;
|
namespace PostFundManagement.Application.Services.Shared;
|
||||||
|
|
||||||
public sealed class IdentityCacheService(IdentityService service, IDistributedCache cache) : ServiceCacheBase, IIdentityService
|
public sealed class IdentityCacheService(IdentityService service) : ServiceCacheBase, IIdentityService
|
||||||
{
|
{
|
||||||
public async ValueTask<Result> UserIsAuthorisedAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
public async ValueTask<Result> UserIsAuthorisedAsync(Guid userId, CancellationToken cancellationToken = default) =>
|
||||||
await Result.Try((Func<ValueTask<Result>>)(async () =>
|
await service.UserIsAuthorisedAsync(userId, cancellationToken);
|
||||||
{
|
|
||||||
var cacheKey = HashService.ToMd5Hash($"userisauthorised-{userId}").Value;
|
|
||||||
|
|
||||||
var cached = await cache.GetStringAsync(cacheKey);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(cached))
|
|
||||||
{
|
|
||||||
var result = await service.UserIsAuthorisedAsync(userId, cancellationToken); ;
|
|
||||||
|
|
||||||
if (result.IsFailed) return result;
|
|
||||||
|
|
||||||
await cache.SetStringAsync(cacheKey, "true", cacheOptions, cancellationToken);
|
|
||||||
|
|
||||||
return Result.Ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
return cached.Equals("true", StringComparison.OrdinalIgnoreCase)
|
|
||||||
? Result.Ok()
|
|
||||||
: Result.Fail(NotFoundError.New);
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user