Restructure data access layer/data layer

This commit is contained in:
Aaron Po
2026-01-11 23:36:26 -05:00
parent 8d6b903aa7
commit 372aac897a
17 changed files with 457 additions and 256 deletions

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using DataAccessLayer.Entities;
namespace DataAccessLayer
{
public interface IUserAccountRepository : IRepository<UserAccount>
{
IEnumerable<UserAccount> GetAll();
UserAccount? GetByUsername(string username);
UserAccount? GetByEmail(string email);
}
}

View File

@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using DataAccessLayer.Entities;
using Microsoft.Data.SqlClient;
namespace DataAccessLayer
{
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."
);
}
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);
connection.Open();
command.ExecuteNonQuery();
}
public UserAccount? GetById(Guid id)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new(
"usp_GetUserAccountById",
connection
);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@UserAccountId", id);
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(reader) : null;
}
public void Update(UserAccount userAccount)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new("usp_UpdateUserAccount", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
AddUserAccountUpdateParameters(command, userAccount);
connection.Open();
command.ExecuteNonQuery();
}
public void Delete(Guid id)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new(
"usp_DeleteUserAccount",
connection
);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@UserAccountId", id);
connection.Open();
command.ExecuteNonQuery();
}
public IEnumerable<UserAccount> GetAll()
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new(
"usp_GetAllUserAccounts",
connection
);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
List<UserAccount> users = new();
while (reader.Read())
{
users.Add(MapUserAccount(reader));
}
return users;
}
public UserAccount? GetByUsername(string username)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new(
"usp_GetUserAccountByUsername",
connection
);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Username", username);
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(reader) : null;
}
public UserAccount? GetByEmail(string email)
{
using SqlConnection connection = new(_connectionString);
using SqlCommand command = new(
"usp_GetUserAccountByEmail",
connection
);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Email", email);
connection.Open();
using SqlDataReader reader = command.ExecuteReader();
return reader.Read() ? MapUserAccount(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)
{
return new UserAccount
{
UserAccountID = reader.GetGuid(0),
Username = reader.GetString(1),
FirstName = reader.GetString(2),
LastName = reader.GetString(3),
Email = reader.GetString(4),
CreatedAt = reader.GetDateTime(5),
UpdatedAt = reader.IsDBNull(6) ? null : reader.GetDateTime(6),
DateOfBirth = reader.GetDateTime(7),
Timer = reader.IsDBNull(8) ? null : (byte[])reader[8],
};
}
}
}

View File

@@ -2,7 +2,7 @@
using System.Data;
using Microsoft.Data.SqlClient;
namespace DataAccessLayer
namespace DataAccessLayer.Sql
{
public class DatabaseHelper
{

View File

@@ -0,0 +1,187 @@
USE Biergarten;
GO
CREATE OR ALTER PROCEDURE usp_CreateUserAccount
(
@UserAccountId UNIQUEIDENTIFIER = NULL,
@Username VARCHAR(64),
@FirstName NVARCHAR(128),
@LastName NVARCHAR(128),
@DateOfBirth DATETIME,
@Email VARCHAR(128)
)
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO UserAccount
(
UserAccountID,
Username,
FirstName,
LastName,
DateOfBirth,
Email
)
VALUES
(
COALESCE(@UserAccountId, NEWID()),
@Username,
@FirstName,
@LastName,
@DateOfBirth,
@Email
);
COMMIT TRANSACTION
END;
GO
CREATE OR ALTER PROCEDURE usp_DeleteUserAccount
(
@UserAccountId UNIQUEIDENTIFIER
)
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRANSACTION
IF NOT EXISTS (SELECT 1 FROM UserAccount WHERE UserAccountId = @UserAccountId)
BEGIN
RAISERROR('UserAccount with the specified ID does not exist.', 16,
1);
ROLLBACK TRANSACTION
RETURN
END
DELETE FROM UserAccount
WHERE UserAccountId = @UserAccountId;
COMMIT TRANSACTION
END;
GO
CREATE OR ALTER PROCEDURE usp_UpdateUserAccount
(
@Username VARCHAR(64),
@FirstName NVARCHAR(128),
@LastName NVARCHAR(128),
@DateOfBirth DATETIME,
@Email VARCHAR(128),
@UserAccountId UNIQUEIDENTIFIER
)
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRANSACTION
IF NOT EXISTS (SELECT 1 FROM UserAccount WHERE UserAccountId = @UserAccountId)
BEGIN
RAISERROR('UserAccount with the specified ID does not exist.', 16,
1);
ROLLBACK TRANSACTION
RETURN
END
UPDATE UserAccount
SET
Username = @Username,
FirstName = @FirstName,
LastName = @LastName,
DateOfBirth = @DateOfBirth,
Email = @Email
WHERE UserAccountId = @UserAccountId;
COMMIT TRANSACTION
END;
GO
CREATE OR ALTER PROCEDURE usp_GetUserAccountById
(
@UserAccountId UNIQUEIDENTIFIER
)
AS
BEGIN
SET NOCOUNT ON;
SELECT UserAccountID,
Username,
FirstName,
LastName,
Email,
CreatedAt,
UpdatedAt,
DateOfBirth,
Timer
FROM dbo.UserAccount
WHERE UserAccountID = @UserAccountId;
END;
GO
CREATE OR ALTER PROCEDURE usp_GetAllUserAccounts
AS
BEGIN
SET NOCOUNT ON;
SELECT UserAccountID,
Username,
FirstName,
LastName,
Email,
CreatedAt,
UpdatedAt,
DateOfBirth,
Timer
FROM dbo.UserAccount;
END;
GO
CREATE OR ALTER PROCEDURE usp_GetUserAccountByUsername
(
@Username VARCHAR(64)
)
AS
BEGIN
SET NOCOUNT ON;
SELECT UserAccountID,
Username,
FirstName,
LastName,
Email,
CreatedAt,
UpdatedAt,
DateOfBirth,
Timer
FROM dbo.UserAccount
WHERE Username = @Username;
END;
GO
CREATE OR ALTER PROCEDURE usp_GetUserAccountByEmail
(
@Email VARCHAR(128)
)
AS
BEGIN
SET NOCOUNT ON;
SELECT UserAccountID,
Username,
FirstName,
LastName,
Email,
CreatedAt,
UpdatedAt,
DateOfBirth,
Timer
FROM dbo.UserAccount
WHERE Email = @Email;
END;
GO

View File

@@ -1,51 +0,0 @@
using System;
using System.Collections.Generic;
using System.Data;
using DataAccessLayer.Entities;
using Microsoft.Data.SqlClient;
namespace DataAccessLayer
{
public class UserAccountRepository : IRepository<UserAccount>
{
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."
);
}
public void Add(UserAccount userAccount)
{
}
public UserAccount? GetById(Guid id)
{
return null;
}
public void Update(UserAccount userAccount)
{
}
public void Delete(Guid id)
{
}
public IEnumerable<UserAccount> GetAll()
{
return new List<UserAccount>
{
};
}
}
}