-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
executable file
·64 lines (55 loc) · 1.64 KB
/
report.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 handler
import (
"context"
"errors"
"net/http"
"time"
"github.com/greatfocus/gf-notify/repositories"
"github.com/greatfocus/gf-sframe/server"
)
// Report struct
type Report struct {
FileHandler func(http.ResponseWriter, *http.Request)
messageRepository *repositories.MessageRepository
meta *server.Meta
}
// ServeHTTP checks if is valid method
func (hdl Report) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
hdl.get(w, r)
return
}
// catch all
// if no method is satisfied return an error
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Add("Allow", "GET")
}
// Init method
func (hdl *Report) Init(meta *server.Meta) {
hdl.messageRepository = &repositories.MessageRepository{}
hdl.messageRepository.Init(meta.DB, meta.Cache)
hdl.meta = meta
}
// getDashboard method gets dashboard
func (hdl *Report) get(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(hdl.meta.Timeout)*time.Second)
defer cancel()
table := r.FormValue("table")
channelID := r.FormValue("channelId")
year := r.FormValue("year")
month := r.FormValue("month")
lastID := r.FormValue("page")
if table != "" && channelID != "" && year != "" && month != "" && lastID != "" {
messages, err := hdl.messageRepository.Report(ctx, hdl.meta.JWT.Secret, table, channelID, year, month, lastID)
if err != nil {
hdl.meta.Error(w, r, err)
return
}
w.WriteHeader(http.StatusOK)
hdl.meta.Success(w, r, messages)
return
}
derr := errors.New("invalid payload request")
w.WriteHeader(http.StatusUnprocessableEntity)
hdl.meta.Error(w, r, derr)
}