-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
59 lines (50 loc) · 1.57 KB
/
handler.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
package publicationexporting
import (
"net/http"
"github.com/ugent-library/biblio-backoffice/backends"
"github.com/ugent-library/biblio-backoffice/handlers"
"github.com/ugent-library/biblio-backoffice/models"
"github.com/ugent-library/biblio-backoffice/render"
"github.com/ugent-library/bind"
)
type Handler struct {
handlers.BaseHandler
PublicationSearchIndex backends.PublicationIndex
PublicationListExporters map[string]backends.PublicationListExporterFactory
}
type Context struct {
handlers.BaseContext
SearchArgs *models.SearchArgs
ExportArgs *ExportArgs
}
type ExportArgs struct {
Format string `path:"format,omitempty"`
}
func (h *Handler) Wrap(fn func(http.ResponseWriter, *http.Request, Context)) http.HandlerFunc {
return h.BaseHandler.Wrap(func(w http.ResponseWriter, r *http.Request, ctx handlers.BaseContext) {
if ctx.User == nil {
render.Unauthorized(w, r)
return
}
searchArgs := models.NewSearchArgs()
if err := bind.Request(r, searchArgs); err != nil {
h.Logger.Warnw("publication search: could not bind search arguments", "errors", err, "request", r, "user", ctx.User.ID)
render.BadRequest(w, r, err)
return
}
exportArgs := &ExportArgs{}
if err := bind.Request(r, exportArgs); err != nil {
h.Logger.Warnw("publication search: could not bind export arguments", "errors", err, "request", r, "user", ctx.User.ID)
render.BadRequest(w, r, err)
return
}
if exportArgs.Format == "" {
exportArgs.Format = "xlsx"
}
fn(w, r, Context{
BaseContext: ctx,
SearchArgs: searchArgs,
ExportArgs: exportArgs,
})
})
}