Skip to content

Commit

Permalink
fix: 🐛 remove gzip and fix nil errors (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesOberreiter committed May 11, 2024
1 parent 1e457fa commit bcaaead
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
27 changes: 26 additions & 1 deletion pkg/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,33 @@ func CreateCSV(rows []TableRow) string {
var csv string
csv += strings.Join(_selectArray, ",") + "\n"
for _, row := range rows {
scientificName := ""
if row.ScientificName.Valid {
scientificName = row.ScientificName.String
}
countryCode := ""
if row.CountryCode.Valid {
countryCode = row.CountryCode.String
}
observationID := ""
if row.ObservationID.Valid {
observationID = row.ObservationID.String
}
observationDate := ""
if row.ObservationDate.Valid {
observationDate = row.ObservationDate.Time.Format("2006-01-02")
}
synonymName := ""
if row.SynonymName.Valid {
synonymName = row.SynonymName.String
}
synonymID := ""
if row.SynonymID.Valid {
synonymID = row.SynonymID.String
}
/* Needs to be same order as _selectArray */
csv += fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%t,%s,%s\n", row.TaxonID, row.ScientificName.String, row.CountryCode.String, row.LastFetch.Time.Format("2006-01-02"), row.ObservationID.String, row.ObservationDate.Time.Format("2006-01-02"), row.TaxonKingdom, row.TaxonPhylum, row.TaxonClass, row.TaxonOrder, row.TaxonFamily, row.IsSynonym, row.SynonymName.String, row.SynonymID.String)
csv += fmt.Sprintf(
"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%t,%s,%s\n", row.TaxonID, scientificName, countryCode, row.LastFetch.Time.Format("2006-01-02"), observationID, observationDate, row.TaxonKingdom, row.TaxonPhylum, row.TaxonClass, row.TaxonOrder, row.TaxonFamily, row.IsSynonym, synonymName, synonymID)
}
return csv
}
Expand Down
7 changes: 5 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -191,9 +193,10 @@ func download(c echo.Context) error {
table := queries.GetTableData(internal.DB, q, true)
csv := queries.CreateCSV(table)

c.Response().Header().Set("Content-Disposition", "attachment; filename=extinct.csv")
filename := fmt.Sprintf("extinct-%s-%s.csv", time.Now().Format("2006-01-02"), strings.ReplaceAll(c.QueryString(), "&", "-"))

c.Response().Header().Set("Content-Disposition", "attachment; filename="+filename)
c.Response().Header().Set("Content-Type", "text/csv")
c.Response().Header().Set("Content-Encoding", "gzip")
return c.String(http.StatusOK, csv)
}

Expand Down

0 comments on commit bcaaead

Please sign in to comment.