|
4 | 4 | using System.Text.Json; |
5 | 5 | using System.Threading.Tasks; |
6 | 6 |
|
7 | | -namespace Files_M3 |
| 7 | +namespace Files_M3; |
| 8 | + |
| 9 | +public static class JsonRetrievalAsync |
8 | 10 | { |
9 | | - public static class JsonRetrievalAsync |
| 11 | + private static readonly JsonSerializerOptions _options = new JsonSerializerOptions |
10 | 12 | { |
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); |
16 | 21 |
|
17 | | - public static async Task<BankCustomerDTO> LoadBankCustomerDTOAsync(string filePath) |
| 22 | + if (customerDTO == null) |
18 | 23 | { |
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 | + } |
21 | 26 |
|
22 | | - if (customerDTO == null) |
23 | | - { |
24 | | - throw new Exception("Customer could not be deserialized."); |
25 | | - } |
| 27 | + return customerDTO; |
| 28 | + } |
26 | 29 |
|
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); |
29 | 33 |
|
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); |
33 | 35 |
|
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 | + } |
35 | 41 |
|
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); |
41 | 45 |
|
42 | | - foreach (var accountNumber in customerDTO.AccountNumbers) |
| 46 | + if (existingAccount == null) |
43 | 47 | { |
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); |
45 | 50 |
|
46 | | - if (existingAccount == null) |
| 51 | + if (recoveredAccount != null) |
47 | 52 | { |
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); |
59 | 54 | } |
60 | 55 | } |
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 |
69 | 57 | { |
70 | | - customers.Add(await LoadBankCustomerAsync(bank, filePath, accountsDirectoryPath, transactionsDirectoryPath)); |
| 58 | + bankCustomer.AddAccount(existingAccount); |
71 | 59 | } |
72 | | - return customers; |
73 | 60 | } |
74 | 61 |
|
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 | + } |
80 | 64 |
|
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 | + } |
88 | 74 |
|
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); |
90 | 78 |
|
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); |
99 | 80 |
|
100 | | - return recoveredBankAccount; |
101 | | - } |
| 81 | + if (existingAccount != null) |
| 82 | + { |
| 83 | + return (BankAccount)existingAccount; |
102 | 84 | } |
103 | | - |
104 | | - public static async Task<IEnumerable<Transaction>> LoadAllTransactionsAsync(string filePath) |
| 85 | + else |
105 | 86 | { |
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"); |
108 | 90 |
|
109 | | - if (transactions == null) |
| 91 | + if (File.Exists(transactionsFilePath)) |
110 | 92 | { |
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 | + } |
112 | 98 | } |
113 | 99 |
|
114 | | - return transactions; |
| 100 | + return recoveredBankAccount; |
115 | 101 | } |
| 102 | + } |
116 | 103 |
|
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) |
118 | 110 | { |
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 | + } |
121 | 113 |
|
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); |
126 | 121 |
|
127 | | - return accountDTO; |
| 122 | + if (accountDTO == null) |
| 123 | + { |
| 124 | + throw new Exception("Account could not be deserialized."); |
128 | 125 | } |
| 126 | + |
| 127 | + return accountDTO; |
129 | 128 | } |
130 | 129 | } |
| 130 | + |
0 commit comments