forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
61 lines (53 loc) · 1.71 KB
/
notification.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
package handler
import (
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/controller"
)
func notification(router chi.Router) {
router.Get("/", getNotification)
router.Delete("/", deleteNotification)
router.Delete("/all", deleteAllNotifications)
}
func getNotification(writer http.ResponseWriter, request *http.Request) {
start, err := strconv.ParseInt(request.URL.Query().Get("start"), 10, 64)
if err != nil {
start = 0
}
end, err := strconv.ParseInt(request.URL.Query().Get("end"), 10, 64)
if err != nil {
end = -1
}
notifications, errorResponse := controller.GetNotifications(database, start, end)
if errorResponse != nil {
render.Render(writer, request, errorResponse)
return
}
if err := render.Render(writer, request, notifications); err != nil {
render.Render(writer, request, api.ErrorRender(err))
}
}
func deleteNotification(writer http.ResponseWriter, request *http.Request) {
notificationKey := request.URL.Query().Get("id")
if notificationKey == "" {
render.Render(writer, request, api.ErrorInvalidRequest(fmt.Errorf("notification id can not be empty")))
return
}
notifications, errorResponse := controller.DeleteNotification(database, notificationKey)
if errorResponse != nil {
render.Render(writer, request, errorResponse)
return
}
if err := render.Render(writer, request, notifications); err != nil {
render.Render(writer, request, api.ErrorRender(err))
}
}
func deleteAllNotifications(writer http.ResponseWriter, request *http.Request) {
if errorResponse := controller.DeleteAllNotifications(database); errorResponse != nil {
render.Render(writer, request, errorResponse)
}
}