1+ using System . Globalization ;
2+ using CsvHelper ;
13using LiveDWAPI . Application . Cs . Dto ;
24using LiveDWAPI . Application . Cs . Queries ;
5+ using LiveDWAPI . Domain . Cs ;
36using MediatR ;
47using Microsoft . AspNetCore . Mvc ;
58using Serilog ;
@@ -85,4 +88,91 @@ public async Task<IActionResult> GetDataByQuery([FromQuery] FilterDto filter)
8588 return StatusCode ( 500 , e . Message ) ;
8689 }
8790 }
91+
92+ [ HttpPost ( "Data" ) ]
93+ [ ProducesResponseType ( typeof ( IndicatorDataDto ) , 200 ) ]
94+ public async Task < IActionResult > GetDataByBody ( [ FromBody ] FilterDto filter )
95+ {
96+ try
97+ {
98+ filter . Limit = 50 ;
99+ var res = await _mediator . Send ( new GetRealtimeFilteredQuery ( filter ) ) ;
100+
101+ if ( res . IsSuccess )
102+ {
103+ var data = res . Value ;
104+ return Ok ( data ) ;
105+ }
106+
107+ throw new Exception ( $ "Error occured ${ res . Error } ") ;
108+ }
109+ catch ( Exception e )
110+ {
111+ Log . Error ( e , "Error loading" ) ;
112+ return StatusCode ( 500 , e . Message ) ;
113+ }
114+ }
115+
116+ [ HttpGet ( "Data/Export" ) ]
117+ public async Task < IActionResult > ExportDataByQuery ( [ FromQuery ] FilterDto filter )
118+ {
119+ // Set the content type and filename for the browser
120+ Response . ContentType = "text/csv" ;
121+ Response . Headers . Add ( "Content-Disposition" , "attachment; filename=cs_export.csv" ) ;
122+
123+ // Use a StreamWriter on the response body to avoid creating a MemoryStream
124+ await using var streamWriter = new StreamWriter ( Response . Body , leaveOpen : true ) ;
125+ await using var csvWriter = new CsvWriter ( streamWriter , CultureInfo . InvariantCulture ) ;
126+
127+ // EF Core 6+ supports IAsyncEnumerable, which is crucial for streaming
128+ var res = await _mediator . Send ( new GetRealtimeFilteredExportQuery ( filter ) ) ;
129+
130+ var data = res . Value ;
131+
132+ // Write the CSV header
133+ csvWriter . WriteHeader < FactRealtimeIndicator > ( ) ;
134+ await csvWriter . NextRecordAsync ( ) ;
135+
136+ // Write records one at a time as they are retrieved from the database
137+ await csvWriter . WriteRecordsAsync ( data ) ;
138+
139+ // Explicitly flush to ensure all data is sent
140+ await streamWriter . FlushAsync ( ) ;
141+
142+ // This indicates the response is complete
143+ return new EmptyResult ( ) ;
144+ }
145+
146+ [ HttpPost ( "Data/Export" ) ]
147+ public async Task < IActionResult > ExportDataByBody ( [ FromBody ] FilterDto filter )
148+ {
149+ Log . Debug ( "Exporting..." ) ;
150+ // Set the content type and filename for the browser
151+ Response . ContentType = "text/csv" ;
152+ Response . Headers . Add ( "Content-Disposition" , "attachment; filename=cs_export.csv" ) ;
153+
154+ // Use a StreamWriter on the response body to avoid creating a MemoryStream
155+ await using var streamWriter = new StreamWriter ( Response . Body , leaveOpen : true ) ;
156+ await using var csvWriter = new CsvWriter ( streamWriter , CultureInfo . InvariantCulture ) ;
157+
158+ // EF Core 6+ supports IAsyncEnumerable, which is crucial for streaming
159+ var res = await _mediator . Send ( new GetRealtimeFilteredExportQuery ( filter ) ) ;
160+
161+ var data = res . Value ;
162+
163+ // Write the CSV header
164+ csvWriter . WriteHeader < FactRealtimeIndicator > ( ) ;
165+ await csvWriter . NextRecordAsync ( ) ;
166+
167+ // Write records one at a time as they are retrieved from the database
168+ await csvWriter . WriteRecordsAsync ( data ) ;
169+
170+ // Explicitly flush to ensure all data is sent
171+ await streamWriter . FlushAsync ( ) ;
172+
173+ Log . Debug ( "Exporting complete !!" ) ;
174+
175+ // This indicates the response is complete
176+ return new EmptyResult ( ) ;
177+ }
88178}
0 commit comments