Skip to content

Commit 03f21d3

Browse files
committed
(#69) Import & Export Csv, Excel Files
1 parent b1e15d7 commit 03f21d3

File tree

6 files changed

+71
-12
lines changed

6 files changed

+71
-12
lines changed

src/Monolith/ClassifiedAds.Blazor.Modules/Products/Pages/List.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Components;
66
using Microsoft.AspNetCore.Components.Authorization;
77
using Microsoft.Extensions.Logging;
8+
using Microsoft.JSInterop;
89
using System.Collections.Generic;
910
using System.Threading.Tasks;
1011

@@ -21,6 +22,9 @@ public partial class List
2122
[Inject]
2223
public NavigationManager NavManager { get; set; }
2324

25+
[Inject]
26+
public IJSRuntime JSRuntime { get; set; }
27+
2428
[Inject]
2529
public ILogger<List> Logger { get; set; }
2630

@@ -67,5 +71,19 @@ public async void ConfirmedDeleteProduct()
6771
Products = await ProductService.GetProductsAsync();
6872
StateHasChanged();
6973
}
74+
75+
protected async Task ExportAsPdf()
76+
{
77+
var token = await ProductService.GetAccessToken();
78+
await JSRuntime.Log(token);
79+
await JSRuntime.InvokeVoidAsync("interop.downloadFile", ProductService.GetExportPdfUrl(), token, "Products.pdf");
80+
}
81+
82+
protected async Task ExportAsCsv()
83+
{
84+
var token = await ProductService.GetAccessToken();
85+
await JSRuntime.Log(token);
86+
await JSRuntime.InvokeVoidAsync("interop.downloadFile", ProductService.GetExportCsvUrl(), token, "Products.csv");
87+
}
7088
}
7189
}

src/Monolith/ClassifiedAds.Blazor.Modules/Products/Pages/List.razor

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55
<div class="card">
66
<div class="card-header">
77
Product List
8-
<button class="btn btn-primary"
9-
style="float: right;"
10-
@onclick="QuickAddProduct">
11-
Add Product
12-
</button>
8+
9+
<div style="float: right">
10+
<button type="button" class="btn btn-secondary" @onclick="ExportAsPdf">
11+
Export as Pdf
12+
</button>
13+
&nbsp;
14+
<button type="button" class="btn btn-secondary" @onclick="ExportAsCsv">
15+
Export as Csv
16+
</button>
17+
&nbsp;
18+
<button class="btn btn-primary"
19+
style="float: right;"
20+
@onclick="QuickAddProduct">
21+
Add Product
22+
</button>
23+
</div>
1324
</div>
1425
<div class="card-body">
1526
<div class="table-responsive">

src/Monolith/ClassifiedAds.Blazor.Modules/Products/Services/ProductService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,15 @@ public async Task<List<ProductAuditLogModel>> GetAuditLogsAsync(Guid id)
4848
var auditLogs = await GetAsync<List<ProductAuditLogModel>>($"api/products/{id}/auditlogs");
4949
return auditLogs;
5050
}
51+
52+
public string GetExportPdfUrl()
53+
{
54+
return $"{_httpClient.BaseAddress.AbsoluteUri.Trim('/')}/api/products/ExportAsPdf";
55+
}
56+
57+
public string GetExportCsvUrl()
58+
{
59+
return $"{_httpClient.BaseAddress.AbsoluteUri.Trim('/')}/api/products/ExportAsCsv";
60+
}
5161
}
5262
}

src/Monolith/ClassifiedAds.Blazor.Modules/Settings/Pages/List.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
using ClassifiedAds.Blazor.Modules.Settings.Services;
55
using Microsoft.AspNetCore.Components;
66
using Microsoft.Extensions.Logging;
7+
using Microsoft.JSInterop;
78
using System;
89
using System.Collections.Generic;
9-
using System.Linq;
10-
using System.Text;
1110
using System.Threading.Tasks;
1211

1312
namespace ClassifiedAds.Blazor.Modules.Settings.Pages
1413
{
1514
public partial class List
1615
{
16+
[Inject]
17+
public IJSRuntime JSRuntime { get; set; }
18+
1719
[Inject]
1820
public ILogger<List> Logger { get; set; }
1921

@@ -78,5 +80,12 @@ public async void ConfirmedAddEdit(ConfigurationEntryModel model)
7880
ConfigurationEntries = await ConfigurationEntryService.GetListAsync();
7981
StateHasChanged();
8082
}
83+
84+
protected async Task ExportAsExcel()
85+
{
86+
var token = await ConfigurationEntryService.GetAccessToken();
87+
await JSRuntime.Log(token);
88+
await JSRuntime.InvokeVoidAsync("interop.downloadFile", ConfigurationEntryService.GetExportExcelUrl(), token, "Settings.xlsx");
89+
}
8190
}
8291
}

src/Monolith/ClassifiedAds.Blazor.Modules/Settings/Pages/List.razor

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
<div class="card">
77
<div class="card-header">
88
Settings
9-
<button class="btn btn-primary"
10-
style="float: right;"
11-
@onclick="@(()=> AddSetting())">
12-
Add
13-
</button>
9+
<div style="float: right">
10+
<button type="button" class="btn btn-secondary" @onclick="ExportAsExcel">
11+
Export as Excel
12+
</button>
13+
&nbsp;
14+
<button class="btn btn-primary"
15+
style="float: right;"
16+
@onclick="@(()=> AddSetting())">
17+
Add
18+
</button>
19+
</div>
1420
</div>
1521
<div class="card-body">
1622
<div class="table-responsive">

src/Monolith/ClassifiedAds.Blazor.Modules/Settings/Services/ConfigurationEntryService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ public async Task<List<FileEntryAuditLogModel>> GetAuditLogsAsync(Guid id)
4949
var auditLogs = await GetAsync<List<FileEntryAuditLogModel>>($"api/ConfigurationEntries/{id}/AuditLogs");
5050
return auditLogs;
5151
}
52+
53+
public string GetExportExcelUrl()
54+
{
55+
return $"{_httpClient.BaseAddress.AbsoluteUri.Trim('/')}/api/ConfigurationEntries/ExportAsExcel";
56+
}
5257
}
5358
}

0 commit comments

Comments
 (0)