1 | using Dapper;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Npgsql;
|
---|
4 | using System.Data;
|
---|
5 | using System.Text;
|
---|
6 | using WineTrackerWebApi.Models.ErrorHandle;
|
---|
7 | using WineTrackerWebApi.Models.Report;
|
---|
8 | using WineTrackerWebApi.Models.WineType;
|
---|
9 |
|
---|
10 | namespace WineTrackerWebApi.Controllers
|
---|
11 | {
|
---|
12 | [Route("api/[controller]")]
|
---|
13 | [ApiController]
|
---|
14 | public class ReportsController : ControllerBase
|
---|
15 | {
|
---|
16 | private readonly string _connectionString;
|
---|
17 |
|
---|
18 | public ReportsController(IConfiguration configuration)
|
---|
19 | {
|
---|
20 | _connectionString = configuration.GetConnectionString("DefaultConnection");
|
---|
21 | }
|
---|
22 |
|
---|
23 | // GET: api/Reports/GetWineReport
|
---|
24 | [HttpGet("getShippedWinesReport/{month}/{year}")]
|
---|
25 | public async Task<IActionResult> GetShippedWineReport(int month, int year)
|
---|
26 | {
|
---|
27 | try
|
---|
28 | {
|
---|
29 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
30 | {
|
---|
31 | var parameters = new DynamicParameters();
|
---|
32 | parameters.Add("@Month", month, DbType.Int32);
|
---|
33 | parameters.Add("@Year", year, DbType.Int32);
|
---|
34 |
|
---|
35 | const string query = "SELECT * FROM GetShippedWinesReport(@Month, @Year)";
|
---|
36 | var shippedWineReport = await db.QueryAsync<WineReport>(query, parameters);
|
---|
37 |
|
---|
38 | // Convert the list of WineReport to a CSV format
|
---|
39 | var csvContent = GenerateCsv(shippedWineReport);
|
---|
40 |
|
---|
41 | // Return the CSV file as a download
|
---|
42 | var fileName = $"ShippedWineReport_{month}_{year}.csv";
|
---|
43 | var byteArray = Encoding.UTF8.GetBytes(csvContent);
|
---|
44 | return File(byteArray, "text/csv", fileName);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | catch (Exception ex)
|
---|
48 | {
|
---|
49 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [HttpGet("getCustomerShipmentsReport/{month}/{year}")]
|
---|
54 | public async Task<IActionResult> GetCustomerShipmentsReport(int month, int year)
|
---|
55 | {
|
---|
56 | try
|
---|
57 | {
|
---|
58 | using (IDbConnection db = new NpgsqlConnection(_connectionString)) // Use NpgsqlConnection for PostgreSQL
|
---|
59 | {
|
---|
60 | var parameters = new DynamicParameters();
|
---|
61 | parameters.Add("@Month", month, DbType.Int32);
|
---|
62 | parameters.Add("@Year", year, DbType.Int32);
|
---|
63 |
|
---|
64 | const string query = "SELECT * FROM GetCustomerShipmentsReport(@Month, @Year)";
|
---|
65 | var customerShipments = await db.QueryAsync<CustomerReport>(query, parameters);
|
---|
66 |
|
---|
67 | // Convert the list of WineReport to a CSV format
|
---|
68 | var csvContent = GenerateCsv(customerShipments);
|
---|
69 |
|
---|
70 | // Return the CSV file as a download
|
---|
71 | var fileName = $"CustomerShipmentsReport_{month}_{year}.csv";
|
---|
72 | var byteArray = Encoding.UTF8.GetBytes(csvContent);
|
---|
73 | return File(byteArray, "text/csv", fileName);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | catch (Exception ex)
|
---|
77 | {
|
---|
78 | return BadRequest(new ErrorHandler { Name = "Error", Description = ex.Message });
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private string GenerateCsv<T>(IEnumerable<T> reports)
|
---|
83 | {
|
---|
84 | var csvBuilder = new StringBuilder();
|
---|
85 | var headers = typeof(T).GetProperties().Select(p => p.Name).ToArray();
|
---|
86 |
|
---|
87 | // Add header row
|
---|
88 | csvBuilder.AppendLine(string.Join(",", headers));
|
---|
89 |
|
---|
90 | // Add data rows
|
---|
91 | foreach (var report in reports)
|
---|
92 | {
|
---|
93 | var row = headers.Select(header =>
|
---|
94 | {
|
---|
95 | var propertyValue = report.GetType().GetProperty(header).GetValue(report, null);
|
---|
96 | return propertyValue?.ToString().Replace(",", ""); // Remove commas in case of text fields
|
---|
97 | }).ToArray();
|
---|
98 |
|
---|
99 | csvBuilder.AppendLine(string.Join(",", row));
|
---|
100 | }
|
---|
101 |
|
---|
102 | return csvBuilder.ToString();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|