Refactor user entities and repositories, update seeders

Standardized property naming in user-related entities to use 'Id' suffix (e.g., UserAccountId). Moved and updated repository interfaces and implementations to the DataAccessLayer.Repositories namespace. Refactored DBSeed seeders to use repository classes and improved structure. Updated .gitignore and project references
This commit is contained in:
Aaron Po
2026-01-15 13:23:41 -05:00
parent 60ef65ec52
commit b8cd855916
16 changed files with 660 additions and 760 deletions

View File

@@ -2,7 +2,7 @@ namespace DataAccessLayer.Entities;
public class UserAccount
{
public Guid UserAccountID { get; set; }
public Guid UserAccountId { get; set; }
public string Username { get; set; } = string.Empty;
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;

View File

@@ -2,8 +2,8 @@ namespace DataAccessLayer.Entities;
public class UserCredential
{
public Guid UserCredentialID { get; set; }
public Guid UserAccountID { get; set; }
public Guid UserCredentialId { get; set; }
public Guid UserAccountId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime Expiry { get; set; }
public string Hash { get; set; } = string.Empty;

View File

@@ -2,8 +2,8 @@ namespace DataAccessLayer.Entities;
public class UserVerification
{
public Guid UserVerificationID { get; set; }
public Guid UserAccountID { get; set; }
public Guid UserVerificationId { get; set; }
public Guid UserAccountId { get; set; }
public DateTime VerificationDateTime { get; set; }
public byte[]? Timer { get; set; }
}

View File

@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
namespace DataAccessLayer
using Microsoft.Data.SqlClient;
namespace DataAccessLayer.Repositories
{
public interface IRepository<T>
where T : class
@@ -13,5 +14,7 @@ namespace DataAccessLayer
T? GetById(Guid id);
void Update(T entity);
void Delete(Guid id);
T MapToEntity(SqlDataReader entity);
}
}

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using DataAccessLayer.Entities;
namespace DataAccessLayer
namespace DataAccessLayer.Repositories
{
public interface IUserAccountRepository : IRepository<UserAccount>
{

View File

@@ -1,30 +1,36 @@
using System;
using System.Collections.Generic;
using DataAccessLayer.Entities;
using Microsoft.Data.SqlClient;
namespace DataAccessLayer
namespace DataAccessLayer.Repositories
{
public class UserAccountRepository : IUserAccountRepository
{
private readonly string _connectionString;
public UserAccountRepository()
{
// Retrieve the connection string from environment variables
_connectionString =
Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
?? throw new InvalidOperationException(
"The connection string is not set in the environment variables."
);
}
private readonly string _connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
?? throw new InvalidOperationException(
"The connection string is not set in the environment variables."
);
public void Add(UserAccount userAccount)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new("usp_CreateUserAccount", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
AddUserAccountCreateParameters(command, userAccount);
command.Parameters.AddWithValue(
"@UserAccountId",
userAccount.UserAccountId
);
command.Parameters.AddWithValue("@Username", userAccount.Username);
command.Parameters.AddWithValue(
"@FirstName",
userAccount.FirstName
);
command.Parameters.AddWithValue("@LastName", userAccount.LastName);
command.Parameters.AddWithValue("@Email", userAccount.Email);
command.Parameters.AddWithValue(
"@DateOfBirth",
userAccount.DateOfBirth
);
connection.Open();
command.ExecuteNonQuery();
}
@@ -41,7 +47,7 @@ namespace DataAccessLayer
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(reader) : null;
return reader.Read() ? MapToEntity(reader) : null;
}
public void Update(UserAccount userAccount)
@@ -49,7 +55,25 @@ namespace DataAccessLayer
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new("usp_UpdateUserAccount", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
AddUserAccountUpdateParameters(command, userAccount);
command.Parameters.AddWithValue(
"@UserAccountId",
userAccount.UserAccountId
);
command.Parameters.AddWithValue("@Username", userAccount.Username);
command.Parameters.AddWithValue(
"@FirstName",
userAccount.FirstName
);
command.Parameters.AddWithValue("@LastName", userAccount.LastName);
command.Parameters.AddWithValue("@Email", userAccount.Email);
command.Parameters.AddWithValue(
"@DateOfBirth",
userAccount.DateOfBirth
);
command.Parameters.AddWithValue(
"@UserAccountId",
userAccount.UserAccountId
);
connection.Open();
command.ExecuteNonQuery();
}
@@ -64,9 +88,10 @@ namespace DataAccessLayer
command.ExecuteNonQuery();
}
public IEnumerable<UserAccount> GetAll(int? limit, int? offset)
{
if (limit.HasValue && limit <= 0)
if (limit is <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(limit),
@@ -110,7 +135,7 @@ namespace DataAccessLayer
List<UserAccount> users = new();
while (reader.Read())
{
users.Add(MapUserAccount(reader));
users.Add(MapToEntity(reader));
}
return users;
@@ -127,8 +152,8 @@ namespace DataAccessLayer
command.Parameters.AddWithValue("@Username", username);
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(reader) : null;
using SqlDataReader? reader = command.ExecuteReader();
return reader.Read() ? MapToEntity(reader) : null;
}
public UserAccount? GetByEmail(string email)
@@ -143,48 +168,14 @@ namespace DataAccessLayer
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(reader) : null;
return reader.Read() ? MapToEntity(reader) : null;
}
private static void AddUserAccountCreateParameters(
SqlCommand command,
UserAccount userAccount
)
{
command.Parameters.AddWithValue(
"@UserAccountId",
userAccount.UserAccountID
);
command.Parameters.AddWithValue("@Username", userAccount.Username);
command.Parameters.AddWithValue(
"@FirstName",
userAccount.FirstName
);
command.Parameters.AddWithValue("@LastName", userAccount.LastName);
command.Parameters.AddWithValue("@Email", userAccount.Email);
command.Parameters.AddWithValue(
"@DateOfBirth",
userAccount.DateOfBirth
);
}
private static void AddUserAccountUpdateParameters(
SqlCommand command,
UserAccount userAccount
)
{
AddUserAccountCreateParameters(command, userAccount);
command.Parameters.AddWithValue(
"@UserAccountId",
userAccount.UserAccountID
);
}
private static UserAccount MapUserAccount(SqlDataReader reader)
public UserAccount MapToEntity(SqlDataReader reader)
{
return new UserAccount
{
UserAccountID = reader.GetGuid(0),
UserAccountId = reader.GetGuid(0),
Username = reader.GetString(1),
FirstName = reader.GetString(2),
LastName = reader.GetString(3),

View File

@@ -4,45 +4,31 @@ using Microsoft.Data.SqlClient;
namespace DataAccessLayer.Sql
{
public class DatabaseHelper
public class DatabaseHelper(string connectionString)
{
private readonly string _connectionString;
public DatabaseHelper(string connectionString)
{
_connectionString = connectionString;
}
public void ExecuteRawSql(string query)
{
try
{
using (
SqlConnection connection = new SqlConnection(
_connectionString
)
)
using var connection = new SqlConnection(
connectionString
);
connection.Open();
using var command = new SqlCommand(query, connection);
command.CommandType = CommandType.Text;
using var reader = command.ExecuteReader();
while (reader.Read())
{
connection.Open();
using (
SqlCommand command = new SqlCommand(query, connection)
)
for (var i = 0; i < reader.FieldCount; i++)
{
command.CommandType = CommandType.Text;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine(
$"{reader.GetName(i)}: {reader.GetValue(i)}"
);
}
}
}
Console.WriteLine(
$"{reader.GetName(i)}: {reader.GetValue(i)}"
);
}
}
}