fake-sms/internal/route/helper.go

118 lines
2.3 KiB
Go
Raw Permalink Normal View History

2020-12-21 15:56:56 +01:00
package route
import (
"net/http"
"strconv"
"time"
2021-01-15 16:28:45 +01:00
"forge.cadoles.com/Cadoles/fake-sms/internal/config"
2020-12-21 15:56:56 +01:00
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
2021-01-15 16:28:45 +01:00
"gitlab.com/wpetit/goweb/service"
2020-12-21 15:56:56 +01:00
"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),
2021-01-15 16:28:45 +01:00
withBaseURL(ctn),
2020-12-21 15:56:56 +01:00
)
if err != nil {
panic(errors.Wrap(err, "could not extend template data"))
}
return data
}
2021-01-15 16:28:45 +01:00
func withBaseURL(ctn *service.Container) template.DataExtFunc {
return func(data template.Data) (template.Data, error) {
conf, err := config.From(ctn)
if err != nil {
return nil, errors.WithStack(err)
}
data["BaseURL"] = conf.HTTP.BaseURL
return data, nil
}
}
2020-12-21 15:56:56 +01:00
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
}