Skip to content

Commit cf86818

Browse files
committed
Save financial data to the db.
1 parent 860a85e commit cf86818

19 files changed

Lines changed: 735 additions & 17 deletions

CashFlowAnalyzer.Client/FinancialData/Category/Categories.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public static List<Category> GetAllCategories()
1717
new Category(CategoryType.Sport, SharingMode.RequiresReview),
1818
new Category(CategoryType.Transport, SharingMode.RequiresReview),
1919
new Category(CategoryType.RequiresReview, SharingMode.RequiresReview),
20-
new Category(CategoryType.Gifts, SharingMode.RequiresReview)
20+
new Category(CategoryType.Gifts, SharingMode.RequiresReview),
21+
new Category(CategoryType.Home, SharingMode.Shared)
2122
};
2223
}
2324
}

CashFlowAnalyzer.Client/FinancialData/Category/CategoryType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public enum CategoryType
6464
[Description("Gifts")]
6565
Gifts,
6666

67+
/// <summary>
68+
/// tools, furniture, cleaning utensils etc
69+
/// </summary>
70+
[Description("Home")]
71+
Home,
72+
6773
/// <summary>
6874
/// requires manual categorizing
6975
/// </summary>

CashFlowAnalyzer.Client/FinancialData/Spreadsheet/FinancialRecordMapper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public List<FinancialRecord> Map(List<CeskaSporitelnaRecord> spreadsheetRecords)
3636
List<FinancialRecord> financialRecords = new();
3737
foreach(var record in spreadsheetRecords)
3838
{
39-
financialRecords.Add(Map(record));
39+
var financialRecord = Map(record);
40+
// 0-valued fees and incomes are not interesting
41+
if (financialRecord.Value != 0)
42+
{
43+
financialRecords.Add(financialRecord);
44+
}
4045
}
4146
return financialRecords;
4247
}

CashFlowAnalyzer.Client/Pages/Security/Login.razor

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
@page "/Login"
2-
@* custom loginForm paths issue: *@
3-
@* https://github.com/dotnet/aspnetcore/issues/58811 *@
42
@using CashFlowAnalyzer.Client.Services
53
@inject NavigationManager Navigation
64
@inject HttpClient Http

CashFlowAnalyzer.Client/Pages/Upload.razor

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
@using CashFlowAnalyzer.Client.FinancialData
33
@attribute [Authorize]
44
@inject SpreadsheetReader SpreadsheetReader
5+
@inject IFinancialDataService FinancialDataService
56

67
<PageTitle>Upload</PageTitle>
78

@@ -29,11 +30,11 @@
2930
</select>
3031
</div>
3132

32-
<InputFile OnChange="HandleFileSelected" class="form-control" disabled="@((selectedBank is null) && (defaultCurrency is null))" />
33+
<InputFile OnChange="HandleFileSelected" key="@(fileInputKey)" class="form-control" disabled="@((selectedBank is null) && (defaultCurrency is null))" />
3334

3435
@if (financialRecords == null || financialRecords.Count == 0)
3536
{
36-
<p><em>Loading...</em></p>
37+
<p><em>Waiting for the file...</em></p>
3738
}
3839
else
3940
{
@@ -94,9 +95,27 @@ else
9495
<button @onclick="TogglePreviewMode" class="btn btn-primary" disabled="@HasRequiresReview()">Preview</button>
9596
}
9697
}
98+
@if (showSuccessMessage)
99+
{
100+
<div class="alert alert-success" role="alert">
101+
Records upload was successful.
102+
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseSuccessMessage"> </button>
103+
</div>
104+
}
105+
@if (showErrorMessage)
106+
{
107+
<div class="alert alert-danger" role="alert">
108+
Records upload failed.
109+
<button type="button" class="btn-close" aria-label="Close" @onclick="CloseErrorMessage"> </button>
110+
</div>
111+
}
97112

