This commit is contained in:
53
server/handler/incidentReport.go
Normal file
53
server/handler/incidentReport.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
}
|
@@ -4,16 +4,16 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"mocksnow/components/views"
|
||||
"mocksnow/db/queries"
|
||||
"mocksnow/internal/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *Handler) RenderIncomingTable(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
|
||||
h.Logger.Debug("Querying updates table")
|
||||
h.Logger.Debug("Querying incoming table")
|
||||
results, err := h.Database.Queries().ListIncoming(ctx)
|
||||
if err != nil {
|
||||
h.Logger.Error("Unable to query incoming table", "error", err)
|
||||
|
@@ -31,6 +31,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
|
||||
mux.HandleFunc("/api/now/import/x_dusa2_itom_inc_imp", h.NewSnow)
|
||||
mux.HandleFunc("/api/now/table/incident/", h.GetIncident)
|
||||
mux.HandleFunc("/api/print", h.RenderIncomingTable)
|
||||
mux.HandleFunc("/api/print/incidents", h.RenderIncidentTable)
|
||||
|
||||
// TODO - fallback route that will just echo incoming payload
|
||||
mux.HandleFunc("/", h.Fallback)
|
||||
|
Reference in New Issue
Block a user