-
Notifications
You must be signed in to change notification settings - Fork 13
/
controller.go
93 lines (80 loc) · 3.47 KB
/
controller.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
package notification
import (
"net/http"
"strconv"
"github.com/qor/admin"
"github.com/qor/responder"
)
type controller struct {
Notification *Notification
action *Action
}
func (c *controller) List(context *admin.Context) {
context.Set("Notification", c.Notification)
var currentPage int
if p, err := strconv.Atoi(context.Request.URL.Query().Get("page")); err == nil {
currentPage = p
}
context.Execute("notifications/notifications", map[string]interface{}{
"Messages": c.Notification.GetNotifications(context.CurrentUser, context.Context),
"LoadMoreNextPage": currentPage + 1,
})
}
func (c *controller) Action(context *admin.Context) {
action := c.action
message := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.Set("Notification", c.Notification)
if context.Request.Method == "GET" {
context.Execute("action", action)
} else {
var actionArgument = &ActionArgument{
Message: message,
Context: context,
}
if action.Resource != nil {
result := action.Resource.NewStruct()
action.Resource.Decode(context.Context, result)
actionArgument.Argument = result
}
if err := action.Handler(actionArgument); err == nil {
flash := action.FlashMessage(actionArgument, true /* succeed */, false /* undo */)
responder.With("html", func() {
context.Flash(flash, "success")
http.Redirect(context.Writer, context.Request, context.Request.Referer(), http.StatusFound)
}).With("json", func() {
notification := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.JSON("OK", map[string]string{"status": "ok", "message": flash, "notification": string(context.Render("notification", notification))})
}).Respond(context.Request)
} else {
notification := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.JSON("OK", map[string]string{"status": "error", "error": action.FlashMessage(actionArgument, false /* succeed */, false /* undo */), "notification": string(context.Render("notification", notification))})
}
}
}
func (c *controller) UndoAction(context *admin.Context) {
action := c.action
message := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.Set("Notification", c.Notification)
var actionArgument = &ActionArgument{
Message: message,
Context: context,
}
if action.Resource != nil {
result := action.Resource.NewStruct()
action.Resource.Decode(context.Context, result)
actionArgument.Argument = result
}
if err := action.Undo(actionArgument); err == nil {
flash := action.FlashMessage(actionArgument, true /* succeed */, true /* undo */)
responder.With("html", func() {
context.Flash(flash, "success")
http.Redirect(context.Writer, context.Request, context.Request.Referer(), http.StatusFound)
}).With("json", func() {
notification := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.JSON("OK", map[string]string{"status": "ok", "message": flash, "notification": string(context.Render("notification", notification))})
}).Respond(context.Request)
} else {
notification := c.Notification.GetNotification(context.CurrentUser, context.ResourceID, context.Context)
context.JSON("OK", map[string]string{"status": "error", "error": action.FlashMessage(actionArgument, false /* succeed */, true /* undo */), "notification": string(context.Render("notification", notification))})
}
}