Working on user+org create
This commit is contained in:
parent
a15cc00595
commit
0010a32248
6 changed files with 49 additions and 21 deletions
|
|
@ -2,8 +2,7 @@
|
|||
{
|
||||
public class CreateOrganizationCommand
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public Guid CreatedById { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,41 @@
|
|||
using Insight.Database;
|
||||
using Microsoft.ApplicationInsights;
|
||||
using PlanTempus.Core.Sql;
|
||||
using PlanTempus.Core.Telemetry;
|
||||
|
||||
namespace PlanTempus.Components.Organizations.Create
|
||||
{
|
||||
public class CreateOrganizationHandler(IDatabaseOperations databaseOperations)
|
||||
public class CreateOrganizationHandler(TelemetryClient telemetryClient, IDatabaseOperations databaseOperations)
|
||||
{
|
||||
public async Task<CreateOrganizationResult> Handle(CreateOrganizationCommand command)
|
||||
{
|
||||
using var db = databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
|
||||
|
||||
using var transaction = db.Connection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var sql = @"INSERT INTO organizations (connection_string, created_by)
|
||||
VALUES (@ConnectionString, @CreatedBy)
|
||||
RETURNING id, created_at";
|
||||
var createOrg = @"
|
||||
INSERT INTO organizations (connection_string)
|
||||
VALUES (@ConnectionString)
|
||||
RETURNING id, created_at";
|
||||
|
||||
|
||||
var data = await db.Connection.QuerySqlAsync<CreateOrganizationResult>(sql, new
|
||||
var data = await db.Connection.QuerySqlAsync<CreateOrganizationResult>(createOrg, new
|
||||
{
|
||||
ConnectionString = command.ConnectionString,
|
||||
CreatedBy = command.CreatedById
|
||||
command.ConnectionString
|
||||
});
|
||||
|
||||
var orgResult = data.First();
|
||||
|
||||
await db.Connection.ExecuteAsync(@$"
|
||||
INSERT INTO user_organizations (user_id, organization_id)
|
||||
VALUES (@UserId, @OrganizationId)",
|
||||
new { command.UserId, OrganizationId = orgResult.Id });
|
||||
|
||||
transaction.Commit();
|
||||
|
||||
db.Success();
|
||||
|
||||
return data.First();
|
||||
telemetryClient.TrackTrace(GetType().Name, orgResult.Format());
|
||||
return orgResult;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue