-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
56 lines (52 loc) · 1.37 KB
/
queue.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 server
import (
"net/http"
"github.com/ReformedDevs/anonbot/db"
"github.com/flosch/pongo2"
"github.com/gorilla/mux"
)
func (s *Server) queue(w http.ResponseWriter, r *http.Request) {
queue := []*db.QueueItem{}
if err := s.database.C.
Order("account_id, date").
Preload("User").
Preload("Account").
Find(&queue).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s.render(w, r, "queue.html", pongo2.Context{
"title": "Queue",
"queue": queue,
})
}
func (s *Server) deleteQueueItem(w http.ResponseWriter, r *http.Request) {
s.database.Transaction(func(c *db.Connection) error {
var (
id = mux.Vars(r)["id"]
q = &db.QueueItem{}
)
if err := c.C.Preload("Account").Find(q, id).Error; err != nil {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return nil
}
if r.Method == http.MethodPost {
if err := c.C.Delete(q).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
q.Account.QueueLength--
if err := c.C.Save(q.Account).Error; err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return nil
}
http.Redirect(w, r, "/queue", http.StatusFound)
return nil
}
s.render(w, r, "delete.html", pongo2.Context{
"title": "Delete Queue Item",
"name": "this queue item",
})
return nil
})
}