fix sql and getIncident
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,7 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE worknotes ADD COLUMN created_at DATETIME DEFAULT CURRENT_TIMESTAMP;
|
||||
ALTER TABLE worknotes ADD COLUMN created_at DATETIME;
|
||||
UPDATE worknotes SET created_at = CURRENT_TIMESTAMP WHERE created_at IS NULL;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
|
@@ -62,8 +62,8 @@ GROUP BY incidents.id;
|
||||
|
||||
-- name: CreateWorkNote :one
|
||||
INSERT into worknotes (
|
||||
"incident_number", "note"
|
||||
"incident_number", "note", "created_at"
|
||||
) VALUES(
|
||||
?, ?
|
||||
?, ?, CURRENT_TIMESTAMP
|
||||
)
|
||||
RETURNING *;
|
@@ -132,9 +132,9 @@ func (q *Queries) CreateIncoming(ctx context.Context, arg CreateIncomingParams)
|
||||
|
||||
const createWorkNote = `-- name: CreateWorkNote :one
|
||||
INSERT into worknotes (
|
||||
"incident_number", "note"
|
||||
"incident_number", "note", "created_at"
|
||||
) VALUES(
|
||||
?, ?
|
||||
?, ?, CURRENT_TIMESTAMP
|
||||
)
|
||||
RETURNING id, incident_number, note, created_at
|
||||
`
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user