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

36 lines
1.1 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
{
2025-03-05 16:56:58 +01:00
public class CreateOrganizationHandler(IDatabaseOperations databaseOperations)
2025-03-03 00:42:20 +01:00
{
2025-03-04 23:54:55 +01:00
public async Task<CreateOrganizationResult> Handle(CreateOrganizationCommand command)
2025-03-03 00:42:20 +01:00
{
2025-03-05 16:56:58 +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-05 16:56:58 +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;
}
}
}
}