/
event.go
64 lines (57 loc) · 1.64 KB
/
event.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
package handlers
import (
"net/http"
"strconv"
"gopkg.in/mgo.v2/bson"
"github.com/AdhityaRamadhanus/gopatrol"
"github.com/AdhityaRamadhanus/gopatrol/api/middlewares"
"github.com/AdhityaRamadhanus/gopatrol/api/render"
"github.com/gorilla/mux"
)
type EventsHandlers struct {
EventService gopatrol.EventService
CacheService gopatrol.CacheService
}
func (h *EventsHandlers) AddRoutes(router *mux.Router, isUnixDomain bool) {
if !isUnixDomain {
router.HandleFunc("/events/all", middlewares.AuthenticateToken(middlewares.Gzip(http.HandlerFunc(h.GetAllEvents)), 2)).Methods("GET")
} else {
router.HandleFunc("/events/all", h.GetAllEvents).Methods("GET")
}
}
func (h *EventsHandlers) GetAllEvents(res http.ResponseWriter, req *http.Request) {
page := 0
size := 10
// Take Querystring
queryStrings := req.URL.Query()
if len(queryStrings["page"]) > 0 {
page, _ = strconv.Atoi(queryStrings["page"][0])
}
if len(queryStrings["size"]) > 0 {
size, _ = strconv.Atoi(queryStrings["size"][0])
}
// cacheKey := fmt.Sprintf("events:%d:%d", page, size)
// respBytes, err := h.CacheService.Get(cacheKey)
// if err == nil {
// helper.WriteGzipBytes(res, req, http.StatusOK, respBytes)
// return
// }
query := bson.M{}
counts, _ := h.EventService.CountEvents(query)
events, _ := h.EventService.GetAllEvents(map[string]interface{}{
"query": query,
"pagination": true,
"page": page,
"limit": size,
})
response := render.JSON{
"pagination": map[string]int{
"total": counts,
"page": page,
"size": size,
},
"events": events,
}
// h.CacheService.Set(cacheKey, respBytes)
render.WriteJSON(res, http.StatusOK, response)
}