mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Update data access layer, begin acquiring raw data
This commit is contained in:
@@ -1,3 +1,55 @@
|
||||
namespace DataAccessLayer;
|
||||
using System;
|
||||
using System.Data;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
public class Class1 { }
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
public class DatabaseHelper
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public DatabaseHelper(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public void ExecuteRawSql(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (
|
||||
SqlConnection connection = new SqlConnection(
|
||||
_connectionString
|
||||
)
|
||||
)
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
using (
|
||||
SqlCommand command = new SqlCommand(query, connection)
|
||||
)
|
||||
{
|
||||
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)}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"An error occurred: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,12 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.3" />
|
||||
<PackageReference
|
||||
Include="Microsoft.SqlServer.Types"
|
||||
Version="160.1000.6"
|
||||
/>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
14
DataAccessLayer/IRepository.cs
Normal file
14
DataAccessLayer/IRepository.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DataAccessLayer
|
||||
{
|
||||
public interface IRepository<T>
|
||||
where T : class
|
||||
{
|
||||
void Add(T entity);
|
||||
T? GetById(Guid id);
|
||||
void Update(T entity);
|
||||
void Delete(Guid id);
|
||||
}
|
||||
}
|
||||
257
DataAccessLayer/UserAccountRepository.cs
Normal file
257
DataAccessLayer/UserAccountRepository.cs
Normal file
@@ -0,0 +1,257 @@
|
||||
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)
|
||||
{
|
||||
const string query =
|
||||
@"INSERT INTO UserAccount (UserAccountID, Username, FirstName, LastName, Email, CreatedAt, UpdatedAt, DateOfBirth, Timer)
|
||||
VALUES (@UserAccountID, @Username, @FirstName, @LastName, @Email, @CreatedAt, @UpdatedAt, @DateOfBirth, @Timer);";
|
||||
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
using (var command = new SqlCommand(query, connection))
|
||||
{
|
||||
_ = 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(
|
||||
"@CreatedAt",
|
||||
userAccount.CreatedAt
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@UpdatedAt",
|
||||
userAccount.UpdatedAt ?? (object)DBNull.Value
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@DateOfBirth",
|
||||
userAccount.DateOfBirth
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@Timer",
|
||||
userAccount.Timer ?? (object)DBNull.Value
|
||||
);
|
||||
|
||||
connection.Open();
|
||||
_ = command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
public UserAccount? GetById(Guid id)
|
||||
{
|
||||
const string query =
|
||||
"SELECT * FROM UserAccount WHERE UserAccountID = @UserAccountID;";
|
||||
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
using (var command = new SqlCommand(query, connection))
|
||||
{
|
||||
_ = command.Parameters.AddWithValue("@UserAccountID", id);
|
||||
|
||||
connection.Open();
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return new UserAccount
|
||||
{
|
||||
UserAccountID = reader.GetGuid(
|
||||
reader.GetOrdinal("UserAccountID")
|
||||
),
|
||||
Username = reader.GetString(
|
||||
reader.GetOrdinal("Username")
|
||||
),
|
||||
FirstName = reader.GetString(
|
||||
reader.GetOrdinal("FirstName")
|
||||
),
|
||||
LastName = reader.GetString(
|
||||
reader.GetOrdinal("LastName")
|
||||
),
|
||||
Email = reader.GetString(
|
||||
reader.GetOrdinal("Email")
|
||||
),
|
||||
CreatedAt = reader.GetDateTime(
|
||||
reader.GetOrdinal("CreatedAt")
|
||||
),
|
||||
UpdatedAt = reader.IsDBNull(
|
||||
reader.GetOrdinal("UpdatedAt")
|
||||
)
|
||||
? null
|
||||
: reader.GetDateTime(
|
||||
reader.GetOrdinal("UpdatedAt")
|
||||
),
|
||||
DateOfBirth = reader.GetDateTime(
|
||||
reader.GetOrdinal("DateOfBirth")
|
||||
),
|
||||
Timer = reader.IsDBNull(reader.GetOrdinal("Timer"))
|
||||
? null
|
||||
: (byte[])reader["Timer"],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Update(UserAccount userAccount)
|
||||
{
|
||||
const string query =
|
||||
@"UPDATE UserAccount
|
||||
SET Username = @Username, FirstName = @FirstName, LastName = @LastName, Email = @Email, CreatedAt = @CreatedAt, UpdatedAt = @UpdatedAt, DateOfBirth = @DateOfBirth, Timer = @Timer
|
||||
WHERE UserAccountID = @UserAccountID;";
|
||||
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
using (var command = new SqlCommand(query, connection))
|
||||
{
|
||||
_ = 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(
|
||||
"@CreatedAt",
|
||||
userAccount.CreatedAt
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@UpdatedAt",
|
||||
userAccount.UpdatedAt ?? (object)DBNull.Value
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@DateOfBirth",
|
||||
userAccount.DateOfBirth
|
||||
);
|
||||
_ = command.Parameters.AddWithValue(
|
||||
"@Timer",
|
||||
userAccount.Timer ?? (object)DBNull.Value
|
||||
);
|
||||
|
||||
connection.Open();
|
||||
_ = command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(Guid id)
|
||||
{
|
||||
const string query =
|
||||
"DELETE FROM UserAccount WHERE UserAccountID = @UserAccountID;";
|
||||
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
using (var command = new SqlCommand(query, connection))
|
||||
{
|
||||
_ = command.Parameters.AddWithValue("@UserAccountID", id);
|
||||
|
||||
connection.Open();
|
||||
_ = command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<UserAccount> GetAll()
|
||||
{
|
||||
const string query = "SELECT * FROM UserAccount;";
|
||||
|
||||
var userAccounts = new List<UserAccount>();
|
||||
|
||||
using (var connection = new SqlConnection(_connectionString))
|
||||
using (var command = new SqlCommand(query, connection))
|
||||
{
|
||||
connection.Open();
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var userAccount = new UserAccount
|
||||
{
|
||||
UserAccountID = reader.GetGuid(
|
||||
reader.GetOrdinal("UserAccountID")
|
||||
),
|
||||
Username = reader.GetString(
|
||||
reader.GetOrdinal("Username")
|
||||
),
|
||||
FirstName = reader.GetString(
|
||||
reader.GetOrdinal("FirstName")
|
||||
),
|
||||
LastName = reader.GetString(
|
||||
reader.GetOrdinal("LastName")
|
||||
),
|
||||
Email = reader.GetString(
|
||||
reader.GetOrdinal("Email")
|
||||
),
|
||||
CreatedAt = reader.GetDateTime(
|
||||
reader.GetOrdinal("CreatedAt")
|
||||
),
|
||||
UpdatedAt = reader.IsDBNull(
|
||||
reader.GetOrdinal("UpdatedAt")
|
||||
)
|
||||
? null
|
||||
: reader.GetDateTime(
|
||||
reader.GetOrdinal("UpdatedAt")
|
||||
),
|
||||
DateOfBirth = reader.GetDateTime(
|
||||
reader.GetOrdinal("DateOfBirth")
|
||||
),
|
||||
Timer = reader.IsDBNull(reader.GetOrdinal("Timer"))
|
||||
? null
|
||||
: (byte[])reader["Timer"],
|
||||
};
|
||||
|
||||
userAccounts.Add(userAccount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return userAccounts;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserAccount
|
||||
{
|
||||
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;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
public DateTime DateOfBirth { get; set; }
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserCredential
|
||||
{
|
||||
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;
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace DataAccessLayer.Entities;
|
||||
|
||||
public class UserVerification
|
||||
{
|
||||
public Guid UserVerificationID { get; set; }
|
||||
public Guid UserAccountID { get; set; }
|
||||
public DateTime VerificationDateTime { get; set; }
|
||||
public byte[]? Timer { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user