test creating excel report from inventory table
Some checks failed
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-09-26 21:22:45 +10:00
parent dcbbff830d
commit f88b812fa9
5 changed files with 136 additions and 9 deletions

View File

@@ -0,0 +1,35 @@
package handler
import (
"context"
"encoding/json"
"fmt"
"net/http"
"vctp/internal/report"
)
func (h *Handler) ReportDownload(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
// Generate the XLSX report
reportData, err := report.CreateReport(h.Logger, h.Database, ctx)
if err != nil {
h.Logger.Error("Failed to create report", "error", err)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Unable to create xlsx report: '%s'", err),
})
return
}
// Set HTTP headers to indicate file download
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", `attachment; filename="inventory_report.xlsx"`)
w.Header().Set("File-Name", "inventory_report.xlsx")
// Write the XLSX file to the HTTP response
w.Write(reportData)
}

View File

@@ -31,5 +31,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
// temporary endpoint
//mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
mux.HandleFunc("/api/report/download", h.ReportDownload)
return middleware.NewLoggingMiddleware(logger, mux)
}