Files
mocksnow/server/handler/incidentReport.go
Nathan Coad 252bf0a1c8
All checks were successful
continuous-integration/drone/push Build is passing
fix incident view
2025-03-24 17:03:16 +11:00

54 lines
1.4 KiB
Go

package handler
import (
"context"
"encoding/json"
"fmt"
"mocksnow/components/views"
"mocksnow/db/queries"
"mocksnow/internal/utils"
"net/http"
)
func (h *Handler) RenderIncidentTable(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
h.Logger.Debug("Querying incidents table")
results, err := h.Database.Queries().GetIncidentReport(ctx)
if err != nil {
h.Logger.Error("Unable to query incoming table", "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 query incoming table: '%s'", err),
})
return
}
if len(results) == 0 {
h.Logger.Error("Empty updates result")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"status": "ERROR",
"message": fmt.Sprintf("Empty updates result"),
})
return
}
views.IncidentsTable(ConvertIncidentList(results)).Render(r.Context(), w)
}
// Converts a slice of Incoming to []IncomingRow
func ConvertIncidentList(list []queries.GetIncidentReportRow) []views.IncidentRow {
rows := make([]views.IncidentRow, 0, len(list))
for _, in := range list {
//prettyPrint(in)
var row views.IncidentRow
utils.ConvertStruct(in, &row)
rows = append(rows, row)
}
return rows
}