Initial commit: SWP.Core enterprise framework with multi-tenant architecture, configuration management, security, telemetry and comprehensive test suite
This commit is contained in:
commit
5275a75502
87 changed files with 6140 additions and 0 deletions
79
Database/Core/UserService.cs
Normal file
79
Database/Core/UserService.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using Insight.Database;
|
||||
using PlanTempus.Core;
|
||||
using PlanTempus.Core.Entities.Users;
|
||||
using System.Data;
|
||||
|
||||
namespace PlanTempus.Database.Core
|
||||
{
|
||||
public class UserService
|
||||
{
|
||||
public record UserCreateCommand(string CorrelationId, string Email, string Password);
|
||||
|
||||
private readonly IDbConnection _db;
|
||||
|
||||
public UserService(IDbConnection db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task CreateUser(UserCreateCommand command)
|
||||
{
|
||||
var user = new User
|
||||
{
|
||||
Email = command.Email,
|
||||
PasswordHash = new SecureTokenizer().TokenizeText(command.Password),
|
||||
SecurityStamp = Guid.NewGuid().ToString(),
|
||||
EmailConfirmed = false,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
var userId = await _db.ExecuteScalarAsync<int>(@$"
|
||||
INSERT INTO users (email, password_hash, security_stamp, email_confirmed, created_at)
|
||||
VALUES (@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed, @CreatedDate)
|
||||
RETURNING id", user);
|
||||
}
|
||||
|
||||
public async Task CreateOrganization(int userId, string organizationConnectionString)
|
||||
{
|
||||
var schema = "dev";
|
||||
|
||||
|
||||
using var transaction = _db.OpenWithTransaction();
|
||||
try
|
||||
{
|
||||
// Create organization
|
||||
var organization = new Organization
|
||||
{
|
||||
ConnectionString = organizationConnectionString,
|
||||
CreatedDate = DateTime.UtcNow,
|
||||
CreatedBy = userId,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
var organizationId = await _db.ExecuteScalarAsync<int>(@$"
|
||||
INSERT INTO {schema}.organizations (connection_string, created_date, is_active)
|
||||
VALUES (@ConnectionString, @CreatedDate, @IsActive)
|
||||
RETURNING id", organization);
|
||||
|
||||
// Link user to organization
|
||||
var userOrganization = new UserOrganization
|
||||
{
|
||||
UserId = userId,
|
||||
OrganizationId = organizationId,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await _db.ExecuteAsync(@$"
|
||||
INSERT INTO {schema}.user_organizations (user_id, organization_id, created_date)
|
||||
VALUES (@UserId, @OrganizationId, @CreatedDate)", userOrganization);
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue