Files
vctp2/server/handler/reportDownload.go
Nathan Coad f2d6b3158b
All checks were successful
continuous-integration/drone/push Build is passing
Refactor code to use 'any' type and improve context handling
2026-02-18 16:16:27 +11:00

66 lines
2.5 KiB
Go

package handler
import (
"fmt"
"net/http"
"vctp/internal/report"
)
// InventoryReportDownload returns the inventory report as an XLSX download.
// @Summary Download inventory report
// @Description Generates an inventory XLSX report and returns it as a file download.
// @Tags reports
// @Produce application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
// @Success 200 {file} file "Inventory XLSX report"
// @Failure 500 {object} models.ErrorResponse "Report generation failed"
// @Router /api/report/inventory [get]
func (h *Handler) InventoryReportDownload(w http.ResponseWriter, r *http.Request) {
ctx, cancel := withRequestTimeout(r, reportRequestTimeout)
defer cancel()
// Generate the XLSX report
reportData, err := report.CreateInventoryReport(h.Logger, h.Database, ctx)
if err != nil {
h.Logger.Error("Failed to create report", "error", err)
writeJSONError(w, http.StatusInternalServerError, 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)
}
// UpdateReportDownload returns the updates report as an XLSX download.
// @Summary Download updates report
// @Description Generates an updates XLSX report and returns it as a file download.
// @Tags reports
// @Produce application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
// @Success 200 {file} file "Updates XLSX report"
// @Failure 500 {object} models.ErrorResponse "Report generation failed"
// @Router /api/report/updates [get]
func (h *Handler) UpdateReportDownload(w http.ResponseWriter, r *http.Request) {
ctx, cancel := withRequestTimeout(r, reportRequestTimeout)
defer cancel()
// Generate the XLSX report
reportData, err := report.CreateUpdatesReport(h.Logger, h.Database, ctx)
if err != nil {
h.Logger.Error("Failed to create report", "error", err)
writeJSONError(w, http.StatusInternalServerError, 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="updates_report.xlsx"`)
w.Header().Set("File-Name", "updates_report.xlsx")
// Write the XLSX file to the HTTP response
w.Write(reportData)
}