@@ -3,6 +3,7 @@ package main
33import (
44 "fmt"
55 "net/http"
6+ "sort"
67 "strconv"
78
89 httptools "github.com/XDoubleU/essentia/pkg/communication/http"
@@ -110,11 +111,19 @@ func (app *Application) getLocationCheckInsDayHandler(w http.ResponseWriter,
110111 Format (constants .CSVFileNameFormat )
111112 filename = "Day-" + filename
112113
114+ orderedSchoolNames := make ([]string , 0 , len (valueMap ))
115+ for schoolName := range valueMap {
116+ orderedSchoolNames = append (orderedSchoolNames , schoolName )
117+ }
118+ sort .Slice (orderedSchoolNames , func (i , j int ) bool {
119+ return orderedSchoolNames [i ] < orderedSchoolNames [j ]
120+ })
121+
113122 err = httptools .WriteCSV (
114123 w ,
115124 filename ,
116- getCSVHeaders (valueMap ),
117- getCSVData (dateStrings , capacities , valueMap ),
125+ getCSVHeaders (orderedSchoolNames ),
126+ getCSVData (dateStrings , capacities , valueMap , orderedSchoolNames ),
118127 )
119128 } else {
120129 err = httptools .WriteJSON (w , http .StatusOK , dtos.CheckInsGraphDto {
@@ -197,11 +206,19 @@ func (app *Application) getLocationCheckInsRangeHandler(
197206 Format (constants .CSVFileNameFormat )
198207 filename = "Range-" + filename
199208
209+ orderedSchoolNames := make ([]string , 0 , len (valueMap ))
210+ for schoolName := range valueMap {
211+ orderedSchoolNames = append (orderedSchoolNames , schoolName )
212+ }
213+ sort .Slice (orderedSchoolNames , func (i , j int ) bool {
214+ return orderedSchoolNames [i ] < orderedSchoolNames [j ]
215+ })
216+
200217 err = httptools .WriteCSV (
201218 w ,
202219 filename ,
203- getCSVHeaders (valueMap ),
204- getCSVData (dateStrings , capacities , valueMap ),
220+ getCSVHeaders (orderedSchoolNames ),
221+ getCSVData (dateStrings , capacities , valueMap , orderedSchoolNames ),
205222 )
206223 } else {
207224 err = httptools .WriteJSON (w , http .StatusOK , dtos.CheckInsGraphDto {
@@ -217,16 +234,14 @@ func (app *Application) getLocationCheckInsRangeHandler(
217234}
218235
219236func getCSVHeaders (
220- valueMap map [ string ][] int ,
237+ schoolNames [] string ,
221238) []string {
222239 headers := []string {
223240 "datetime" ,
224241 "capacity" ,
225242 }
226243
227- for schoolName := range valueMap {
228- headers = append (headers , schoolName )
229- }
244+ headers = append (headers , schoolNames ... )
230245
231246 return headers
232247}
@@ -235,23 +250,26 @@ func getCSVData(
235250 dateStrings []string ,
236251 capacities map [string ][]int ,
237252 valuesPerSchool map [string ][]int ,
253+ orderedSchoolNames []string ,
238254) [][]string {
239255 var output [][]string
240256
241257 for i , dateString := range dateStrings {
242- for _ , values := range valuesPerSchool {
243- var entry []string
244-
245- var totalCapacity int
246- for _ , capacity := range capacities {
247- totalCapacity += capacity [i ]
248- }
249-
250- entry = append (entry , dateString )
251- entry = append (entry , fmt .Sprintf ("%d" , totalCapacity ))
252- entry = append (entry , strconv .Itoa (values [i ]))
253- output = append (output , entry )
258+ var entry []string
259+
260+ var totalCapacity int
261+ for _ , capacity := range capacities {
262+ totalCapacity += capacity [i ]
254263 }
264+
265+ entry = append (entry , dateString )
266+ entry = append (entry , fmt .Sprintf ("%d" , totalCapacity ))
267+
268+ for _ , schoolName := range orderedSchoolNames {
269+ entry = append (entry , strconv .Itoa (valuesPerSchool [schoolName ][i ]))
270+ }
271+
272+ output = append (output , entry )
255273 }
256274
257275 return output
0 commit comments