-
Notifications
You must be signed in to change notification settings - Fork 41
/
notifications.go
102 lines (95 loc) · 2.77 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
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 "github.com/sirupsen/logrus"
"net/http"
"strconv"
)
func configNotificationsRouter(r *gin.Engine, daoWrapper dao.DaoWrapper) {
routes := notificationRoutes{daoWrapper}
notifications := r.Group("/api/notifications")
{
notifications.GET("/", routes.getNotifications)
notifications.POST("/", tools.Admin, routes.createNotification)
notifications.DELETE("/:id", tools.Admin, routes.deleteNotification)
}
}
type notificationRoutes struct {
dao.DaoWrapper
}
func (r notificationRoutes) getNotifications(c *gin.Context) {
f, _ := c.Get("TUMLiveContext")
ctx := f.(tools.TUMLiveContext)
targets := []model.NotificationTarget{model.TargetAll}
if ctx.User != nil {
targets = append(targets, model.TargetUser)
switch ctx.User.Role {
case model.AdminType:
targets = append(targets, model.TargetAdmin)
case model.LecturerType:
targets = append(targets, model.TargetLecturer)
case model.StudentType:
targets = append(targets, model.TargetStudent)
}
}
notifications, err := r.NotificationsDao.GetNotifications(targets...)
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusNotFound,
CustomMessage: "can not get notifications",
Err: err,
})
return
}
c.JSON(http.StatusOK, notifications)
}
func (r notificationRoutes) createNotification(c *gin.Context) {
var notification model.Notification
if err := c.BindJSON(¬ification); err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "can not bind body",
Err: err,
})
return
}
if *notification.Title == "" {
notification.Title = nil
}
notification.Body = notification.SanitizedBody // reverse json binding
if err := r.NotificationsDao.AddNotification(¬ification); err != nil {
log.Error(err)
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not add notification",
Err: err,
})
return
}
c.JSON(http.StatusOK, notification)
}
func (r notificationRoutes) deleteNotification(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "invalid param 'id'",
Err: err,
})
return
}
err = r.NotificationsDao.DeleteNotification(uint(id))
if err != nil {
log.WithError(err).Error("error deleting notification")
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "error deleting notification",
Err: err,
})
return
}
c.JSON(http.StatusOK, gin.H{"success": true})
}