Merge pull request 'Endpoints' (#5) from endpoints into main
Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
@@ -1,30 +0,0 @@
|
|||||||
using PostFundManagement.Application.Extensions;
|
|
||||||
using PostFundManagement.Application.Services.Communications.Events;
|
|
||||||
using PostFundManagement.Domain.Abstractions;
|
|
||||||
using PostFundManagement.Domain.Extensions;
|
|
||||||
using PostFundManagement.Domain.Security;
|
|
||||||
|
|
||||||
namespace PostFundManagement.Api.Endpoints.Communications;
|
|
||||||
|
|
||||||
[ApiVersionTarget(1)]
|
|
||||||
public class SyncIdentityUserEndpoint : IEndpoint
|
|
||||||
{
|
|
||||||
public void Map(IEndpointRouteBuilder builder)
|
|
||||||
{
|
|
||||||
builder.MapPost("api/integration/sync", async (IJobOrchestrator jobOrchestrator,
|
|
||||||
CancellationToken cancellationToken = default) =>
|
|
||||||
{
|
|
||||||
var syncEvent = new SyncUserEvent();
|
|
||||||
|
|
||||||
await jobOrchestrator.SendAsync(syncEvent, cancellationToken);
|
|
||||||
|
|
||||||
return Results.Ok();
|
|
||||||
})
|
|
||||||
.WithDescription("Trigger integration and data synchronization in the background")
|
|
||||||
.WithName(typeof(SyncIdentityUserEndpoint).ToEndpointName())
|
|
||||||
.MapToApiVersion(new ApiVersion(1))
|
|
||||||
.Produces(StatusCodes.Status200OK)
|
|
||||||
.Produces(StatusCodes.Status401Unauthorized)
|
|
||||||
.WithTags(EndpointTags.Communications);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Users;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Models;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Users;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class GetUserEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/users/{userId:Guid}", async (Guid userId, UserService service,
|
||||||
|
HttpContext context, CancellationToken cancellationToken = default) =>
|
||||||
|
{
|
||||||
|
var result = await service.GetUserAsync(userId, cancellationToken);
|
||||||
|
|
||||||
|
return result.IsSuccess
|
||||||
|
? Results.Ok(result.Value)
|
||||||
|
: Results.NotFound();
|
||||||
|
})
|
||||||
|
.WithDescription("Gets the metadata of a single user.")
|
||||||
|
.WithName(typeof(GetUserEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces<User>(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.WithTags(EndpointTags.Users);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using PostFundManagement.Application.Extensions;
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Communications.Events;
|
||||||
using PostFundManagement.Application.Services.Users;
|
using PostFundManagement.Application.Services.Users;
|
||||||
using PostFundManagement.Domain.Abstractions;
|
using PostFundManagement.Domain.Abstractions;
|
||||||
using PostFundManagement.Domain.Extensions;
|
using PostFundManagement.Domain.Extensions;
|
||||||
@@ -11,20 +12,22 @@ public class SyncUserEndpoint : IEndpoint
|
|||||||
{
|
{
|
||||||
public void Map(IEndpointRouteBuilder builder)
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
{
|
{
|
||||||
builder.MapPost("api/users/sync", async (HttpContext context,
|
builder.MapPost("api/integration/sync", async (IJobOrchestrator jobOrchestrator,
|
||||||
UserService service, CancellationToken cancellationToken = default) =>
|
HttpContext context, UserService service, CancellationToken cancellationToken = default) =>
|
||||||
{
|
{
|
||||||
var result = await service.SyncUserAsync(context.GetUserId(), context.GetUserEmail(), cancellationToken);
|
var result = await service.SyncUserAsync(context.GetUserId(), context.GetUserEmail(), cancellationToken);
|
||||||
|
|
||||||
|
if (result.IsSuccess)
|
||||||
|
await jobOrchestrator.SendAsync(SyncUserEvent.Create(), cancellationToken);
|
||||||
|
|
||||||
return result.IsSuccess
|
return result.IsSuccess
|
||||||
? Results.Ok()
|
? Results.Ok()
|
||||||
: Results.BadRequest(result.Errors.ToProblems());
|
: Results.BadRequest(result.Errors.ToProblems());
|
||||||
})
|
})
|
||||||
.WithDescription("Synchronise caller user profile using id_token's SID, inserting/updating user details in database")
|
.WithDescription("Trigger integration and data synchronization in the background")
|
||||||
.WithName(typeof(SyncUserEndpoint).ToEndpointName())
|
.WithName(typeof(SyncUserEndpoint).ToEndpointName())
|
||||||
.MapToApiVersion(new ApiVersion(1))
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
.Produces(StatusCodes.Status200OK)
|
.Produces(StatusCodes.Status200OK)
|
||||||
.Produces<Microsoft.AspNetCore.Mvc.ProblemDetails>(StatusCodes.Status400BadRequest)
|
|
||||||
.Produces(StatusCodes.Status401Unauthorized)
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
.WithTags(EndpointTags.Users);
|
.WithTags(EndpointTags.Users);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using PostFundManagement.Application.Extensions;
|
||||||
|
using PostFundManagement.Application.Services.Users;
|
||||||
|
using PostFundManagement.Domain;
|
||||||
|
using PostFundManagement.Domain.Abstractions;
|
||||||
|
using PostFundManagement.Domain.Extensions;
|
||||||
|
using PostFundManagement.Domain.Security;
|
||||||
|
|
||||||
|
namespace PostFundManagement.Api.Endpoints.Users;
|
||||||
|
|
||||||
|
[ApiVersionTarget(1)]
|
||||||
|
public sealed class UpdateUserStatusEndpoint : IEndpoint
|
||||||
|
{
|
||||||
|
public void Map(IEndpointRouteBuilder builder)
|
||||||
|
{
|
||||||
|
builder.MapGet("api/users/{userId:Guid}/status/{status:int}", async (Guid userId, ActivityStatus status,
|
||||||
|
UserService service, CancellationToken cancellationToken = default) =>
|
||||||
|
{
|
||||||
|
var result = await service.UpdateUserStatusAsync(userId, status, cancellationToken);
|
||||||
|
|
||||||
|
return result.IsSuccess
|
||||||
|
? Results.Ok()
|
||||||
|
: Results.NotFound();
|
||||||
|
})
|
||||||
|
.WithDescription("Changes the Activity status of the user in the system")
|
||||||
|
.WithName(typeof(UpdateUserStatusEndpoint).ToEndpointName())
|
||||||
|
.MapToApiVersion(new ApiVersion(1))
|
||||||
|
.Produces(StatusCodes.Status200OK)
|
||||||
|
.Produces(StatusCodes.Status404NotFound)
|
||||||
|
.Produces(StatusCodes.Status401Unauthorized)
|
||||||
|
.WithTags(EndpointTags.Users);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,8 @@ namespace PostFundManagement.Application.Services.Communications.Events;
|
|||||||
public class SyncUserEvent : EventBase, IEvent
|
public class SyncUserEvent : EventBase, IEvent
|
||||||
{
|
{
|
||||||
public string Name { get; set; } = nameof(SyncUserEvent);
|
public string Name { get; set; } = nameof(SyncUserEvent);
|
||||||
|
|
||||||
|
public static SyncUserEvent Create() => new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SyncUserEventHandler(ILogger<SyncUserEvent> logger) : INotificationHandler<SyncUserEvent>
|
public class SyncUserEventHandler(ILogger<SyncUserEvent> logger) : INotificationHandler<SyncUserEvent>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace PostFundManagement.Application.Services.Users;
|
|||||||
|
|
||||||
public sealed class UserService(IDbContextFactory<ApplicationDbContext> contextFactory) : IService
|
public sealed class UserService(IDbContextFactory<ApplicationDbContext> contextFactory) : IService
|
||||||
{
|
{
|
||||||
public async ValueTask<Result> SyncUserAsync(Guid userId, string email, CancellationToken cancellationToken = default)
|
public async ValueTask<Result> SyncUserAsync(Guid userId, string email, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -44,8 +44,8 @@ public sealed class UserService(IDbContextFactory<ApplicationDbContext> contextF
|
|||||||
|
|
||||||
public async ValueTask<Result<User[]>> GetUsersAsync(Pagination pagination, UserFilterRequest request, CancellationToken cancellationToken = default)
|
public async ValueTask<Result<User[]>> GetUsersAsync(Pagination pagination, UserFilterRequest request, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
var query = context.Users.AsNoTracking();
|
var query = context.Users.AsNoTracking();
|
||||||
@@ -74,9 +74,47 @@ public sealed class UserService(IDbContextFactory<ApplicationDbContext> contextF
|
|||||||
|
|
||||||
return Result.Ok(users?.Select(u => u.Map()).ToArray() ?? []);
|
return Result.Ok(users?.Select(u => u.Map()).ToArray() ?? []);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<Result> UpdateUserStatusAsync(Guid userId, ActivityStatus status, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var changedTotal = await context.Users
|
||||||
|
.Where(f => f.Id == userId)
|
||||||
|
.ExecuteUpdateAsync(f => f.SetProperty(u => u.Status, status), cancellationToken);
|
||||||
|
|
||||||
|
return changedTotal > 0
|
||||||
|
? Result.Ok()
|
||||||
|
: Result.Fail("Failed to update user activity status.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<User>> GetUserAsync(Guid userId, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
|
||||||
|
var user = await context.Users.AsNoTracking().FirstOrDefaultAsync(f => f.Id == userId, cancellationToken);
|
||||||
|
|
||||||
|
return user is not null
|
||||||
|
? Result.Ok(user.Map())
|
||||||
|
: Result.Fail("Failed to find specific user.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user