Renaming from tenant to organization
This commit is contained in:
parent
bf50563ab7
commit
087f8ce0e9
16 changed files with 129 additions and 243 deletions
|
|
@ -6,6 +6,8 @@ namespace Database.Core
|
|||
{
|
||||
public class UserService
|
||||
{
|
||||
public record UserCreateCommand(string CorrelationId, string Email, string Password);
|
||||
|
||||
private readonly IDbConnection _db;
|
||||
|
||||
public UserService(IDbConnection db)
|
||||
|
|
@ -13,56 +15,57 @@ namespace Database.Core
|
|||
_db = db;
|
||||
}
|
||||
|
||||
public async Task CreateUserWithTenant(string email, string password, string tenantConnectionString)
|
||||
public async Task CreateUser(UserCreateCommand command)
|
||||
{
|
||||
var schema = "dev";
|
||||
|
||||
if (_db.State != ConnectionState.Open)
|
||||
_db.Open();
|
||||
|
||||
using var transaction = _db.BeginTransaction();
|
||||
try
|
||||
var user = new User
|
||||
{
|
||||
// Create user
|
||||
var user = new User
|
||||
{
|
||||
Email = email,
|
||||
PasswordHash = PasswordHasher.HashPassword(password),
|
||||
SecurityStamp = Guid.NewGuid().ToString(),
|
||||
EmailConfirmed = false,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
Email = command.Email,
|
||||
PasswordHash = PasswordHasher.HashPassword(command.Password),
|
||||
SecurityStamp = Guid.NewGuid().ToString(),
|
||||
EmailConfirmed = false,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
var userId = await _db.ExecuteScalarAsync<int>(@$"
|
||||
INSERT INTO {schema}.users (email, password_hash, security_stamp, email_confirmed, created_date)
|
||||
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);
|
||||
|
||||
// Create tenant
|
||||
var tenant = new Tenant
|
||||
}
|
||||
public async Task CreateOrganization(int userId, string organizationConnectionString)
|
||||
{
|
||||
var schema = "dev";
|
||||
|
||||
|
||||
using var transaction = _db.OpenWithTransaction();
|
||||
try
|
||||
{
|
||||
// Create organization
|
||||
var organization = new Organization
|
||||
{
|
||||
ConnectionString = tenantConnectionString,
|
||||
ConnectionString = organizationConnectionString,
|
||||
CreatedDate = DateTime.UtcNow,
|
||||
CreatedBy = userId,
|
||||
IsActive = true
|
||||
};
|
||||
|
||||
var tenantId = await _db.ExecuteScalarAsync<int>(@$"
|
||||
INSERT INTO {schema}.tenants (connection_string, created_date, created_by, is_active)
|
||||
var organizationId = await _db.ExecuteScalarAsync<int>(@$"
|
||||
INSERT INTO {schema}.organizations (connection_string, created_date, created_by, is_active)
|
||||
VALUES (@ConnectionString, @CreatedDate, @CreatedBy, @IsActive)
|
||||
RETURNING id", tenant);
|
||||
RETURNING id", organization);
|
||||
|
||||
// Link user to tenant
|
||||
var userTenant = new UserTenant
|
||||
// Link user to organization
|
||||
var userOrganization = new UserOrganization
|
||||
{
|
||||
UserId = userId,
|
||||
TenantId = tenantId,
|
||||
OrganizationId = organizationId,
|
||||
CreatedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await _db.ExecuteAsync(@$"
|
||||
INSERT INTO {schema}.user_tenants (user_id, tenant_id, created_date)
|
||||
VALUES (@UserId, @TenantId, @CreatedDate)", userTenant);
|
||||
INSERT INTO {schema}.user_organizations (user_id, organization_id, created_date)
|
||||
VALUES (@UserId, @OrganizationId, @CreatedDate)", userOrganization);
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue