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

@@ -0,0 +1,20 @@
using Microsoft.Data.SqlClient;
namespace DataAccessLayer.Repositories
{
public interface IRepository<T>
where T : class
{
void Add(T entity);
IEnumerable<T> GetAll(int? limit, int? offset);
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),