Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions EBanking.DataAccess/DatabaseAccess.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
using System.Configuration;

namespace EBanking.DataAccess
{
public class DatabaseAccess
public static class DatabaseAccess
{
private const string _connectionString = @"Data Source = .;Initial Catalog=eBanking;Integrated Security=True";
private const string ConnectionStringName = "EBankingDb";

private static readonly Lazy<string> _connectionString = new(() =>
{
var configString = ConfigurationManager.ConnectionStrings[ConnectionStringName]?.ConnectionString;
if (string.IsNullOrWhiteSpace(configString))
{
throw new InvalidOperationException(
$"Connection string '{ConnectionStringName}' was not found in configuration. " +
"Ensure App.config defines <connectionStrings><add name=\"" + ConnectionStringName + "\" ... /></connectionStrings>.");
}
return configString;
});

public static string ConnectionString => _connectionString.Value;
}
}
2 changes: 2 additions & 0 deletions EBanking.DataAccess/EBanking.DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion EBanking.DataAccess/Models/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class Account
{
public string AccountNumber { get; set; }
public int UserId { get; set; }
public double Balance { get; set; }
public decimal Balance { get; set; }
public string Type { get; set; }
public string Currency { get; set; }
public DateTime DateCreated { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion EBanking.DataAccess/Models/CurrencyExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace EBanking.DataAccess.Models
public class CurrencyExchange
{
public string Currency { get; set; }
public double Value { get; set; }
public decimal Value { get; set; }
}
}
4 changes: 2 additions & 2 deletions EBanking.DataAccess/Models/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class Transaction
public int TransactionId { get; set; }
public string AccountNumber { get; set; }
public string CardNumber { get; set; }
public double Amount { get; set; }
public double BalanceAfterTransaction { get; set; }
public decimal Amount { get; set; }
public decimal BalanceAfterTransaction { get; set; }
public DateTime Date { get; set; }
public string SecondaryPartyName { get; set; }
public string SecondaryPartyAccountNumber { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions EBanking.DataAccess/Repositories/IAccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public interface IAccountRepository
{
void CreateAccount(Account model);
List<Account> GetAccountsByUserId(int userId);
Task<Account> GetAccountByAccountNumber(string accountNumber);
void UpdateBalance(double balance, string accountNumber);
Account GetAccountByAccountNumber(string accountNumber);
void UpdateBalance(decimal balance, string accountNumber);
bool IsValidAccount(string accountNumber);
}
}
14 changes: 14 additions & 0 deletions EBanking.DataAccess/Repositories/ITransactionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,19 @@ public interface ITransactionRepository
{
int CreateTransaction(Transaction transaction);
List<Transaction> GetTransactionsByAccountNumber(string accountNumber);
void ExecuteTransfer(
string payerAccountNumber,
string recipientAccountNumber,
decimal amount,
string recipientName,
string payerFullName,
DateTime occurredAt);
void ExecuteExchange(
string sourceAccountNumber,
string destinationAccountNumber,
decimal sourceAmount,
decimal destinationAmount,
string userFullName,
DateTime occurredAt);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using EBanking.DataAccess.Models;
using System.Data;
using System.Data.SqlClient;

namespace EBanking.DataAccess.Repositories.Implementation
{
public class AccountRepository : IAccountRepository
{
private const string _connectionString = @"Data Source = .\SQLEXPRESS;Initial Catalog=EBankingSystem;Integrated Security=True";

public void CreateAccount(Account model)
{
using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

Expand All @@ -19,21 +18,21 @@ public void CreateAccount(Account model)
"VALUES(@accountNumber, @userId, @balance, @currency, @type, @dateCreated)";
sqlCommand.Parameters.AddWithValue("@accountNumber", model.AccountNumber);
sqlCommand.Parameters.AddWithValue("@userId", model.UserId);
sqlCommand.Parameters.AddWithValue("@balance", model.Balance);
sqlCommand.Parameters.Add(new SqlParameter("@balance", SqlDbType.Decimal) { Precision = 18, Scale = 2, Value = model.Balance });
sqlCommand.Parameters.AddWithValue("@currency", model.Currency);
sqlCommand.Parameters.AddWithValue("@type", model.Type);
sqlCommand.Parameters.AddWithValue("@dateCreated", model.DateCreated);

sqlCommand.ExecuteScalar();
sqlCommand.ExecuteNonQuery();
}
}
}

public async Task<Account> GetAccountByAccountNumber(string accountNumber)
public Account GetAccountByAccountNumber(string accountNumber)
{
Account account = new Account();

using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

Expand All @@ -48,7 +47,7 @@ public async Task<Account> GetAccountByAccountNumber(string accountNumber)
{
account.UserId = (int)reader["userId"];
account.AccountNumber = reader["accountNumber"] as string;
account.Balance = decimal.ToDouble((decimal)reader["balance"]);
account.Balance = (decimal)reader["balance"];
account.DateCreated = (DateTime)reader["dateCreated"];
account.Type = reader["type"] as string;
account.Currency = reader["currency"] as string;
Expand All @@ -64,7 +63,7 @@ public List<Account> GetAccountsByUserId(int userId)
{
List<Account> accounts = new List<Account>();

using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

Expand All @@ -81,7 +80,7 @@ public List<Account> GetAccountsByUserId(int userId)
{
UserId = (int)reader["userId"],
AccountNumber = reader["accountNumber"] as string,
Balance = decimal.ToDouble((decimal)reader["balance"]),
Balance = (decimal)reader["balance"],
DateCreated = (DateTime)reader["dateCreated"],
Type = reader["type"] as string,
Currency = reader["currency"] as string,
Expand All @@ -103,37 +102,33 @@ public List<Account> GetAccountsByUserId(int userId)

public bool IsValidAccount(string accountNumber)
{
object accountId;

using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = "SELECT * FROM Account WHERE accountNumber = @accountNumber";
sqlCommand.CommandText = "SELECT 1 FROM Account WHERE accountNumber = @accountNumber";
sqlCommand.Parameters.AddWithValue("@accountNumber", accountNumber);

accountId = sqlCommand.ExecuteScalar();
return sqlCommand.ExecuteScalar() is not null;
}
}

return accountId as string is not null;
}

public void UpdateBalance(double balance, string accountNumber)
public void UpdateBalance(decimal balance, string accountNumber)
{
using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = "UPDATE Account SET balance = @balance WHERE accountNumber = @accountNumber";
sqlCommand.Parameters.AddWithValue("@balance", balance);
sqlCommand.Parameters.Add(new SqlParameter("@balance", SqlDbType.Decimal) { Precision = 18, Scale = 2, Value = balance });
sqlCommand.Parameters.AddWithValue("@accountNumber", accountNumber);

sqlCommand.ExecuteScalar();
sqlCommand.ExecuteNonQuery();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ namespace EBanking.DataAccess.Repositories.Implementation
{
public class CurrencyExchangeRepository : ICurrencyExchangeRepository
{
private const string _connectionString = @"Data Source = .\SQLEXPRESS;Initial Catalog=EBankingSystem;Integrated Security=True";

public List<CurrencyExchange> GetExchangeRatesByCurrency(string currency)
{
List<CurrencyExchange> exchangeRates = new List<CurrencyExchange>();

using (SqlConnection sqlConnection = new SqlConnection(_connectionString))
using (SqlConnection sqlConnection = new SqlConnection(DatabaseAccess.ConnectionString))
{
sqlConnection.Open();

Expand All @@ -26,7 +24,7 @@ public List<CurrencyExchange> GetExchangeRatesByCurrency(string currency)
exchangeRates.Add(new CurrencyExchange
{
Currency = reader["currency"] as string,
Value = decimal.ToDouble((decimal)reader["value"]),
Value = (decimal)reader["value"],
});
}
}
Expand Down
Loading