mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DataAccessLayer.Entities;
|
||||
|
||||
namespace DataAccessLayer
|
||||
namespace DataAccessLayer.Repositories
|
||||
{
|
||||
public interface IUserAccountRepository : IRepository<UserAccount>
|
||||
{
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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)}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user