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

44 lines
1.3 KiB
C#
Raw Normal View History

2025-03-03 00:42:20 +01:00
using Insight.Database;
2025-03-03 17:40:16 +01:00
using PlanTempus.Core.Sql;
2025-03-03 00:42:20 +01:00
namespace PlanTempus.Components.Organizations.Create
{
public class CreateOrganizationHandler
{
private readonly IDatabaseOperations _databaseOperations;
public CreateOrganizationHandler(IDatabaseOperations databaseOperations)
{
_databaseOperations = databaseOperations;
}
2025-03-04 23:54:55 +01:00
public async Task<CreateOrganizationResult> Handle(CreateOrganizationCommand command)
2025-03-03 00:42:20 +01:00
{
using var db = _databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
2025-03-04 23:54:55 +01:00
2025-03-03 00:42:20 +01:00
try
{
2025-03-04 23:54:55 +01:00
var sql = @"
INSERT INTO organizations (connection_string, created_by)
VALUES (@ConnectionString, @CreatedBy)
RETURNING id, created_at";
2025-03-03 17:40:16 +01:00
2025-03-03 00:42:20 +01:00
2025-03-04 23:54:55 +01:00
var data = await db.Connection.QuerySqlAsync<CreateOrganizationResult>(sql, new
2025-03-03 00:42:20 +01:00
{
2025-03-04 23:54:55 +01:00
ConnectionString = command.ConnectionString,
CreatedBy = command.CreatedById
2025-03-03 00:42:20 +01:00
});
db.Success();
2025-03-04 23:54:55 +01:00
return data.First();
2025-03-03 00:42:20 +01:00
}
catch (Exception ex)
{
db.Error(ex);
throw;
}
}
}
}