Skip to content

Commit

Permalink
Refactor InvoiceManager to use constructor injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
denys-olleik committed Dec 10, 2024
1 parent 6fb2f4b commit 5ea11ae
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions Accounting.Database/DapperPsqlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,17 +1673,24 @@ public async Task<int> UpdateTitleAndDescription(List<InvoiceLine> invoiceLines,

public IInvoiceManager GetInvoiceManager()
{
return new InvoiceManager();
return new InvoiceManager(_connectionString);
}

public class InvoiceManager : IInvoiceManager
{
private readonly string _connectionString;

public InvoiceManager(string connectionString)
{
_connectionString = connectionString;
}

public async Task<string> CalculateInvoiceStatusAsync(int invoiceId, int organizationId)
{
decimal invoiceTotal = 0;
decimal receivedAmount = 0;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
DynamicParameters p = new DynamicParameters();
p.Add("@InvoiceId", invoiceId);
Expand Down Expand Up @@ -1736,7 +1743,7 @@ public async Task<int> ComputeAndUpdateTotalAmountAndReceivedAmount(int invoiceI
{
int affectedRows = 0;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
string totalAmountQuery = """
SELECT SUM("Quantity" * "Price")
Expand Down Expand Up @@ -1811,7 +1818,7 @@ public async Task<Invoice> CreateAsync(Invoice entity)

IEnumerable<Invoice> result;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.QueryAsync<Invoice>("""
INSERT INTO "Invoice" (
Expand Down Expand Up @@ -1860,7 +1867,7 @@ public async Task<bool> ExistsAsync(int invoiceId, int organizationId)

bool result;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.ExecuteScalarAsync<bool>("""
SELECT COUNT(*) > 0
Expand Down Expand Up @@ -1900,7 +1907,7 @@ public IEnumerable<Invoice> GetAll()

IEnumerable<Invoice> paginatedResult;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
paginatedResult = await con.QueryAsync<Invoice>($"""
SELECT * FROM (
Expand Down Expand Up @@ -1935,7 +1942,7 @@ public async Task<List<Invoice>> GetAllAsync(int organizationId, string[] inStat

IEnumerable<Invoice> result;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.QueryAsync<Invoice>("""
SELECT * FROM "Invoice"
Expand All @@ -1955,7 +1962,7 @@ public async Task<Invoice> GetAsync(int id, int organizationId)

IEnumerable<Invoice> result;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.QueryAsync<Invoice>("""
SELECT *
Expand All @@ -1973,7 +1980,7 @@ public async Task<List<Invoice>> GetAsync(string invoiceIdsCsv, int organization
IEnumerable<Invoice> result;
var invoiceIds = invoiceIdsCsv.Split(',').Select(id => int.Parse(id.Trim())).ToArray();

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.QueryAsync<Invoice>("""
SELECT * FROM "Invoice"
Expand All @@ -1993,7 +2000,7 @@ public async Task<DateTime> GetLastUpdatedAsync(int invoiceId, int organizationI

DateTime result;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
result = await con.ExecuteScalarAsync<DateTime>("""
SELECT "LastUpdated"
Expand All @@ -2013,7 +2020,7 @@ public async Task<DateTime> GetLastUpdatedAsync(int invoiceId, int organizationI
decimal unpaid = 0.0M;
decimal paid = 0.0M;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
await con.OpenAsync();

Expand Down Expand Up @@ -2047,7 +2054,7 @@ public async Task<bool> IsVoidAsync(int invoiceId, int organizationId)

string? voidReason;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
voidReason = await con.ExecuteScalarAsync<string>("""
SELECT "VoidReason"
Expand All @@ -2072,7 +2079,7 @@ public async Task<List<Invoice>> SearchInvoicesAsync(

var companyParts = company?.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();

using (var con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (var con = new NpgsqlConnection(_connectionString))
{
var sql = """
SELECT i.*
Expand Down Expand Up @@ -2132,7 +2139,7 @@ public async Task<int> UpdateLastUpdated(int invoiceId, int organizationId)

int rowsModified;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
rowsModified = await con.ExecuteAsync("""
UPDATE "Invoice"
Expand All @@ -2154,7 +2161,7 @@ public async Task<int> UpdatePaymentInstructions(int invoiceId, string? paymentI

int rowsModified;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
rowsModified = await con.ExecuteAsync("""
UPDATE "Invoice"
Expand All @@ -2175,7 +2182,7 @@ public async Task<int> UpdateStatusAsync(int invoiceId, string status)

int rowsModified;

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
rowsModified = await con.ExecuteAsync("""
UPDATE "Invoice"
Expand All @@ -2196,7 +2203,7 @@ public async Task<int> VoidAsync(int invoiceId, string? voidReason, int organiza
p.Add("@Status", Invoice.InvoiceStatusConstants.Void);
p.Add("@OrganizationId", organizationId);

using (NpgsqlConnection con = new NpgsqlConnection(ConfigurationSingleton.Instance.ConnectionStringDefaultPsql))
using (NpgsqlConnection con = new NpgsqlConnection(_connectionString))
{
rowsAffected = await con.ExecuteAsync("""
UPDATE "Invoice"
Expand Down

0 comments on commit 5ea11ae

Please sign in to comment.