getIncident list response
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mocksnow/server/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -37,10 +39,80 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Handle the specific 'number' query parameter
|
||||
number := query.Get("number")
|
||||
active := query.Get("active")
|
||||
if number != "" {
|
||||
h.Logger.Debug("GetIncident called for specific incident number", "number", number)
|
||||
|
||||
// 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 {
|
||||
// Requested a single incident
|
||||
|
Reference in New Issue
Block a user