PlanTempusApp/PlanTempus.Components/Organizations/Create/CreateOrganizationHandler.cs

52 lines
1.6 KiB
C#
Raw Normal View History

2025-03-03 00:42:20 +01:00
using Insight.Database;
namespace PlanTempus.Components.Organizations.Create
{
public class CreateOrganizationHandler
{
private readonly IDatabaseOperations _databaseOperations;
public CreateOrganizationHandler(IDatabaseOperations databaseOperations)
{
_databaseOperations = databaseOperations;
}
public async Task<CreateOrganizationResponse> Handle(CreateOrganizationCommand command)
{
using var db = _databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
try
{
var organizationId = Guid.NewGuid();
var now = DateTime.UtcNow;
var sql = @"
INSERT INTO Organizations (Id, Name, Description, CreatedById, CreatedAt, UpdatedAt)
VALUES (@Id, @Name, @Description, @CreatedById, @CreatedAt, @UpdatedAt)";
await db.Connection.ExecuteSqlAsync(sql, new
{
Id = organizationId,
command.Name,
command.Description,
CreatedById = command.CreatedById,
CreatedAt = now,
UpdatedAt = now
});
db.Success();
return new CreateOrganizationResponse
{
Id = organizationId,
Name = command.Name,
CreatedAt = now
};
}
catch (Exception ex)
{
db.Error(ex);
throw;
}
}
}
}