code style cleanup

This commit is contained in:
Aaron Po
2026-06-20 13:55:17 -04:00
parent 07aedcb866
commit 254431928f
167 changed files with 3711 additions and 3522 deletions

View File

@@ -4,7 +4,7 @@ using MediatR;
namespace Features.UserManagement.Commands.UpdateUser;
/// <summary>
/// Updates an existing user account. Not currently exposed via any HTTP route, carried forward
/// from <c>IUserService.UpdateAsync</c> as-is.
/// Updates an existing user account. Not currently exposed via any HTTP route, carried forward
/// from <c>IUserService.UpdateAsync</c> as-is.
/// </summary>
public record UpdateUserCommand(UserAccount UserAccount) : IRequest;
public record UpdateUserCommand(UserAccount UserAccount) : IRequest;

View File

@@ -4,12 +4,14 @@ using MediatR;
namespace Features.UserManagement.Commands.UpdateUser;
/// <summary>
/// Handles <see cref="UpdateUserCommand"/> by persisting changes to an existing user account.
/// Handles <see cref="UpdateUserCommand" /> by persisting changes to an existing user account.
/// </summary>
/// <param name="repository">Repository used to persist the updated user account.</param>
public class UpdateUserHandler(IUserAccountRepository repository)
: IRequestHandler<UpdateUserCommand>
{
public Task Handle(UpdateUserCommand request, CancellationToken cancellationToken) =>
repository.UpdateAsync(request.UserAccount);
}
public Task Handle(UpdateUserCommand request, CancellationToken cancellationToken)
{
return repository.UpdateAsync(request.UserAccount);
}
}

View File

@@ -4,42 +4,41 @@ using Features.UserManagement.Queries.GetUserById;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Features.UserManagement.Controllers
namespace Features.UserManagement.Controllers;
/// <summary>
/// Provides read-only endpoints for retrieving user accounts.
/// </summary>
/// <param name="mediator">Used to dispatch user queries to their handlers.</param>
[ApiController]
[Route("api/[controller]")]
public class UserController(IMediator mediator) : ControllerBase
{
/// <summary>
/// Provides read-only endpoints for retrieving user accounts.
/// Retrieves a paginated list of user accounts.
/// </summary>
/// <param name="mediator">Used to dispatch user queries to their handlers.</param>
[ApiController]
[Route("api/[controller]")]
public class UserController(IMediator mediator) : ControllerBase
/// <param name="limit">The maximum number of user accounts to return, or <c>null</c> for no limit.</param>
/// <param name="offset">The number of user accounts to skip before returning results, or <c>null</c> for no offset.</param>
/// <returns>A <c>200 OK</c> result containing the collection of <see cref="UserAccount" /> entities.</returns>
[HttpGet]
public async Task<ActionResult<IEnumerable<UserAccount>>> GetAll(
[FromQuery] int? limit,
[FromQuery] int? offset
)
{
/// <summary>
/// Retrieves a paginated list of user accounts.
/// </summary>
/// <param name="limit">The maximum number of user accounts to return, or <c>null</c> for no limit.</param>
/// <param name="offset">The number of user accounts to skip before returning results, or <c>null</c> for no offset.</param>
/// <returns>A <c>200 OK</c> result containing the collection of <see cref="UserAccount"/> entities.</returns>
[HttpGet]
public async Task<ActionResult<IEnumerable<UserAccount>>> GetAll(
[FromQuery] int? limit,
[FromQuery] int? offset
)
{
var users = await mediator.Send(new GetAllUsersQuery(limit, offset));
return Ok(users);
}
/// <summary>
/// Retrieves a single user account by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier of the user account to retrieve.</param>
/// <returns>A <c>200 OK</c> result containing the matching <see cref="UserAccount"/>.</returns>
[HttpGet("{id:guid}")]
public async Task<ActionResult<UserAccount>> GetById(Guid id)
{
var user = await mediator.Send(new GetUserByIdQuery(id));
return Ok(user);
}
IEnumerable<UserAccount> users = await mediator.Send(new GetAllUsersQuery(limit, offset));
return Ok(users);
}
}
/// <summary>
/// Retrieves a single user account by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier of the user account to retrieve.</param>
/// <returns>A <c>200 OK</c> result containing the matching <see cref="UserAccount" />.</returns>
[HttpGet("{id:guid}")]
public async Task<ActionResult<UserAccount>> GetById(Guid id)
{
UserAccount user = await mediator.Send(new GetUserByIdQuery(id));
return Ok(user);
}
}

