statping/handlers/incident.go

116 lines
2.5 KiB
Go
Raw Normal View History

2019-06-24 22:21:38 +00:00
package handlers
import (
2020-02-01 03:53:00 +00:00
"encoding/json"
"github.com/gorilla/mux"
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"
)
func apiAllIncidentsHandler(w http.ResponseWriter, r *http.Request) {
2020-03-04 10:29:00 +00:00
inc := incidents.All()
returnJson(inc, w, r)
2019-06-24 22:21:38 +00:00
}
2020-02-01 03:53:00 +00:00
2020-03-13 04:06:06 +00:00
func apiServiceIncidentsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incids := incidents.FindByService(utils.ToInt(vars["id"]))
returnJson(incids, w, r)
}
2020-03-16 18:28:45 +00:00
func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
incid, err := incidents.Find(utils.ToInt(vars["id"]))
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(incid.Updates(), w, r)
}
2020-03-13 04:06:06 +00:00
func apiCreateIncidentUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var update *incidents.IncidentUpdate
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&update)
if err != nil {
sendErrorJson(err, w, r)
return
}
update.IncidentId = utils.ToInt(vars["id"])
err = update.Create()
if err != nil {
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-03-04 10:29:00 +00:00
var incident *incidents.Incident
2020-02-01 03:53:00 +00:00
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&incident)
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
err = incident.Create()
2020-02-01 03:53:00 +00:00
if err != nil {
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) {
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
incident, err := incidents.Find(utils.ToInt(vars["id"]))
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&incident)
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
updates := incident.Updates()
sendJsonAction(updates, "update", w, r)
2020-02-01 03:53:00 +00:00
}
func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2020-03-04 10:29:00 +00:00
incident, err := incidents.Find(utils.ToInt(vars["id"]))
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
err = incident.Delete()
2020-02-01 03:53:00 +00:00
if err != nil {
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
}
err = update.Delete()
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(update, "delete", w, r)
}