Adds Generic CommandHandlerDecorator
This commit is contained in:
parent
49f9b99ee1
commit
64e696dc5a
21 changed files with 131 additions and 110 deletions
|
|
@ -1,27 +1,30 @@
|
|||
using System.Diagnostics;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.DataContracts;
|
||||
using PlanTempus.Core.CommandQueries;
|
||||
|
||||
namespace PlanTempus.Components.Users.Create;
|
||||
|
||||
public class CreateUserHandlerDecorator(
|
||||
ICommandHandler<CreateUserCommand, CreateUserResult> decoratedHandler,
|
||||
TelemetryClient telemetryClient)
|
||||
: ICommandHandler<CreateUserCommand, CreateUserResult>, ICommandHandlerDecorator
|
||||
public class CommandHandlerDecorator<TCommand>(
|
||||
ICommandHandler<TCommand> decoratedHandler,
|
||||
TelemetryClient telemetryClient) : ICommandHandler<TCommand>, ICommandHandlerDecorator where TCommand : ICommand
|
||||
{
|
||||
public async Task<CreateUserResult> Handle(CreateUserCommand command)
|
||||
public async Task<CommandResponse> Handle(TCommand command)
|
||||
{
|
||||
// var correlationId = Activity.Current?.RootId ?? command.CorrelationId;
|
||||
|
||||
using (var operation =
|
||||
telemetryClient.StartOperation<RequestTelemetry>($"Handle {nameof(CreateUserCommand)}",
|
||||
telemetryClient.StartOperation<RequestTelemetry>($"Handle {decoratedHandler.GetType().FullName}",
|
||||
command.CorrelationId.ToString()))
|
||||
{
|
||||
try
|
||||
{
|
||||
operation.Telemetry.Properties["CorrelationId"] = command.CorrelationId.ToString();
|
||||
operation.Telemetry.Properties["TransactionId"] = command.TransactionId.ToString();
|
||||
|
||||
var result = await decoratedHandler.Handle(command);
|
||||
|
||||
operation.Telemetry.Properties["RequestId"] = result.RequestId.ToString();
|
||||
operation.Telemetry.Success = true;
|
||||
|
||||
return result;
|
||||
|
|
@ -32,7 +35,9 @@ public class CreateUserHandlerDecorator(
|
|||
|
||||
telemetryClient.TrackException(ex, new Dictionary<string, string>
|
||||
{
|
||||
["CommandType"] = nameof(CreateUserCommand)
|
||||
["CorrelationId"] = command.CorrelationId.ToString(),
|
||||
["Command"] = command.GetType().Name,
|
||||
["CommandHandler"] = decoratedHandler.GetType().FullName
|
||||
});
|
||||
throw;
|
||||
}
|
||||
|
|
@ -1,15 +1,11 @@
|
|||
using PlanTempus.Core.CommandQueries;
|
||||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public interface ICommand
|
||||
{
|
||||
Guid CorrelationId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateUserCommand : ICommand
|
||||
public class CreateUserCommand : Command
|
||||
{
|
||||
public required string Email { get; set; }
|
||||
public required string Password { get; set; }
|
||||
public bool IsActive { get; set; } = true;
|
||||
public required Guid CorrelationId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +1,31 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using PlanTempus.Core.CommandQueries;
|
||||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/users")]
|
||||
public class CreateUserController(CreateUserHandler handler, CreateUserValidator validator) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CreateUserResult>> Create([FromBody] CreateUserCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
var validationResult = await validator.ValidateAsync(command);
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.Errors);
|
||||
}
|
||||
[ApiController]
|
||||
[Route("api/users")]
|
||||
public class CreateUserController(CreateUserHandler handler, CreateUserValidator validator) : ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CommandResponse>> Create([FromBody] CreateUserCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
var validationResult = await validator.ValidateAsync(command);
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
return BadRequest(validationResult.Errors);
|
||||
}
|
||||
|
||||
var result = await handler.Handle(command);
|
||||
|
||||
return CreatedAtAction(
|
||||
nameof(CreateUserCommand),
|
||||
"GetUser",
|
||||
new { id = result.Id },
|
||||
result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
var result = await handler.Handle(command);
|
||||
|
||||
return Accepted($"/api/requests/{result.RequestId}", result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,17 +3,16 @@ using Microsoft.ApplicationInsights;
|
|||
using Npgsql;
|
||||
using PlanTempus.Components.Users.Exceptions;
|
||||
using PlanTempus.Core;
|
||||
using PlanTempus.Core.CommandQueries;
|
||||
using PlanTempus.Core.Database;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public class CreateUserHandler(
|
||||
TelemetryClient telemetryClient,
|
||||
IDatabaseOperations databaseOperations,
|
||||
ISecureTokenizer secureTokenizer) : ICommandHandler<CreateUserCommand, CreateUserResult>
|
||||
ISecureTokenizer secureTokenizer) : ICommandHandler<CreateUserCommand>
|
||||
{
|
||||
public async Task<CreateUserResult> Handle(CreateUserCommand command)
|
||||
public async Task<CommandResponse> Handle(CreateUserCommand command)
|
||||
{
|
||||
using var db = databaseOperations.CreateScope(nameof(CreateUserHandler));
|
||||
try
|
||||
|
|
@ -28,20 +27,19 @@ namespace PlanTempus.Components.Users.Create
|
|||
|
||||
var data = await db.Connection.QuerySqlAsync<CreateUserResult>(sql, new
|
||||
{
|
||||
Email = command.Email,
|
||||
command.Email,
|
||||
PasswordHash = secureTokenizer.TokenizeText(command.Password),
|
||||
SecurityStamp = Guid.NewGuid().ToString("N"),
|
||||
EmailConfirmed = false,
|
||||
AccessFailedCount = 0,
|
||||
LockoutEnabled = false,
|
||||
IsActive = command.IsActive,
|
||||
command.IsActive,
|
||||
});
|
||||
|
||||
|
||||
var result = data.First();
|
||||
|
||||
|
||||
return result;
|
||||
return new CommandResponse(command.CorrelationId);
|
||||
}
|
||||
catch (PostgresException ex) when (ex.SqlState == "23505")
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue