forked from StevenWeathers/thunderdome-planning-poker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_alerts.go
89 lines (70 loc) · 2.32 KB
/
handlers_alerts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
)
// handleGetAlerts gets a list of alerts
func (s *server) handleGetAlerts() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Limit, _ := strconv.Atoi(vars["limit"])
Offset, _ := strconv.Atoi(vars["offset"])
Alerts := s.database.AlertsList(Limit, Offset)
s.respondWithJSON(w, http.StatusOK, Alerts)
}
}
// handleAlertCreate creates a new alert
func (s *server) handleAlertCreate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keyVal := s.getJSONRequestBody(r, w)
Name := keyVal["name"].(string)
Type := keyVal["type"].(string)
Content := keyVal["content"].(string)
Active := keyVal["active"].(bool)
AllowDismiss := keyVal["allowDismiss"].(bool)
RegisteredOnly := keyVal["registeredOnly"].(bool)
err := s.database.AlertsCreate(Name, Type, Content, Active, AllowDismiss, RegisteredOnly)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ActiveAlerts = s.database.GetActiveAlerts()
s.respondWithJSON(w, http.StatusOK, ActiveAlerts)
}
}
// handleAlertUpdate updates an alert
func (s *server) handleAlertUpdate() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keyVal := s.getJSONRequestBody(r, w)
vars := mux.Vars(r)
ID := vars["id"]
Name := keyVal["name"].(string)
Type := keyVal["type"].(string)
Content := keyVal["content"].(string)
Active := keyVal["active"].(bool)
AllowDismiss := keyVal["allowDismiss"].(bool)
RegisteredOnly := keyVal["registeredOnly"].(bool)
err := s.database.AlertsUpdate(ID, Name, Type, Content, Active, AllowDismiss, RegisteredOnly)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ActiveAlerts = s.database.GetActiveAlerts()
s.respondWithJSON(w, http.StatusOK, ActiveAlerts)
}
}
// handleAlertDelete handles deleting an alert
func (s *server) handleAlertDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
keyVal := s.getJSONRequestBody(r, w)
AlertID := keyVal["id"].(string)
err := s.database.AlertDelete(AlertID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
ActiveAlerts = s.database.GetActiveAlerts()
s.respondWithJSON(w, http.StatusOK, ActiveAlerts)
}
}