enhance implementation
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:
@@ -1,6 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -8,8 +11,9 @@ import (
|
||||
|
||||
// GetIncident responds to the link generated in the response to a New Snow
|
||||
func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
h.Logger.Debug("GetIncident Request received", "method", r.Method, "path", r.URL.Path)
|
||||
// TODO
|
||||
h.Logger.Debug("GetIncident Request received", "method", r.Method, "url", r.URL, "path", r.URL.Path, "query", r.URL.Query())
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
path := r.URL.Path
|
||||
// Expected format: /api/now/table/incident/{id}
|
||||
@@ -30,17 +34,54 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if len(parts) == 6 && strings.HasPrefix(path, "/api/now/table/incident/") {
|
||||
// Requested a single incident
|
||||
id := parts[5] // Extract {id}
|
||||
h.Logger.Debug("GetIncident called for specific incident", "id", id)
|
||||
|
||||
// Handle the specific 'number' query parameter
|
||||
number := query.Get("number")
|
||||
if number != "" {
|
||||
h.Logger.Debug("GetIncident called for specific incident number", "number", number)
|
||||
|
||||
// Query record from database and return that
|
||||
|
||||
} else {
|
||||
// Requested a single incident
|
||||
id := parts[5] // Extract {id}
|
||||
h.Logger.Debug("GetIncident called for specific incident", "id", id)
|
||||
|
||||
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 := 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),
|
||||
})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, string(b))
|
||||
return
|
||||
}
|
||||
|
||||
} else if strings.HasPrefix(path, "/api/now/table/incident") {
|
||||
h.Logger.Debug("GetIncident called for list of incidents")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "{\"result\": [{}]}")
|
||||
|
||||
}
|
||||
|
||||
// TODO - provide an incident list if necessary
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "{\"result\": [{}]}")
|
||||
}
|
||||
|
Reference in New Issue
Block a user