getIncident list response
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nathan Coad
2025-03-25 13:24:23 +11:00
parent e722ebe3f4
commit 4e670f8785
2 changed files with 90 additions and 3 deletions

View File

@@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"mocksnow/server/models"
"net/http" "net/http"
"strconv"
"strings" "strings"
) )
@@ -37,10 +39,80 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
// Handle the specific 'number' query parameter // Handle the specific 'number' query parameter
number := query.Get("number") number := query.Get("number")
active := query.Get("active")
if number != "" { if number != "" {
h.Logger.Debug("GetIncident called for specific incident number", "number", number) h.Logger.Debug("GetIncident called for specific incident number", "number", number)
// Query record from database and return that // Query record from database and return that
} else if active == "true" {
// Return list of all incidents
h.Logger.Debug("GetIncident called for list all incidents")
responseList := make([]models.SingleIncidentResponse, 0)
incList, err := h.Database.Queries().ListIncidents(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
h.Logger.Debug("No incidents found")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "{\"result\": [{}]}")
return
} else {
h.Logger.Error("", "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 database for all incidents: '%s'", err),
})
return
}
}
// convert incList into json output
for _, inc := range incList {
// transform inc to SingleIncidentResponse
r := models.SingleIncidentResponse{
Number: inc.IncidentNumber.String,
SysID: inc.SysID.String,
IncidentState: strconv.FormatInt(inc.State.Int64, 10),
State: strconv.FormatInt(inc.State.Int64, 10),
Impact: strconv.FormatInt(inc.Impact.Int64, 10),
Urgency: strconv.FormatInt(inc.Urgency.Int64, 10),
ShortDescription: inc.ShortDescription.String,
AssignedTo: inc.AssignedTo.String,
Category: inc.Category.String,
Subcategory: inc.SubCategory.String,
// TODO
}
// add to responseList
h.Logger.Debug("Adding incident to active inc response list", "incident", r)
responseList = append(responseList, r)
}
// marshal struct into json and return
wrappedList := models.MultipleIncidentResponse{
Result: responseList,
}
prettyPrint(wrappedList)
b, err := json.Marshal(wrappedList)
if err != nil {
h.Logger.Error("Unable to convert database records into json", "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 convert database records into json: '%s'", err),
})
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(b))
return
} else { } else {
// Requested a single incident // Requested a single incident

View File

@@ -18,11 +18,26 @@ type IncidentResultItem struct {
// TODO - populate more fields here // TODO - populate more fields here
type SingleIncidentResponse struct { type SingleIncidentResponse struct {
IncidentNumber string `json:"incident_number,omitempty"` // The incident number in ServiceNow. If blank, creates a new incident, if populated with a valid value it updates that record. Number string `json:"number"` // corresponds with incident_number
State string `json:"state,omitempty"` // integer value, 1-6 (6 is resolved) SysID string `json:"sys_id,omitempty"`
IncidentState string `json:"incident_state"` // integer value, 1-6 (6 is resolved)
State string `json:"state,omitempty"` // unsure how this differs from IncidentState
ShortDescription string `json:"short_description"`
AssignedTo interface{} `json:"assigned_to,omitempty"`
Description string `json:"description"`
Urgency string `json:"urgency"`
Impact string `json:"impact"`
Category string `json:"category"`
Subcategory string `json:"subcategory"`
// TODO - unvalidated from here on
// Associated DeviceID (UUID from CPDB) // Associated DeviceID (UUID from CPDB)
ExternalID string `json:"external_id,omitempty"` // CPDB UUID for the configuration item ExternalID string `json:"external_id,omitempty"` // CPDB UUID for the configuration item
SysID string `json:"sys_id,omitempty"`
}
type MultipleIncidentResponse struct {
Result []SingleIncidentResponse `json:"result"`
} }
type Incoming struct { type Incoming struct {