-
Notifications
You must be signed in to change notification settings - Fork 28
/
messages.go
56 lines (45 loc) · 1.4 KB
/
messages.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
package admin
import (
"html/template"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/olliecoleman/alloy/app/handlers"
"github.com/olliecoleman/alloy/app/models"
"github.com/olliecoleman/alloy/app/views"
"github.com/pkg/errors"
)
func ListMessages(w http.ResponseWriter, r *http.Request) error {
v := views.New(r)
pageNum := handlers.GetPageNum(r)
p, err := views.NewPaginator(pageNum, handlers.PerPage, r.URL)
if err != nil {
return handlers.StatusError{Code: 500, Err: errors.WithStack(err)}
}
messages, numMessages, err := models.ListMessages(p.Start, p.Limit)
if err != nil {
return handlers.StatusError{Code: 500, Err: errors.WithStack(err)}
}
pagination := p.Render(numMessages)
if err != nil {
return handlers.StatusError{Code: 500, Err: errors.WithStack(err)}
}
v.Vars["Messages"] = messages
v.Vars["Pagination"] = template.HTML(pagination)
v.Render(w, "admin/support-messages/index")
return nil
}
func DeleteMessage(w http.ResponseWriter, r *http.Request) error {
id, err := strconv.Atoi(chi.URLParam(r, "ID"))
if err != nil {
return errors.WithStack(err)
}
message := models.SupportMessage{ID: int64(id)}
err = message.Delete()
if err != nil {
return handlers.StatusError{Code: 500, Err: errors.WithStack(err)}
}
views.SuccessFlash(w, r, "Support message was deleted successfully.")
http.Redirect(w, r, "/admin/support-messages", http.StatusSeeOther)
return nil
}