fake-sms/internal/route/helper.go
2020-12-21 15:59:07 +01:00

102 lines
2.0 KiB
Go

package route
import (
"net/http"
"strconv"
"time"
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template"
)
func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Data) template.Data {
ctn := container.Must(r.Context())
data, err := template.Extend(data,
template.WithBuildInfo(w, r, ctn),
)
if err != nil {
panic(errors.Wrap(err, "could not extend template data"))
}
return data
}
func createOutboxQueryFromRequest(r *http.Request) (*query.GetOutboxRequest, error) {
orderBy := r.URL.Query().Get("orderBy")
reverse := r.URL.Query().Get("reverse")
recipient := r.URL.Query().Get("recipient")
body := r.URL.Query().Get("body")
var err error
var limit int64 = 0
rawLimit := r.URL.Query().Get("limit")
if rawLimit != "" {
limit, err = strconv.ParseInt(rawLimit, 10, 32)
if err != nil {
return nil, errors.WithStack(err)
}
}
var skip int64 = 0
rawSkip := r.URL.Query().Get("skip")
if rawSkip != "" {
skip, err = strconv.ParseInt(rawSkip, 10, 32)
if err != nil {
return nil, errors.WithStack(err)
}
}
var after time.Time
rawAfter := r.URL.Query().Get("after")
if rawAfter != "" {
after, err = time.Parse(time.RFC3339, rawAfter)
if err != nil {
return nil, errors.WithStack(err)
}
}
var before time.Time
rawBefore := r.URL.Query().Get("before")
if rawBefore != "" {
before, err = time.Parse(time.RFC3339, rawBefore)
if err != nil {
return nil, errors.WithStack(err)
}
}
search := &query.OutboxSearch{}
if recipient != "" {
search.Recipient = recipient
}
if body != "" {
search.Body = body
}
if rawAfter != "" {
search.After = after
}
if rawBefore != "" {
search.Before = before
}
inboxRequest := &query.GetOutboxRequest{
OrderBy: orderBy,
Reverse: reverse == "y",
Skip: int(skip),
Limit: int(limit),
Search: search,
}
return inboxRequest, nil
}