Compare commits
2
Commits
0a1378fe25
...
e274c33592
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e274c33592 | ||
|
|
a218b19875 |
@@ -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);
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,19 @@ public sealed class Portfolio : IEntityTypeConfiguration<Domain.Entities.Portfol
|
|||||||
builder.ToTable(nameof(Domain.Entities.Portfolio).Pluralize());
|
builder.ToTable(nameof(Domain.Entities.Portfolio).Pluralize());
|
||||||
|
|
||||||
builder.HasKey(pk => pk.Id);
|
builder.HasKey(pk => pk.Id);
|
||||||
|
builder.Property(f => f.OrganisationId).IsRequired();
|
||||||
builder.Property(f => f.CreatedBy).IsRequired();
|
builder.Property(f => f.CreatedBy).IsRequired();
|
||||||
builder.Property(f => f.OwnedBy).IsRequired();
|
builder.Property(f => f.OwnedBy).IsRequired();
|
||||||
builder.Property(f => f.CreatedAt).IsRequired().HasDefaultValueSql("now()");
|
builder.Property(f => f.CreatedAt).IsRequired().HasDefaultValueSql("now()");
|
||||||
builder.Property(f => f.Name).IsRequired().HasMaxLength(LabelLength);
|
builder.Property(f => f.Name).IsRequired().HasMaxLength(LabelLength);
|
||||||
builder.Property(f => f.Description).IsRequired(false).HasMaxLength(TextLength);
|
builder.Property(f => f.Description).IsRequired(false).HasMaxLength(TextLength);
|
||||||
|
|
||||||
|
builder.HasOne(f => f.Organisation)
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey(fk => fk.OrganisationId)
|
||||||
|
.IsRequired()
|
||||||
|
.OnDelete(DeleteBehavior.Restrict);
|
||||||
|
|
||||||
builder.HasOne(f => f.Creator)
|
builder.HasOne(f => f.Creator)
|
||||||
.WithMany()
|
.WithMany()
|
||||||
.HasForeignKey(fk => fk.CreatedBy)
|
.HasForeignKey(fk => fk.CreatedBy)
|
||||||
|
|||||||
@@ -7,5 +7,7 @@ public class Portfolio : Models.Portfolio
|
|||||||
|
|
||||||
public virtual User? Owner { get; set; }
|
public virtual User? Owner { get; set; }
|
||||||
|
|
||||||
|
public virtual Organisation? Organisation { get; set; }
|
||||||
|
|
||||||
public virtual ICollection<Programme> Programmes { get; set; } = [];
|
public virtual ICollection<Programme> Programmes { get; set; } = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ public class Portfolio
|
|||||||
{
|
{
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
public long OrganisationId { get; set; }
|
||||||
|
|
||||||
public Guid OwnedBy { get; set; }
|
public Guid OwnedBy { get; set; }
|
||||||
|
|
||||||
public Guid CreatedBy { get; set; }
|
public Guid CreatedBy { get; set; }
|
||||||
|
|||||||
+1979
File diff suppressed because it is too large
Load Diff
+50
@@ -0,0 +1,50 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace PostFundManagement.Infrastructure.Database.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class AddedOrganisationToPortfolio : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<long>(
|
||||||
|
name: "OrganisationId",
|
||||||
|
table: "Portfolios",
|
||||||
|
type: "bigint",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0L);
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Portfolios_OrganisationId",
|
||||||
|
table: "Portfolios",
|
||||||
|
column: "OrganisationId");
|
||||||
|
|
||||||
|
migrationBuilder.AddForeignKey(
|
||||||
|
name: "FK_Portfolios_Organisations_OrganisationId",
|
||||||
|
table: "Portfolios",
|
||||||
|
column: "OrganisationId",
|
||||||
|
principalTable: "Organisations",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Restrict);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropForeignKey(
|
||||||
|
name: "FK_Portfolios_Organisations_OrganisationId",
|
||||||
|
table: "Portfolios");
|
||||||
|
|
||||||
|
migrationBuilder.DropIndex(
|
||||||
|
name: "IX_Portfolios_OrganisationId",
|
||||||
|
table: "Portfolios");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "OrganisationId",
|
||||||
|
table: "Portfolios");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -1006,6 +1006,9 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
|
|||||||
.HasMaxLength(256)
|
.HasMaxLength(256)
|
||||||
.HasColumnType("character varying(256)");
|
.HasColumnType("character varying(256)");
|
||||||
|
|
||||||
|
b.Property<long>("OrganisationId")
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
b.Property<Guid>("OwnedBy")
|
b.Property<Guid>("OwnedBy")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
@@ -1013,6 +1016,8 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
|
|||||||
|
|
||||||
b.HasIndex("CreatedBy");
|
b.HasIndex("CreatedBy");
|
||||||
|
|
||||||
|
b.HasIndex("OrganisationId");
|
||||||
|
|
||||||
b.HasIndex("OwnedBy");
|
b.HasIndex("OwnedBy");
|
||||||
|
|
||||||
b.ToTable("Portfolios", (string)null);
|
b.ToTable("Portfolios", (string)null);
|
||||||
@@ -1799,6 +1804,12 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
|
|||||||
.OnDelete(DeleteBehavior.NoAction)
|
.OnDelete(DeleteBehavior.NoAction)
|
||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("PostFundManagement.Domain.Entities.Organisation", "Organisation")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("OrganisationId")
|
||||||
|
.OnDelete(DeleteBehavior.Restrict)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
b.HasOne("PostFundManagement.Domain.Entities.User", "Owner")
|
b.HasOne("PostFundManagement.Domain.Entities.User", "Owner")
|
||||||
.WithMany()
|
.WithMany()
|
||||||
.HasForeignKey("OwnedBy")
|
.HasForeignKey("OwnedBy")
|
||||||
@@ -1807,6 +1818,8 @@ namespace PostFundManagement.Infrastructure.Database.Migrations
|
|||||||
|
|
||||||
b.Navigation("Creator");
|
b.Navigation("Creator");
|
||||||
|
|
||||||
|
b.Navigation("Organisation");
|
||||||
|
|
||||||
b.Navigation("Owner");
|
b.Navigation("Owner");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user