Skip to content

Commit 9402a82

Browse files
committed
LP5 M3 updating Bank.cs
1 parent 7e2df56 commit 9402a82

3 files changed

Lines changed: 90 additions & 103 deletions

File tree

DownloadableCodeProjects/LP5_file-io-json-async/Files_M3/Solution/Models/Bank.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ internal IEnumerable<BankCustomer> GetCustomersByName(string firstName, string l
4343
// get customer based on Customer ID
4444
internal BankCustomer? GetCustomerById(string customerId)
4545
{
46-
foreach (var customer in _customers)
47-
{
48-
if (customer.CustomerId.Equals(customerId, StringComparison.OrdinalIgnoreCase))
49-
{
50-
return customer;
51-
}
52-
}
53-
return null;
46+
return _customers.FirstOrDefault(customer => customer.CustomerId.Equals(customerId, StringComparison.OrdinalIgnoreCase));
47+
5448
}
5549

5650
internal int GetNumberOfTransactions()

DownloadableCodeProjects/LP5_file-io-json-async/Files_M3/Solution/Services/JsonRetrievalAsync.cs

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,127 @@
44
using System.Text.Json;
55
using System.Threading.Tasks;
66

7-
namespace Files_M3
7+
namespace Files_M3;
8+
9+
public static class JsonRetrievalAsync
810
{
9-
public static class JsonRetrievalAsync
11+
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions
1012
{
11-
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions
12-
{
13-
MaxDepth = 64,
14-
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
15-
};
13+
MaxDepth = 64,
14+
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
15+
};
16+
17+
public static async Task<BankCustomerDTO> LoadBankCustomerDTOAsync(string filePath)
18+
{
19+
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
20+
var customerDTO = await JsonSerializer.DeserializeAsync<BankCustomerDTO>(stream, _options);
1621

17-
public static async Task<BankCustomerDTO> LoadBankCustomerDTOAsync(string filePath)
22+
if (customerDTO == null)
1823
{
19-
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
20-
var customerDTO = await JsonSerializer.DeserializeAsync<BankCustomerDTO>(stream, _options);
24+
throw new Exception("Customer could not be deserialized.");
25+
}
2126

22-
if (customerDTO == null)
23-
{
24-
throw new Exception("Customer could not be deserialized.");
25-
}
27+
return customerDTO;
28+
}
2629

27-
return customerDTO;
28-
}
30+
public static async Task<BankCustomer> LoadBankCustomerAsync(Bank bank, string filePath, string accountsDirectoryPath, string transactionsDirectoryPath)
31+
{
32+
var customerDTO = await LoadBankCustomerDTOAsync(filePath);
2933

30-
public static async Task<BankCustomer> LoadBankCustomerAsync(Bank bank, string filePath, string accountsDirectoryPath, string transactionsDirectoryPath)
31-
{
32-
var customerDTO = await LoadBankCustomerDTOAsync(filePath);
34+
var bankCustomer = bank.GetCustomerById(customerDTO.CustomerId);
3335

34-
var bankCustomer = bank.GetCustomerById(customerDTO.CustomerId);
36+
if (bankCustomer == null)
37+
{
38+
bankCustomer = new BankCustomer(customerDTO.FirstName, customerDTO.LastName, customerDTO.CustomerId, bank);
39+
bank.AddCustomer(bankCustomer);
40+
}
3541

36-
if (bankCustomer == null)
37-
{
38-
bankCustomer = new BankCustomer(customerDTO.FirstName, customerDTO.LastName, customerDTO.CustomerId, bank);
39-
bank.AddCustomer(bankCustomer);
40-
}
42+
foreach (var accountNumber in customerDTO.AccountNumbers)
43+
{
44+
var existingAccount = bankCustomer.Accounts.FirstOrDefault(a => a.AccountNumber == accountNumber);
4145

42-
foreach (var accountNumber in customerDTO.AccountNumbers)
46+
if (existingAccount == null)
4347
{
44-
var existingAccount = bankCustomer.Accounts.FirstOrDefault(a => a.AccountNumber == accountNumber);
48+
var accountFilePath = Path.Combine(accountsDirectoryPath, $"{accountNumber}.json");
49+
var recoveredAccount = await LoadBankAccountAsync(accountFilePath, transactionsDirectoryPath, bankCustomer);
4550

46-
if (existingAccount == null)
51+
if (recoveredAccount != null)
4752
{
48-
var accountFilePath = Path.Combine(accountsDirectoryPath, $"{accountNumber}.json");
49-
var recoveredAccount = await LoadBankAccountAsync(accountFilePath, transactionsDirectoryPath, bankCustomer);
50-
51-
if (recoveredAccount != null)
52-
{
53-
bankCustomer.AddAccount(recoveredAccount);
54-
}
55-
}
56-
else
57-
{
58-
bankCustomer.AddAccount(existingAccount);
53+
bankCustomer.AddAccount(recoveredAccount);
5954
}
6055
}
61-
62-
return bankCustomer;
63-
}
64-
65-
public static async Task<IEnumerable<BankCustomer>> LoadAllCustomersAsync(Bank bank, string directoryPath, string accountsDirectoryPath, string transactionsDirectoryPath)
66-
{
67-
List<BankCustomer> customers = new List<BankCustomer>();
68-
foreach (var filePath in Directory.GetFiles(Path.Combine(directoryPath, "Customers"), "*.json"))
56+
else
6957
{
70-
customers.Add(await LoadBankCustomerAsync(bank, filePath, accountsDirectoryPath, transactionsDirectoryPath));
58+
bankCustomer.AddAccount(existingAccount);
7159
}
72-
return customers;
7360
}
7461

75-
public static async Task<BankAccount> LoadBankAccountAsync(string accountFilePath, string transactionsDirectoryPath, BankCustomer customer)
76-
{
77-
var accountDTO = await LoadBankAccountDTOAsync(accountFilePath);
78-
79-
var existingAccount = customer.Accounts.FirstOrDefault(a => a.AccountNumber == accountDTO.AccountNumber);
62+
return bankCustomer;
63+
}
8064

81-
if (existingAccount != null)
82-
{
83-
return (BankAccount)existingAccount;
84-
}
85-
else
86-
{
87-
var recoveredBankAccount = new BankAccount(customer, customer.CustomerId, accountDTO.Balance, accountDTO.AccountType);
65+
public static async Task<IEnumerable<BankCustomer>> LoadAllCustomersAsync(Bank bank, string directoryPath, string accountsDirectoryPath, string transactionsDirectoryPath)
66+
{
67+
List<BankCustomer> customers = new List<BankCustomer>();
68+
foreach (var filePath in Directory.GetFiles(Path.Combine(directoryPath, "Customers"), "*.json"))
69+
{
70+
customers.Add(await LoadBankCustomerAsync(bank, filePath, accountsDirectoryPath, transactionsDirectoryPath));
71+
}
72+
return customers;
73+
}
8874

89-
string transactionsFilePath = Path.Combine(transactionsDirectoryPath, $"{accountDTO.AccountNumber}-transactions.json");
75+
public static async Task<BankAccount> LoadBankAccountAsync(string accountFilePath, string transactionsDirectoryPath, BankCustomer customer)
76+
{
77+
var accountDTO = await LoadBankAccountDTOAsync(accountFilePath);
9078

91-
if (File.Exists(transactionsFilePath))
92-
{
93-
var recoveredTransactions = await LoadAllTransactionsAsync(transactionsFilePath);
94-
foreach (var transaction in recoveredTransactions)
95-
{
96-
recoveredBankAccount.AddTransaction(transaction);
97-
}
98-
}
79+
var existingAccount = customer.Accounts.FirstOrDefault(a => a.AccountNumber == accountDTO.AccountNumber);
9980

100-
return recoveredBankAccount;
101-
}
81+
if (existingAccount != null)
82+
{
83+
return (BankAccount)existingAccount;
10284
}
103-
104-
public static async Task<IEnumerable<Transaction>> LoadAllTransactionsAsync(string filePath)
85+
else
10586
{
106-
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
107-
var transactions = await JsonSerializer.DeserializeAsync<IEnumerable<Transaction>>(stream, _options);
87+
var recoveredBankAccount = new BankAccount(customer, customer.CustomerId, accountDTO.Balance, accountDTO.AccountType);
88+
89+
string transactionsFilePath = Path.Combine(transactionsDirectoryPath, $"{accountDTO.AccountNumber}-transactions.json");
10890

109-
if (transactions == null)
91+
if (File.Exists(transactionsFilePath))
11092
{
111-
throw new Exception("Transactions could not be deserialized.");
93+
var recoveredTransactions = await LoadAllTransactionsAsync(transactionsFilePath);
94+
foreach (var transaction in recoveredTransactions)
95+
{
96+
recoveredBankAccount.AddTransaction(transaction);
97+
}
11298
}
11399

114-
return transactions;
100+
return recoveredBankAccount;
115101
}
102+
}
116103

117-
public static async Task<BankAccountDTO> LoadBankAccountDTOAsync(string filePath)
104+
public static async Task<IEnumerable<Transaction>> LoadAllTransactionsAsync(string filePath)
105+
{
106+
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
107+
var transactions = await JsonSerializer.DeserializeAsync<IEnumerable<Transaction>>(stream, _options);
108+
109+
if (transactions == null)
118110
{
119-
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
120-
var accountDTO = await JsonSerializer.DeserializeAsync<BankAccountDTO>(stream, _options);
111+
throw new Exception("Transactions could not be deserialized.");
112+
}
121113

122-
if (accountDTO == null)
123-
{
124-
throw new Exception("Account could not be deserialized.");
125-
}
114+
return transactions;
115+
}
116+
117+
public static async Task<BankAccountDTO> LoadBankAccountDTOAsync(string filePath)
118+
{
119+
using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous);
120+
var accountDTO = await JsonSerializer.DeserializeAsync<BankAccountDTO>(stream, _options);
126121

127-
return accountDTO;
122+
if (accountDTO == null)
123+
{
124+
throw new Exception("Account could not be deserialized.");
128125
}
126+
127+
return accountDTO;
129128
}
130129
}
130+

DownloadableCodeProjects/LP5_file-io-json-async/Files_M3/Starter/Models/Bank.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ internal IEnumerable<BankCustomer> GetCustomersByName(string firstName, string l
4343
// get customer based on Customer ID
4444
internal BankCustomer? GetCustomerById(string customerId)
4545
{
46-
foreach (var customer in _customers)
47-
{
48-
if (customer.CustomerId.Equals(customerId, StringComparison.OrdinalIgnoreCase))
49-
{
50-
return customer;
51-
}
52-
}
53-
return null;
46+
return _customers.FirstOrDefault(customer => customer.CustomerId.Equals(customerId, StringComparison.OrdinalIgnoreCase));
5447
}
5548

5649
internal int GetNumberOfTransactions()

0 commit comments

Comments
 (0)