statping/handlers/incident.go

72 lines
1.6 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"
2019-06-24 22:21:38 +00:00
"github.com/hunterlong/statping/core"
2020-02-25 07:41:28 +00:00
"github.com/hunterlong/statping/database"
2020-02-01 03:53:00 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2019-06-24 22:21:38 +00:00
"net/http"
)
func apiAllIncidentsHandler(w http.ResponseWriter, r *http.Request) {
2020-02-25 07:41:28 +00:00
incidents := database.AllIncidents()
2019-06-24 22:21:38 +00:00
returnJson(incidents, w, r)
}
2020-02-01 03:53:00 +00:00
func apiCreateIncidentHandler(w http.ResponseWriter, r *http.Request) {
var incident *types.Incident
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&incident)
if err != nil {
sendErrorJson(err, w, r)
return
}
newIncident := core.ReturnIncident(incident)
2020-03-03 06:42:37 +00:00
obj, err := database.Create(newIncident)
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-03 06:42:37 +00:00
sendJsonAction(obj, "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-03 06:42:37 +00:00
incident, err := database.Incident(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-03 06:42:37 +00:00
err = database.Update(&incident)
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(incident, "update", w, r)
}
func apiDeleteIncidentHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2020-03-03 06:42:37 +00:00
incident, err := database.Incident(utils.ToInt(vars["id"]))
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-03 06:42:37 +00:00
err = database.Delete(incident)
2020-02-01 03:53:00 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
sendJsonAction(incident, "delete", w, r)
}