View File

@@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace Features.UserManagement.DependencyInjection;
/// <summary>
/// Registers the services owned by the UserManagement feature slice.
/// Registers the services owned by the UserManagement feature slice.
/// </summary>
public static class FeaturesUserManagementServiceCollectionExtensions
{
@@ -13,4 +13,4 @@ public static class FeaturesUserManagementServiceCollectionExtensions
services.AddScoped<IUserAccountRepository, UserAccountRepository>();
return services;
}
}
}

View File

@@ -1,20 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Features.UserManagement</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Features.UserManagement</RootNamespace>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Domain\Domain.Entities\Domain.Entities.csproj" />
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj" />
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Sql\Infrastructure.Sql.csproj" />
<ProjectReference Include="..\..\Shared\Shared.Contracts\Shared.Contracts.csproj" />
<ProjectReference Include="..\..\Shared\Shared.Application\Shared.Application.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Domain\Domain.Entities\Domain.Entities.csproj"/>
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj"/>
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Sql\Infrastructure.Sql.csproj"/>
<ProjectReference Include="..\..\Shared\Shared.Contracts\Shared.Contracts.csproj"/>
<ProjectReference Include="..\..\Shared\Shared.Application\Shared.Application.csproj"/>
</ItemGroup>
</Project>

View File

@@ -5,12 +5,14 @@ using MediatR;
namespace Features.UserManagement.Queries.GetAllUsers;
/// <summary>
/// Handles <see cref="GetAllUsersQuery"/> by retrieving a paginated list of user accounts.
/// Handles <see cref="GetAllUsersQuery" /> by retrieving a paginated list of user accounts.
/// </summary>
/// <param name="repository">Repository used to query user account data.</param>
public class GetAllUsersHandler(IUserAccountRepository repository)
: IRequestHandler<GetAllUsersQuery, IEnumerable<UserAccount>>
{
public Task<IEnumerable<UserAccount>> Handle(GetAllUsersQuery request, CancellationToken cancellationToken) =>
repository.GetAllAsync(request.Limit, request.Offset);
}
public Task<IEnumerable<UserAccount>> Handle(GetAllUsersQuery request, CancellationToken cancellationToken)
{
return repository.GetAllAsync(request.Limit, request.Offset);
}
}

View File

@@ -4,6 +4,6 @@ using MediatR;
namespace Features.UserManagement.Queries.GetAllUsers;
/// <summary>
/// Retrieves a paginated list of user accounts.
/// Retrieves a paginated list of user accounts.
/// </summary>
public record GetAllUsersQuery(int? Limit, int? Offset) : IRequest<IEnumerable<UserAccount>>;
public record GetAllUsersQuery(int? Limit, int? Offset) : IRequest<IEnumerable<UserAccount>>;

View File

@@ -6,7 +6,7 @@ using MediatR;
namespace Features.UserManagement.Queries.GetUserById;
/// <summary>
/// Handles <see cref="GetUserByIdQuery"/> by looking up the matching user account.
/// Handles <see cref="GetUserByIdQuery" /> by looking up the matching user account.
/// </summary>
/// <param name="repository">Repository used to query user account data.</param>
public class GetUserByIdHandler(IUserAccountRepository repository)
@@ -15,9 +15,9 @@ public class GetUserByIdHandler(IUserAccountRepository repository)
/// <exception cref="NotFoundException">Thrown when no user account exists with the given ID.</exception>
public async Task<UserAccount> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
{
var user = await repository.GetByIdAsync(request.UserAccountId);
UserAccount? user = await repository.GetByIdAsync(request.UserAccountId);
if (user is null)
throw new NotFoundException($"User with ID {request.UserAccountId} not found");
return user;
}
}
}