98113
@code {
99114
private bool isPreviewMode = false;
115+
private bool isFileLoaded = false;
116+
private bool showSuccessMessage = false;
117+
private bool showErrorMessage = false;
118+
private string fileInputKey = Guid.NewGuid().ToString();
100119
private string payer = "me"; // ToDo: ThisShouldBeManualInputFromEditableDropdown
101120
private ICurrency defaultCurrency = Currencies.GetDefaultCurrency();
102121
private FinancialRecordMapper mapper;
@@ -185,7 +204,40 @@ else
185204

186205
private async Task SaveData()
187206
{
188-
// ToDo: Implement the logic to save the data to the database
189-
Console.WriteLine("Saving data to the database...");
207+
try
208+
{
209+
await FinancialDataService.SaveFinancialRecordsAsync(financialRecords);
210+
showSuccessMessage = true;
211+
CleanUploadView();
212+
}
213+
catch (Exception ex)
214+
{
215+
Console.WriteLine($"Error saving data: {ex.Message}");
216+
showErrorMessage = true;
217+
}
218+
}
219+
220+
private void CloseSuccessMessage()
221+
{
222+
showSuccessMessage = false;
223+
}
224+
225+
private void CloseErrorMessage()
226+
{
227+
showErrorMessage = false;
228+
}
229+
230+
private void CleanUploadView()
231+
{
232+
// issue: resetting InputFile field does not work neatly:
233+
// https://github.com/SteveSandersonMS/BlazorInputFile/issues/2
234+
fileInputKey = Guid.NewGuid().ToString();
235+
financialRecords = new List<FinancialRecord>();
236+
spreadsheetFinancialRecords = new List<FinancialRecord>();
237+
selectedBank = null;
238+
defaultCurrency = null;
239+
isFileLoaded = false;
240+
TogglePreviewMode();
241+
StateHasChanged();
190242
}
191243
}

CashFlowAnalyzer.Client/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// Services
1616
builder.Services.AddScoped<IAccountService, AccountService>();
1717
builder.Services.AddScoped<SpreadsheetReader>();
18+
builder.Services.AddScoped<IFinancialDataService, FinancialDataService>();
1819

1920
// Supply HttpClient instances that include access tokens when making requests to the server project
2021
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("CashFlowAnalyzer.ServerAPI"));

CashFlowAnalyzer.Client/Services/Account/AccountServiceResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
namespace CashFlowAnalyzer.Client.Services;
2+
13
public class AccountServiceResult
24
{
35
public bool Success { get; set; }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Net.Http.Json;
2+
using CashFlowAnalyzer.Client.FinancialData;
3+
using CashFlowAnalyzer.Shared.Models;
4+
5+
namespace CashFlowAnalyzer.Client.Services;
6+
public class FinancialDataService : IFinancialDataService
7+
{
8+
// same workaround as in AccountService
9+
private string baseAddress = "http://localhost:5130";
10+
private readonly HttpClient _httpClient;
11+
12+
public FinancialDataService(HttpClient httpClient)
13+
{
14+
_httpClient = httpClient;
15+
}
16+
17+
public async Task SaveFinancialRecordsAsync(IEnumerable<FinancialRecord> records)
18+
{
19+
List<FinancialRecordDto> recordDtos = new();
20+
foreach (var record in records)
21+
{
22+
var recordDto = new FinancialRecordDto()
23+
{
24+
ProcessingDate = record.ProcessingDate,
25+
Value = record.Value,
26+
TransactionCurrency = record.TransactionCurrency.ToString(),
27+
Category = record.Category.ToString(),
28+
Bank = record.Bank.ToFriendlyString()
29+
};
30+
recordDtos.Add(recordDto);
31+
}
32+
var response = await _httpClient.PostAsJsonAsync($"{baseAddress}/api/financialrecords", recordDtos);
33+
response.EnsureSuccessStatusCode();
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using CashFlowAnalyzer.Client.FinancialData;
2+
3+
namespace CashFlowAnalyzer.Client.Services;
4+
5+
public interface IFinancialDataService
6+
{
7+
Task SaveFinancialRecordsAsync(IEnumerable<FinancialRecord> records);
8+
}

CashFlowAnalyzer.Client/Services/SpreadsheetReader/SpreadsheetReader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using CashFlowAnalyzer.Client.FinancialData;
33
using OfficeOpenXml;
44

5+
namespace CashFlowAnalyzer.Client.Services;
6+
57
public class SpreadsheetReader
68
{
79
public SpreadsheetReader()

0 commit comments

Comments
 (0)