Cleaning up with Rider
This commit is contained in:
parent
69758735de
commit
91da89a4e8
22 changed files with 574 additions and 386 deletions
|
|
@ -3,7 +3,7 @@
|
|||
public class CreateOrganizationCommand
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public Guid CreatedById { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,37 +12,27 @@ namespace PlanTempus.Components.Organizations.Create
|
|||
_databaseOperations = databaseOperations;
|
||||
}
|
||||
|
||||
public async Task<CreateOrganizationResponse> Handle(CreateOrganizationCommand command)
|
||||
public async Task<CreateOrganizationResult> Handle(CreateOrganizationCommand command)
|
||||
{
|
||||
using var db = _databaseOperations.CreateScope(nameof(CreateOrganizationHandler));
|
||||
|
||||
try
|
||||
{
|
||||
var organizationId = Guid.NewGuid();
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
var sql = @"
|
||||
INSERT INTO organizations (id, name, description, created_by_id)
|
||||
VALUES (@Id, @Name, @Description, @CreatedById)";
|
||||
var sql = @"
|
||||
INSERT INTO organizations (connection_string, created_by)
|
||||
VALUES (@ConnectionString, @CreatedBy)
|
||||
RETURNING id, created_at";
|
||||
|
||||
|
||||
await db.Connection.ExecuteSqlAsync(sql, new
|
||||
var data = await db.Connection.QuerySqlAsync<CreateOrganizationResult>(sql, new
|
||||
{
|
||||
Id = organizationId,
|
||||
command.Name,
|
||||
command.Description,
|
||||
CreatedById = command.CreatedById,
|
||||
CreatedAt = now,
|
||||
UpdatedAt = now
|
||||
ConnectionString = command.ConnectionString,
|
||||
CreatedBy = command.CreatedById
|
||||
});
|
||||
|
||||
db.Success();
|
||||
|
||||
return new CreateOrganizationResponse
|
||||
{
|
||||
Id = organizationId,
|
||||
Name = command.Name,
|
||||
CreatedAt = now
|
||||
};
|
||||
return data.First();
|
||||
}
|
||||
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
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CreateUserResponse>> Create([FromBody] CreateUserCommand command)
|
||||
public async Task<ActionResult<CreateUserResult>> Create([FromBody] CreateUserCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,60 +5,41 @@ using PlanTempus.Core.Sql;
|
|||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public class CreateUserHandler(IDatabaseOperations databaseOperations, ISecureTokenizer secureTokenizer)
|
||||
{
|
||||
private readonly ISecureTokenizer _secureTokenizer;
|
||||
|
||||
public async Task<CreateUserResponse> Handle(CreateUserCommand command)
|
||||
{
|
||||
using var db = databaseOperations.CreateScope(nameof(CreateUserHandler));
|
||||
try
|
||||
{
|
||||
var sql = @"
|
||||
{
|
||||
public async Task<CreateUserResult> Handle(CreateUserCommand command)
|
||||
{
|
||||
using var db = databaseOperations.CreateScope(nameof(CreateUserHandler));
|
||||
try
|
||||
{
|
||||
var sql = @"
|
||||
INSERT INTO system.users(email, password_hash, security_stamp, email_confirmed,
|
||||
access_failed_count, lockout_enabled, lockout_end,
|
||||
is_active, created_at, last_login_at)
|
||||
access_failed_count, lockout_enabled,
|
||||
is_active)
|
||||
VALUES(@Email, @PasswordHash, @SecurityStamp, @EmailConfirmed,
|
||||
@AccessFailedCount, @LockoutEnabled, @LockoutEnd,
|
||||
@IsActive, @CreatedAt, @LastLoginAt)
|
||||
RETURNING id, created_at";
|
||||
@AccessFailedCount, @LockoutEnabled, @IsActive)
|
||||
RETURNING id, created_at, email, is_active";
|
||||
|
||||
var result = await db.Connection.QuerySqlAsync<UserCreationResult>(sql, new
|
||||
{
|
||||
Email = command.Email,
|
||||
PasswordHash = secureTokenizer.TokenizeText(command.Password),
|
||||
SecurityStamp = Guid.NewGuid().ToString("N"),
|
||||
EmailConfirmed = false,
|
||||
AccessFailedCount = 0,
|
||||
LockoutEnabled = true,
|
||||
LockoutEnd = (DateTime?)null,
|
||||
IsActive = command.IsActive,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
LastLoginAt = (DateTime?)null
|
||||
});
|
||||
var data = await db.Connection.QuerySqlAsync<CreateUserResult>(sql, new
|
||||
{
|
||||
Email = command.Email,
|
||||
PasswordHash = secureTokenizer.TokenizeText(command.Password),
|
||||
SecurityStamp = Guid.NewGuid().ToString("N"),
|
||||
EmailConfirmed = false,
|
||||
AccessFailedCount = 0,
|
||||
LockoutEnabled = false,
|
||||
IsActive = command.IsActive,
|
||||
});
|
||||
|
||||
var createdUser = result.First();
|
||||
|
||||
db.Success();
|
||||
db.Success();
|
||||
|
||||
return new CreateUserResponse
|
||||
{
|
||||
Id = createdUser.Id,
|
||||
Email = command.Email,
|
||||
IsActive = command.IsActive,
|
||||
CreatedAt = createdUser.CreatedAt
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private class UserCreationResult
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
}
|
||||
}
|
||||
return data.First();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
db.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
namespace PlanTempus.Components.Users.Create
|
||||
{
|
||||
public class CreateUserResponse
|
||||
public class CreateUserResult
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Email { get; set; }
|
||||
Loading…
Add table
Add a link
Reference in a new issue