-
Notifications
You must be signed in to change notification settings - Fork 41
/
server-notifications.go
89 lines (83 loc) · 2.44 KB
/
server-notifications.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 api
import (
"github.com/gin-gonic/gin"
"github.com/joschahenningsen/TUM-Live/dao"
"github.com/joschahenningsen/TUM-Live/model"
"github.com/joschahenningsen/TUM-Live/tools"
"log"
"net/http"
"time"
)
func configServerNotificationsRoutes(engine *gin.Engine, daoWrapper dao.DaoWrapper) {
routes := serverNotificationRoutes{daoWrapper}
adminGroup := engine.Group("/api/serverNotification")
adminGroup.Use(tools.Admin)
adminGroup.POST("/:notificationId", routes.updateServerNotification)
adminGroup.POST("/create", routes.createServerNotification)
}
type serverNotificationRoutes struct {
dao.DaoWrapper
}
func (r serverNotificationRoutes) updateServerNotification(c *gin.Context) {
var req notificationReq
if err := c.ShouldBind(&req); err != nil {
log.Printf("%v", err)
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind body",
Err: err,
})
return
}
notification := model.ServerNotification{
Text: req.Text,
Warn: req.Type == "warning",
Start: req.From,
Expires: req.Expires,
}
err := r.ServerNotificationDao.UpdateServerNotification(notification, req.Id)
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not update server notification",
Err: err,
})
return
}
c.Redirect(http.StatusFound, "/admin/server-notifications")
}
func (r serverNotificationRoutes) createServerNotification(c *gin.Context) {
var req notificationReq
if err := c.ShouldBind(&req); err != nil {
log.Printf("%v", err)
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind body",
Err: err,
})
return
}
notification := model.ServerNotification{
Text: req.Text,
Warn: req.Type == "warn",
Start: req.From,
Expires: req.Expires,
}
err := r.ServerNotificationDao.CreateServerNotification(notification)
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not create server notification",
Err: err,
})
return
}
c.Redirect(http.StatusFound, "/admin/server-notifications")
}
type notificationReq struct {
Text string `form:"text"`
From time.Time `form:"from" time_format:"2006-01-02 15:04"`
Expires time.Time `form:"expires" time_format:"2006-01-02 15:04"`
Type string `form:"type"`
Id string `form:"id"`
}