|
|
|
@@ -1,6 +1,7 @@
|
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
@@ -43,24 +44,14 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if number != "" {
|
|
|
|
|
h.Logger.Debug("GetIncident called for specific incident number", "number", number)
|
|
|
|
|
|
|
|
|
|
incResult, err := h.Database.Queries().GetIncident(ctx, nullStr(number))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
h.Logger.Debug("No incident record found", "number", number)
|
|
|
|
|
} else {
|
|
|
|
|
h.Logger.Error("Unable to query database for incident number", "error", err)
|
|
|
|
|
//return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b, err := h.getSingleIncident(number, ctx)
|
|
|
|
|
|
|
|
|
|
b, err := json.Marshal(incResult)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.Logger.Error("Unable to convert database record 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 record into json: '%s'", err),
|
|
|
|
|
"message": fmt.Sprintf("%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -145,24 +136,14 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id := parts[5] // Extract {id}
|
|
|
|
|
h.Logger.Debug("GetIncident called for specific incident", "id", id)
|
|
|
|
|
|
|
|
|
|
incResult, err := h.Database.Queries().GetIncident(ctx, nullStr(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
h.Logger.Debug("No incident record found", "id", id)
|
|
|
|
|
} else {
|
|
|
|
|
h.Logger.Error("Unable to query database for incident number", "error", err)
|
|
|
|
|
//return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b, err := h.getSingleIncident(id, ctx)
|
|
|
|
|
|
|
|
|
|
b, err := json.Marshal(incResult)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.Logger.Error("Unable to convert database record 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 record into json: '%s'", err),
|
|
|
|
|
"message": fmt.Sprintf("%s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -183,3 +164,44 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// TODO - provide an incident list if necessary
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) getSingleIncident(number string, ctx context.Context) ([]byte, error) {
|
|
|
|
|
var b []byte
|
|
|
|
|
incResult, err := h.Database.Queries().GetIncident(ctx, nullStr(number))
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
h.Logger.Debug("No incident record found", "number", number)
|
|
|
|
|
} else {
|
|
|
|
|
h.Logger.Error("Unable to query database for incident number", "error", err)
|
|
|
|
|
return b, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert from database resposne to expected json format
|
|
|
|
|
r := models.SingleIncidentResponse{
|
|
|
|
|
Number: incResult.IncidentNumber.String,
|
|
|
|
|
SysID: incResult.SysID.String,
|
|
|
|
|
IncidentState: strconv.FormatInt(incResult.State.Int64, 10),
|
|
|
|
|
State: strconv.FormatInt(incResult.State.Int64, 10),
|
|
|
|
|
Impact: strconv.FormatInt(incResult.Impact.Int64, 10),
|
|
|
|
|
Urgency: strconv.FormatInt(incResult.Urgency.Int64, 10),
|
|
|
|
|
ShortDescription: incResult.ShortDescription.String,
|
|
|
|
|
AssignedTo: incResult.AssignedTo.String,
|
|
|
|
|
Category: incResult.Category.String,
|
|
|
|
|
Subcategory: incResult.SubCategory.String,
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrappedList := models.MultipleIncidentResponse{
|
|
|
|
|
Result: []models.SingleIncidentResponse{r},
|
|
|
|
|
}
|
|
|
|
|
prettyPrint(wrappedList)
|
|
|
|
|
|
|
|
|
|
b, err = json.Marshal(wrappedList)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.Logger.Error("Unable to convert database record into json", "error", err)
|
|
|
|
|
return b, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
|
}
|
|
|
|
|