View File

@@ -4,6 +4,6 @@ using MediatR;
namespace Features.UserManagement.Queries.GetUserById;
/// <summary>
/// Retrieves a single user account by its unique identifier.
/// Retrieves a single user account by its unique identifier.
/// </summary>
public record GetUserByIdQuery(Guid UserAccountId) : IRequest<UserAccount>;
public record GetUserByIdQuery(Guid UserAccountId) : IRequest<UserAccount>;

View File

@@ -1,51 +1,53 @@
using Domain.Entities;
namespace Features.UserManagement.Repository;
/// <summary>
/// Repository for CRUD operations on user account records.
/// Repository for CRUD operations on user account records.
/// </summary>
public interface IUserAccountRepository
{
/// <summary>
/// Retrieves a user account by its unique identifier.
/// Retrieves a user account by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier of the user account.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
Task<Domain.Entities.UserAccount?> GetByIdAsync(Guid id);
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
Task<UserAccount?> GetByIdAsync(Guid id);
/// <summary>
/// Retrieves all user accounts, optionally paginated.
/// Retrieves all user accounts, optionally paginated.
/// </summary>
/// <param name="limit">The maximum number of records to return, or <c>null</c> for no limit.</param>
/// <param name="offset">The number of records to skip, or <c>null</c> for no offset.</param>
/// <returns>The collection of matching <see cref="Domain.Entities.UserAccount"/> records.</returns>
Task<IEnumerable<Domain.Entities.UserAccount>> GetAllAsync(
/// <returns>The collection of matching <see cref="Domain.Entities.UserAccount" /> records.</returns>
Task<IEnumerable<UserAccount>> GetAllAsync(
int? limit,
int? offset
);
/// <summary>
/// Updates an existing user account's details.
/// Updates an existing user account's details.
/// </summary>
/// <param name="userAccount">The user account containing updated values. Must have a valid <c>UserAccountId</c>.</param>
Task UpdateAsync(Domain.Entities.UserAccount userAccount);
Task UpdateAsync(UserAccount userAccount);
/// <summary>
/// Deletes a user account by its unique identifier.
/// Deletes a user account by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier of the user account to delete.</param>
Task DeleteAsync(Guid id);
/// <summary>
/// Retrieves a user account by username.
/// Retrieves a user account by username.
/// </summary>
/// <param name="username">The username to search for.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
Task<Domain.Entities.UserAccount?> GetByUsernameAsync(string username);
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
Task<UserAccount?> GetByUsernameAsync(string username);
/// <summary>
/// Retrieves a user account by email address.
/// Retrieves a user account by email address.
/// </summary>
/// <param name="email">The email address to search for.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
Task<Domain.Entities.UserAccount?> GetByEmailAsync(string email);
}
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
Task<UserAccount?> GetByEmailAsync(string email);
}

View File

