mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Restructure data access layer/data layer
This commit is contained in:
13
DataAccessLayer/Repositories/IUserAccountRepository.cs
Normal file
13
DataAccessLayer/Repositories/IUserAccountRepository.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
163
DataAccessLayer/Repositories/UserAccountRepository.cs
Normal file
163
DataAccessLayer/Repositories/UserAccountRepository.cs
Normal 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],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user