2019-06-24 22:21:38 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-02-01 03:53:00 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-04-17 03:21:17 +00:00
|
|
|
"github.com/statping/statping/types/errors"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/incidents"
|
|
|
|
"github.com/statping/statping/utils"
|
2019-06-24 22:21:38 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-04-16 17:55:47 +00:00
|
|
|
func findIncident(r *http.Request) (*incidents.Incident, int64, error) {
|
|
|
|
vars := mux.Vars(r)
|
2020-04-17 03:21:17 +00:00
|
|
|
if utils.NotNumber(vars["id"]) {
|
|
|
|
return nil, 0, errors.NotNumber
|
|
|
|
}
|
2020-04-16 17:55:47 +00:00
|
|
|
id := utils.ToInt(vars["id"])
|
|
|
|
if id == 0 {
|
2020-04-17 03:21:17 +00:00
|
|
|
return nil, id, errors.IDMissing
|
2020-04-16 17:55:47 +00:00
|
|
|
}
|
|
|
|
checkin, err := incidents.Find(id)
|
|
|
|
if err != nil {
|
2020-04-17 03:21:17 +00:00
|
|
|
return nil, id, errors.Missing(&incidents.Incident{}, id)
|
2020-04-16 17:55:47 +00:00
|
|
|
}
|
|
|
|
return checkin, id, nil
|
|
|
|
}
|
|
|
|
|
2020-03-13 04:06:06 +00:00
|
|
|
func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
|
2020-12-15 08:40:26 +00:00
|
|
|
service, err := findService(r)
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
2020-07-20 11:58:13 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-15 08:40:26 +00:00
|
|
|
returnJson(service.Incidents, w, r)
|
2020-03-13 04:06:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 18:28:45 +00:00
|
|
|
func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-16 17:55:47 +00:00
|
|
|
incid, _, err := findIncident(r)
|
2020-03-16 18:28:45 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-08-04 08:50:06 +00:00
|
|
|
returnJson(incid.Updates, w, r)
|
2020-03-16 18:28:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 04:06:06 +00:00
|
|
|
func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-16 17:55:47 +00:00
|
|
|
incid, _, err := findIncident(r)
|
2020-03-13 04:06:06 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:55:47 +00:00
|
|
|
var update *incidents.IncidentUpdate
|
|
|
|
if err := DecodeJSON(r, &update); err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
update.IncidentId = incid.Id
|
2020-03-13 04:06:06 +00:00
|
|
|
|
2020-07-20 11:58:13 +00:00
|
|
|
if err := update.Create(); err != nil {
|
2020-03-13 04:06:06 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJsonAction(update, "create", w, r)
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:53:00 +00:00
|
|
|
func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-16 17:55:47 +00:00
|
|
|
service, err := findService(r)
|
2020-04-16 17:32:54 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
var incident *incidents.Incident
|
2020-04-16 17:55:47 +00:00
|
|
|
if err := DecodeJSON(r, &incident); err != nil {
|
2020-02-01 03:53:00 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 17:32:54 +00:00
|
|
|
incident.ServiceId = service.Id
|
2020-07-20 11:58:13 +00:00
|
|
|
if err := incident.Create(); err != nil {
|
2020-02-01 03:53:00 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 10:29:00 +00:00
|
|
|
sendJsonAction(incident, "create", w, r)
|
2020-02-01 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func apiIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-16 17:55:47 +00:00
|
|
|
incident, _, err := findIncident(r)
|
2020-02-01 03:53:00 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 17:55:47 +00:00
|
|
|
if err := DecodeJSON(r, &incident); err != nil {
|
2020-02-01 03:53:00 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-04 08:50:06 +00:00
|
|
|
sendJsonAction(incident.Updates, "update", w, r)
|
2020-02-01 03:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-16 17:55:47 +00:00
|
|
|
incident, _, err := findIncident(r)
|
2020-02-01 03:53:00 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-07-20 11:58:13 +00:00
|
|
|
if err := incident.Delete(); err != nil {
|
2020-02-01 03:53:00 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJsonAction(incident, "delete", w, r)
|
|
|
|
}
|
2020-03-13 04:06:06 +00:00
|
|
|
|
|
|
|
func apiDeleteIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
update, err := incidents.FindUpdate(utils.ToInt(vars["uid"]))
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-07-20 11:58:13 +00:00
|
|
|
if err := update.Delete(); err != nil {
|
2020-03-13 04:06:06 +00:00
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJsonAction(update, "delete", w, r)
|
|
|
|
}
|