@@ -1,53 +1,54 @@
using System.Data;
using System.Data.Common;
using Domain.Entities;
using Infrastructure.Sql;
namespace Features.UserManagement.Repository;
/// <summary>
/// ADO.NET-based implementation of <see cref="IUserAccountRepository"/> backed by SQL Server
/// stored procedures.
/// ADO.NET-based implementation of <see cref="IUserAccountRepository" /> backed by SQL Server
/// stored procedures.
/// </summary>
/// <param name="connectionFactory">The factory used to create database connections.</param>
public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
: Infrastructure.Sql.Repository<Domain.Entities.UserAccount>(connectionFactory),
: Repository<UserAccount>(connectionFactory),
IUserAccountRepository
{
/// <summary>
/// Retrieves a user account by ID using the <c>usp_GetUserAccountById</c> stored procedure.
/// Retrieves a user account by ID using the <c>usp_GetUserAccountById</c> stored procedure.
/// </summary>
/// <param name="id">The unique identifier of the user account.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task<Domain.Entities.UserAccount?> GetByIdAsync(Guid id)
public async Task<UserAccount?> GetByIdAsync(Guid id)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_GetUserAccountById";
command.CommandType = CommandType.StoredProcedure;
AddParameter(command, "@UserAccountId", id);
await using var reader = await command.ExecuteReaderAsync();
await using DbDataReader reader = await command.ExecuteReaderAsync();
return await reader.ReadAsync() ? MapToEntity(reader) : null;
}
/// <summary>
/// Retrieves all user accounts, optionally paginated, using the <c>usp_GetAllUserAccounts</c>
/// stored procedure. The <c>@Limit</c> and <c>@Offset</c> parameters are only added when their
/// corresponding argument has a value.
/// Retrieves all user accounts, optionally paginated, using the <c>usp_GetAllUserAccounts</c>
/// stored procedure. The <c>@Limit</c> and <c>@Offset</c> parameters are only added when their
/// corresponding argument has a value.
/// </summary>
/// <param name="limit">The maximum number of records to return, or <c>null</c> for no limit.</param>
/// <param name="offset">The number of records to skip, or <c>null</c> for no offset.</param>
/// <returns>The collection of matching <see cref="Domain.Entities.UserAccount"/> records.</returns>
/// <returns>The collection of matching <see cref="Domain.Entities.UserAccount" /> records.</returns>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task<IEnumerable<Domain.Entities.UserAccount>> GetAllAsync(
public async Task<IEnumerable<UserAccount>> GetAllAsync(
int? limit,
int? offset
)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_GetAllUserAccounts";
command.CommandType = CommandType.StoredProcedure;
@@ -57,27 +58,24 @@ public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
if (offset.HasValue)
AddParameter(command, "@Offset", offset.Value);
await using var reader = await command.ExecuteReaderAsync();
var users = new List<Domain.Entities.UserAccount>();
await using DbDataReader reader = await command.ExecuteReaderAsync();
List<UserAccount> users = new();
while (await reader.ReadAsync())
{
users.Add(MapToEntity(reader));
}
while (await reader.ReadAsync()) users.Add(MapToEntity(reader));
return users;
}
/// <summary>
/// Updates a user account's username, first name, last name, email, and date of birth using
/// the <c>usp_UpdateUserAccount</c> stored procedure.
/// Updates a user account's username, first name, last name, email, and date of birth using
/// the <c>usp_UpdateUserAccount</c> stored procedure.
/// </summary>
/// <param name="userAccount">The user account containing updated values. Must have a valid <c>UserAccountId</c>.</param>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task UpdateAsync(Domain.Entities.UserAccount userAccount)
public async Task UpdateAsync(UserAccount userAccount)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_UpdateUserAccount";
command.CommandType = CommandType.StoredProcedure;
@@ -92,14 +90,14 @@ public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
}
/// <summary>
/// Deletes a user account by ID using the <c>usp_DeleteUserAccount</c> stored procedure.
/// Deletes a user account by ID using the <c>usp_DeleteUserAccount</c> stored procedure.
/// </summary>
/// <param name="id">The unique identifier of the user account to delete.</param>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task DeleteAsync(Guid id)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_DeleteUserAccount";
command.CommandType = CommandType.StoredProcedure;
@@ -108,57 +106,57 @@ public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
}
/// <summary>
/// Retrieves a user account by username using the <c>usp_GetUserAccountByUsername</c> stored procedure.
/// Retrieves a user account by username using the <c>usp_GetUserAccountByUsername</c> stored procedure.
/// </summary>
/// <param name="username">The username to search for.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task<Domain.Entities.UserAccount?> GetByUsernameAsync(
public async Task<UserAccount?> GetByUsernameAsync(
string username
)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_GetUserAccountByUsername";
command.CommandType = CommandType.StoredProcedure;
AddParameter(command, "@Username", username);
await using var reader = await command.ExecuteReaderAsync();
await using DbDataReader reader = await command.ExecuteReaderAsync();
return await reader.ReadAsync() ? MapToEntity(reader) : null;
}
/// <summary>
/// Retrieves a user account by email address using the <c>usp_GetUserAccountByEmail</c> stored procedure.
/// Retrieves a user account by email address using the <c>usp_GetUserAccountByEmail</c> stored procedure.
/// </summary>
/// <param name="email">The email address to search for.</param>
/// <returns>The matching <see cref="Domain.Entities.UserAccount"/>, or <c>null</c> if not found.</returns>
/// <returns>The matching <see cref="Domain.Entities.UserAccount" />, or <c>null</c> if not found.</returns>
/// <exception cref="Microsoft.Data.SqlClient.SqlException">Thrown when the database command fails.</exception>
public async Task<Domain.Entities.UserAccount?> GetByEmailAsync(
public async Task<UserAccount?> GetByEmailAsync(
string email
)
{
await using var connection = await CreateConnection();
await using var command = connection.CreateCommand();
await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand();
command.CommandText = "usp_GetUserAccountByEmail";
command.CommandType = CommandType.StoredProcedure;
AddParameter(command, "@Email", email);
await using var reader = await command.ExecuteReaderAsync();
await using DbDataReader reader = await command.ExecuteReaderAsync();
return await reader.ReadAsync() ? MapToEntity(reader) : null;
}
/// <summary>
/// Maps the current row of a data reader to a <see cref="Domain.Entities.UserAccount"/> entity.
/// Maps the current row of a data reader to a <see cref="Domain.Entities.UserAccount" /> entity.
/// </summary>
/// <param name="reader">The data reader positioned on the row to map.</param>
/// <returns>The mapped <see cref="Domain.Entities.UserAccount"/> instance.</returns>
protected override Domain.Entities.UserAccount MapToEntity(
/// <returns>The mapped <see cref="Domain.Entities.UserAccount" /> instance.</returns>
protected override UserAccount MapToEntity(
DbDataReader reader
)
{
return new Domain.Entities.UserAccount
return new UserAccount
{
UserAccountId = reader.GetGuid(reader.GetOrdinal("UserAccountId")),
Username = reader.GetString(reader.GetOrdinal("Username")),
@@ -172,26 +170,26 @@ public class UserAccountRepository(ISqlConnectionFactory connectionFactory)
DateOfBirth = reader.GetDateTime(reader.GetOrdinal("DateOfBirth")),
Timer = reader.IsDBNull(reader.GetOrdinal("Timer"))
? null
: (byte[])reader["Timer"],
: (byte[])reader["Timer"]
};
}
/// <summary>
/// Helper method to add a parameter to a database command, converting <c>null</c> values to
/// <see cref="DBNull.Value"/>.
/// Helper method to add a parameter to a database command, converting <c>null</c> values to
/// <see cref="DBNull.Value" />.
/// </summary>
/// <param name="command">The command to add the parameter to.</param>
/// <param name="name">The parameter name (including any prefix, e.g. "@Username").</param>
/// <param name="value">The parameter value, or <c>null</c> to bind <see cref="DBNull.Value"/>.</param>
/// <param name="value">The parameter value, or <c>null</c> to bind <see cref="DBNull.Value" />.</param>
private static void AddParameter(
DbCommand command,
string name,
object? value
)
{
var p = command.CreateParameter();
DbParameter p = command.CreateParameter();
p.ParameterName = name;
p.Value = value ?? DBNull.Value;
command.Parameters.Add(p);
}
}
}