Cleaning up with Rider
This commit is contained in:
parent
69758735de
commit
91da89a4e8
22 changed files with 574 additions and 386 deletions
137
Application/Components/ApiViewComponentBase.cs
Normal file
137
Application/Components/ApiViewComponentBase.cs
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
using Microsoft.ApplicationInsights;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace PlanTempus.Application.Components
|
||||||
|
{
|
||||||
|
public abstract class ApiViewComponentBase : ViewComponent
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly TelemetryClient _telemetry;
|
||||||
|
|
||||||
|
protected ApiViewComponentBase(IHttpClientFactory httpClientFactory, TelemetryClient telemetry)
|
||||||
|
{
|
||||||
|
_httpClient = httpClientFactory.CreateClient("ApiClient");
|
||||||
|
_telemetry = telemetry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches data from API as JObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="apiEndpoint">API endpoint path (e.g. "/api/product/get/1")</param>
|
||||||
|
/// <returns>JObject with API result</returns>
|
||||||
|
protected async Task<JObject> GetJObjectFromApiAsync(string apiEndpoint)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_telemetry.TrackEvent("ApiCall", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint,
|
||||||
|
["Source"] = "GetJObjectFromApiAsync"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Use HttpClient to get JSON string
|
||||||
|
var response = await _httpClient.GetAsync(apiEndpoint);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var jsonString = await response.Content.ReadAsStringAsync();
|
||||||
|
return JObject.Parse(jsonString);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint,
|
||||||
|
["Method"] = "GetJObjectFromApiAsync"
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches data from API as JArray
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="apiEndpoint">API endpoint path</param>
|
||||||
|
/// <returns>JArray with API result</returns>
|
||||||
|
protected async Task<JArray> GetJArrayFromApiAsync(string apiEndpoint)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_telemetry.TrackEvent("ApiCall", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint,
|
||||||
|
["Source"] = "GetJArrayFromApiAsync"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Use HttpClient to get JSON string
|
||||||
|
var response = await _httpClient.GetAsync(apiEndpoint);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var jsonString = await response.Content.ReadAsStringAsync();
|
||||||
|
return JArray.Parse(jsonString);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint,
|
||||||
|
["Method"] = "GetJArrayFromApiAsync"
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends POST request to API and receives JObject response
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="apiEndpoint">API endpoint path</param>
|
||||||
|
/// <param name="data">Data to be sent (can be JObject or other type)</param>
|
||||||
|
/// <returns>JObject with response data</returns>
|
||||||
|
protected async Task<JObject> PostToApiAsync(string apiEndpoint, object data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_telemetry.TrackEvent("ApiPost", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint
|
||||||
|
});
|
||||||
|
|
||||||
|
// Convert data to JSON string
|
||||||
|
var content = new StringContent(
|
||||||
|
JsonConvert.SerializeObject(data),
|
||||||
|
System.Text.Encoding.UTF8,
|
||||||
|
"application/json");
|
||||||
|
|
||||||
|
var response = await _httpClient.PostAsync(apiEndpoint, content);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var jsonString = await response.Content.ReadAsStringAsync();
|
||||||
|
return JObject.Parse(jsonString);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_telemetry.TrackException(ex, new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Endpoint"] = apiEndpoint,
|
||||||
|
["Method"] = "PostToApiAsync"
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles errors in a consistent way
|
||||||
|
/// </summary>
|
||||||
|
protected IViewComponentResult HandleError(string message = "An error occurred.")
|
||||||
|
{
|
||||||
|
_telemetry.TrackEvent("ComponentError", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["Message"] = message,
|
||||||
|
["Component"] = this.GetType().Name
|
||||||
|
});
|
||||||
|
|
||||||
|
return Content(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
43
Application/Components/OrganizationViewComponent.cs
Normal file
43
Application/Components/OrganizationViewComponent.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.ApplicationInsights;
|
||||||
|
|
||||||
|
namespace PlanTempus.Application.Components
|
||||||
|
{
|
||||||
|
public class OrganizationViewComponent : ApiViewComponentBase
|
||||||
|
{
|
||||||
|
private readonly TelemetryClient _telemetry;
|
||||||
|
|
||||||
|
public OrganizationViewComponent(
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
|
TelemetryClient telemetry)
|
||||||
|
: base(httpClientFactory, telemetry)
|
||||||
|
{
|
||||||
|
_telemetry = telemetry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IViewComponentResult> InvokeAsync(int organizationId, bool showDetailedView = true)
|
||||||
|
{
|
||||||
|
_telemetry.TrackEvent($"{GetType().Name}Invoked", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["OrganizationId"] = organizationId.ToString(),
|
||||||
|
["ShowDetailedView"] = showDetailedView.ToString()
|
||||||
|
});
|
||||||
|
|
||||||
|
var organization = await GetJObjectFromApiAsync($"/api/organization/get/{organizationId}");
|
||||||
|
|
||||||
|
if (organization == null)
|
||||||
|
return HandleError("Organization not found");
|
||||||
|
|
||||||
|
ViewBag.ShowDetailedView = showDetailedView;
|
||||||
|
|
||||||
|
|
||||||
|
_telemetry.TrackEvent("Viewed", new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["OrganizationId"] = organizationId.ToString(),
|
||||||
|
["Name"] = organization["name"]?.ToString()
|
||||||
|
});
|
||||||
|
|
||||||
|
return View(organization);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -106,23 +106,32 @@ namespace PlanTempus.Core.Logging
|
||||||
|
|
||||||
public async Task LogAsync(RequestTelemetry req, CancellationToken cancellationToken = default)
|
public async Task LogAsync(RequestTelemetry req, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
public async Task LogAsync(Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operationHolder, CancellationToken cancellationToken = default)
|
|
||||||
|
public async Task LogAsync(
|
||||||
|
Microsoft.ApplicationInsights.Extensibility.IOperationHolder<RequestTelemetry> operationHolder,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var req = operationHolder.Telemetry;
|
var req = operationHolder.Telemetry;
|
||||||
|
|
||||||
//https://docs.datalust.co/v2025.1/docs/posting-raw-events
|
//https://docs.datalust.co/v2025.1/docs/posting-raw-events
|
||||||
var seqEvent = new Dictionary<string, object>
|
var seqEvent = new Dictionary<string, object>
|
||||||
{
|
{
|
||||||
|
|
||||||
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
{ "@t", req.Timestamp.UtcDateTime.ToString("o") },
|
||||||
{ "@mt", req.Name },
|
{ "@mt", req.Name },
|
||||||
{ "@l", req.Success ?? true ? "Information" : "Error" },
|
{ "@l", req.Success ?? true ? "Information" : "Error" },
|
||||||
{ "@sp", req.Id }, //Span id Unique identifier of a span Yes, if the event is a span
|
{ "@sp", req.Id }, //Span id Unique identifier of a span Yes, if the event is a span
|
||||||
{ "@tr", req.Context.Operation.Id}, //Trace id An identifier that groups all spans and logs that are in the same trace Yes, if the event is a span
|
{
|
||||||
{ "@sk","Server" }, //Span kind Describes the relationship of the span to others in the trace: Client, Server, Internal, Producer, or Consumer
|
"@tr", req.Context.Operation.Id
|
||||||
{ "@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o") }, //Start The start ISO 8601 timestamp of this span Yes, if the event is a span
|
}, //Trace id An identifier that groups all spans and logs that are in the same trace Yes, if the event is a span
|
||||||
|
{
|
||||||
|
"@sk", "Server"
|
||||||
|
}, //Span kind Describes the relationship of the span to others in the trace: Client, Server, Internal, Producer, or Consumer
|
||||||
|
{
|
||||||
|
"@st", req.Timestamp.UtcDateTime.Subtract(req.Duration).ToString("o")
|
||||||
|
}, //Start The start ISO 8601 timestamp of this span Yes, if the event is a span
|
||||||
{ "SourceContext", typeof(T).FullName },
|
{ "SourceContext", typeof(T).FullName },
|
||||||
{ "Url", req.Url },
|
{ "Url", req.Url },
|
||||||
{ "RequestId", req.Id },
|
{ "RequestId", req.Id },
|
||||||
|
|
@ -140,6 +149,7 @@ namespace PlanTempus.Core.Logging
|
||||||
seqEvent["StatusCode"] = $"{statusCode} Unknown";
|
seqEvent["StatusCode"] = $"{statusCode} Unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(req.Context.Operation.ParentId))
|
if (!string.IsNullOrEmpty(req.Context.Operation.ParentId))
|
||||||
seqEvent["@ps"] = req.Context.Operation.ParentId;
|
seqEvent["@ps"] = req.Context.Operation.ParentId;
|
||||||
|
|
||||||
|
|
@ -158,6 +168,7 @@ namespace PlanTempus.Core.Logging
|
||||||
|
|
||||||
await SendToSeqAsync(seqEvent, cancellationToken);
|
await SendToSeqAsync(seqEvent, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
private async Task SendToSeqAsync(Dictionary<string, object> seqEvent, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var content = new StringContent(
|
var content = new StringContent(
|
||||||
|
|
@ -187,6 +198,7 @@ namespace PlanTempus.Core.Logging
|
||||||
_ => "Information"
|
_ => "Information"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string FormatExceptionForSeq(Exception ex)
|
private static string FormatExceptionForSeq(Exception ex)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
{
|
{
|
||||||
public async Task RotateMasterKey(int tenantId, string oldMasterKey, string newMasterKey)
|
public async Task RotateMasterKey(int tenantId, string oldMasterKey, string newMasterKey)
|
||||||
{
|
{
|
||||||
|
await Task.CompletedTask;
|
||||||
// Hent alle bruger-keys for tenant
|
// Hent alle bruger-keys for tenant
|
||||||
//var users = await GetTenantUsers(tenantId);
|
//var users = await GetTenantUsers(tenantId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
namespace PlanTempus.Core
|
namespace PlanTempus.Core
|
||||||
{
|
{
|
||||||
|
|
||||||
public class SecureTokenizer : ISecureTokenizer
|
public class SecureTokenizer : ISecureTokenizer
|
||||||
{
|
{
|
||||||
private const int _saltSize = 16; // 128 bit
|
private const int _saltSize = 16; // 128 bit
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
namespace PlanTempus.Core.Sql.ConnectionFactory
|
namespace PlanTempus.Core.Sql.ConnectionFactory
|
||||||
{
|
{
|
||||||
public record ConnectionStringParameters(string user, string pwd);
|
|
||||||
|
|
||||||
public interface IDbConnectionFactory
|
public interface IDbConnectionFactory
|
||||||
{
|
{
|
||||||
System.Data.IDbConnection Create();
|
System.Data.IDbConnection Create();
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Data;
|
||||||
namespace PlanTempus.Core.Sql.ConnectionFactory
|
namespace PlanTempus.Core.Sql.ConnectionFactory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public record ConnectionStringParameters(string User, string Pwd);
|
||||||
|
|
||||||
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
|
||||||
{
|
{
|
||||||
|
|
@ -34,8 +35,8 @@ namespace PlanTempus.Core.Sql.ConnectionFactory
|
||||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
|
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
|
||||||
_baseDataSource.ConnectionString)
|
_baseDataSource.ConnectionString)
|
||||||
{
|
{
|
||||||
Username = param.user,
|
Username = param.User,
|
||||||
Password = param.pwd
|
Password = param.Pwd
|
||||||
};
|
};
|
||||||
|
|
||||||
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
|
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
|
||||||
|
|
|
||||||
35
Core/Sql/DatabaseScope.cs
Normal file
35
Core/Sql/DatabaseScope.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
using System.Data;
|
||||||
|
using Microsoft.ApplicationInsights.DataContracts;
|
||||||
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
|
|
||||||
|
namespace PlanTempus.Core.Sql;
|
||||||
|
|
||||||
|
public class DatabaseScope : IDisposable
|
||||||
|
{
|
||||||
|
private readonly IOperationHolder<DependencyTelemetry> _operation;
|
||||||
|
|
||||||
|
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
|
||||||
|
{
|
||||||
|
Connection = connection;
|
||||||
|
_operation = operation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDbConnection Connection { get; }
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_operation.Dispose();
|
||||||
|
Connection.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Success()
|
||||||
|
{
|
||||||
|
_operation.Telemetry.Success = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Error(Exception ex)
|
||||||
|
{
|
||||||
|
_operation.Telemetry.Success = false;
|
||||||
|
_operation.Telemetry.Properties["Error"] = ex.Message;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Core/Sql/IDatabaseOperations.cs
Normal file
10
Core/Sql/IDatabaseOperations.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace PlanTempus.Core.Sql;
|
||||||
|
|
||||||
|
public interface IDatabaseOperations
|
||||||
|
{
|
||||||
|
DatabaseScope CreateScope(string operationName);
|
||||||
|
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
|
||||||
|
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
|
||||||
|
}
|
||||||
|
|
@ -1,48 +1,9 @@
|
||||||
using Microsoft.ApplicationInsights;
|
using System.Data;
|
||||||
|
using Microsoft.ApplicationInsights;
|
||||||
using Microsoft.ApplicationInsights.DataContracts;
|
using Microsoft.ApplicationInsights.DataContracts;
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
|
||||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
namespace PlanTempus.Core.Sql
|
namespace PlanTempus.Core.Sql;
|
||||||
{
|
|
||||||
public class DatabaseScope : IDisposable
|
|
||||||
{
|
|
||||||
private readonly IDbConnection _connection;
|
|
||||||
private readonly IOperationHolder<DependencyTelemetry> _operation;
|
|
||||||
|
|
||||||
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
|
|
||||||
{
|
|
||||||
_connection = connection;
|
|
||||||
_operation = operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDbConnection Connection => _connection;
|
|
||||||
|
|
||||||
public void Success()
|
|
||||||
{
|
|
||||||
_operation.Telemetry.Success = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Error(Exception ex)
|
|
||||||
{
|
|
||||||
_operation.Telemetry.Success = false;
|
|
||||||
_operation.Telemetry.Properties["Error"] = ex.Message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_operation.Dispose();
|
|
||||||
_connection.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IDatabaseOperations
|
|
||||||
{
|
|
||||||
DatabaseScope CreateScope(string operationName);
|
|
||||||
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
|
|
||||||
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SqlOperations : IDatabaseOperations
|
public class SqlOperations : IDatabaseOperations
|
||||||
{
|
{
|
||||||
|
|
@ -95,6 +56,4 @@ namespace PlanTempus.Core.Sql
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -19,6 +19,7 @@ public class SetupConfiguration : IDbConfigure<SetupConfiguration.Command>
|
||||||
{
|
{
|
||||||
using var conn = parameters is null ? _connectionFactory.Create() : _connectionFactory.Create(parameters);
|
using var conn = parameters is null ? _connectionFactory.Create() : _connectionFactory.Create(parameters);
|
||||||
using var transaction = conn.OpenWithTransaction();
|
using var transaction = conn.OpenWithTransaction();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CreateConfigurationTable(conn);
|
CreateConfigurationTable(conn);
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
using Insight.Database;
|
using Insight.Database;
|
||||||
using PlanTempus.Core.Sql.ConnectionFactory;
|
using PlanTempus.Core.Sql.ConnectionFactory;
|
||||||
using PlanTempus.Database.Common;
|
using PlanTempus.Database.Common;
|
||||||
using PlanTempus.Database.Core;
|
|
||||||
|
|
||||||
namespace PlanTempus.Database.Core.DCL
|
namespace PlanTempus.Database.Core.DCL
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
public class CreateOrganizationCommand
|
public class CreateOrganizationCommand
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public string ConnectionString { get; set; }
|
||||||
public Guid CreatedById { get; set; }
|
public Guid CreatedById { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,37 +12,27 @@ namespace PlanTempus.Components.Organizations.Create
|
||||||
_databaseOperations = databaseOperations;
|
_databaseOperations = databaseOperations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CreateOrganizationResponse> Handle(CreateOrganizationCommand command)
|
public async Task<CreateOrganizationResult> Handle(CreateOrganizationCommand command)
|
||||||
{
|
{
|
||||||
using var db = _databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
|
using var db = _databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var organizationId = Guid.NewGuid();
|
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
|
|
||||||
var sql = @"
|
var sql = @"
|
||||||
INSERT INTO organizations (id, name, description, created_by_id)
|
INSERT INTO organizations (connection_string, created_by)
|
||||||
VALUES (@Id, @Name, @Description, @CreatedById)";
|
VALUES (@ConnectionString, @CreatedBy)
|
||||||
|
RETURNING id, created_at";
|
||||||
|
|
||||||
|
|
||||||
await db.Connection.ExecuteSqlAsync(sql, new
|
var data = await db.Connection.QuerySqlAsync<CreateOrganizationResult>(sql, new
|
||||||
{
|
{
|
||||||
Id = organizationId,
|
ConnectionString = command.ConnectionString,
|
||||||
command.Name,
|
CreatedBy = command.CreatedById
|
||||||
command.Description,
|
|
||||||
CreatedById = command.CreatedById,
|
|
||||||
CreatedAt = now,
|
|
||||||
UpdatedAt = now
|
|
||||||
});
|
});
|
||||||
|
|
||||||
db.Success();
|
db.Success();
|
||||||
|
|
||||||
return new CreateOrganizationResponse
|
return data.First();
|
||||||
{
|
|
||||||
Id = organizationId,
|
|
||||||
Name = command.Name,
|
|
||||||
CreatedAt = now
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace PlanTempus.Components.Organizations.Create
|
|
||||||
{
|
|
||||||
public class CreateOrganizationResponse
|
|
||||||
{
|
|
||||||
public Guid Id { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
public DateTime CreatedAt { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace PlanTempus.Components.Organizations.Create
|
||||||
|
{
|
||||||
|
public class CreateOrganizationResult
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,7 @@ namespace PlanTempus.Components.Users.Create
|
||||||
public class CreateUserController(CreateUserHandler handler, CreateUserValidator validator) : ControllerBase
|
public class CreateUserController(CreateUserHandler handler, CreateUserValidator validator) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<CreateUserResponse>> Create([FromBody] CreateUserCommand command)
|
public async Task<ActionResult<CreateUserResult>> Create([FromBody] CreateUserCommand command)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,47 +6,34 @@ namespace PlanTempus.Components.Users.Create
|
||||||
{
|
{
|
||||||
public class CreateUserHandler(IDatabaseOperations databaseOperations, ISecureTokenizer secureTokenizer)
|
public class CreateUserHandler(IDatabaseOperations databaseOperations, ISecureTokenizer secureTokenizer)
|
||||||
{
|
{
|
||||||
private readonly ISecureTokenizer _secureTokenizer;
|
public async Task<CreateUserResult> Handle(CreateUserCommand command)
|
||||||
|
|
||||||
public async Task<CreateUserResponse> Handle(CreateUserCommand command)
|
|
||||||
{
|
{
|
||||||
using var db = databaseOperations.CreateScope(nameof(CreateUserHandler));
|
using var db = databaseOperations.CreateScope(nameof(CreateUserHandler));
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var sql = @"
|
var sql = @"
|
||||||
INSERT INTO system.users(email, password_hash, security_stamp, email_confirmed,
|
INSERT INTO system.users(email, password_hash, security_stamp, email_confirmed,
|
||||||
access_failed_count, lockout_enabled, lockout_end,
|
access_failed_count, lockout_enabled,
|
||||||
is_active, created_at, last_login_at)
|
is_active)
|
||||||
VALUES(@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed,
|
VALUES(@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed,
|
||||||
@AccessFailedCount, @LockoutEnabled, @LockoutEnd,
|
@AccessFailedCount, @LockoutEnabled, @IsActive)
|
||||||
@IsActive, @CreatedAt, @LastLoginAt)
|
RETURNING id, created_at, email, is_active";
|
||||||
RETURNING id, created_at";
|
|
||||||
|
|
||||||
var result = await db.Connection.QuerySqlAsync<UserCreationResult>(sql, new
|
var data = await db.Connection.QuerySqlAsync<CreateUserResult>(sql, new
|
||||||
{
|
{
|
||||||
Email = command.Email,
|
Email = command.Email,
|
||||||
PasswordHash = secureTokenizer.TokenizeText(command.Password),
|
PasswordHash = secureTokenizer.TokenizeText(command.Password),
|
||||||
SecurityStamp = Guid.NewGuid().ToString("N"),
|
SecurityStamp = Guid.NewGuid().ToString("N"),
|
||||||
EmailConfirmed = false,
|
EmailConfirmed = false,
|
||||||
AccessFailedCount = 0,
|
AccessFailedCount = 0,
|
||||||
LockoutEnabled = true,
|
LockoutEnabled = false,
|
||||||
LockoutEnd = (DateTime?)null,
|
|
||||||
IsActive = command.IsActive,
|
IsActive = command.IsActive,
|
||||||
CreatedAt = DateTime.UtcNow,
|
|
||||||
LastLoginAt = (DateTime?)null
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var createdUser = result.First();
|
|
||||||
|
|
||||||
db.Success();
|
db.Success();
|
||||||
|
|
||||||
return new CreateUserResponse
|
return data.First();
|
||||||
{
|
|
||||||
Id = createdUser.Id,
|
|
||||||
Email = command.Email,
|
|
||||||
IsActive = command.IsActive,
|
|
||||||
CreatedAt = createdUser.CreatedAt
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -54,11 +41,5 @@ namespace PlanTempus.Components.Users.Create
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UserCreationResult
|
|
||||||
{
|
|
||||||
public long Id { get; set; }
|
|
||||||
public DateTime CreatedAt { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
namespace PlanTempus.Components.Users.Create
|
namespace PlanTempus.Components.Users.Create
|
||||||
{
|
{
|
||||||
public class CreateUserResponse
|
public class CreateUserResult
|
||||||
{
|
{
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
|
|
@ -170,8 +170,8 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
||||||
|
|
||||||
var superUser = new ConnectionStringParameters(
|
var superUser = new ConnectionStringParameters(
|
||||||
user: userPass.Split(":")[0],
|
User: userPass.Split(":")[0],
|
||||||
pwd: userPass.Split(":")[1]
|
Pwd: userPass.Split(":")[1]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (IsSuperAdmin(superUser))
|
if (IsSuperAdmin(superUser))
|
||||||
|
|
@ -191,7 +191,7 @@ namespace PlanTempus.SetupInfrastructure
|
||||||
sw.Restart();
|
sw.Restart();
|
||||||
|
|
||||||
//use application user, we use that role from now.
|
//use application user, we use that role from now.
|
||||||
var connParams = new ConnectionStringParameters(user: "heimdall", pwd: "3911");
|
var connParams = new ConnectionStringParameters(User: "heimdall", Pwd: "3911");
|
||||||
|
|
||||||
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
|
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
|
||||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
{"resources":{"Scripts/Script-1.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptmain"},"Scripts/Script.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptmain"},"Scripts/SmartConfigSystem.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptdb01"},"Scripts/grant-privileges.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44"}}}
|
{"resources":{"Scripts/Script-1.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptmain"},"Scripts/Script.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptmain"},"Scripts/SmartConfigSystem.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptdb01"},"Scripts/grant-privileges.sql":{"default-datasource":"postgres-jdbc-1948450a8b4-5fc9eec404e65c44","default-catalog":"ptmain"}}}
|
||||||
29
qodana.yaml
Normal file
29
qodana.yaml
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#-------------------------------------------------------------------------------#
|
||||||
|
# Qodana analysis is configured by qodana.yaml file #
|
||||||
|
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
|
||||||
|
#-------------------------------------------------------------------------------#
|
||||||
|
version: "1.0"
|
||||||
|
|
||||||
|
#Specify IDE code to run analysis without container (Applied in CI/CD pipeline)
|
||||||
|
ide: QDNET
|
||||||
|
|
||||||
|
#Specify inspection profile for code analysis
|
||||||
|
profile:
|
||||||
|
name: qodana.starter
|
||||||
|
|
||||||
|
#Enable inspections
|
||||||
|
#include:
|
||||||
|
# - name: <SomeEnabledInspectionId>
|
||||||
|
|
||||||
|
#Disable inspections
|
||||||
|
#exclude:
|
||||||
|
# - name: <SomeDisabledInspectionId>
|
||||||
|
# paths:
|
||||||
|
# - <path/where/not/run/inspection>
|
||||||
|
|
||||||
|
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
|
||||||
|
#bootstrap: sh ./prepare-qodana.sh
|
||||||
|
|
||||||
|
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
|
||||||
|
#plugins:
|
||||||
|
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue