amo/events/main.go

62 lines
1.3 KiB
Go
Raw Permalink Normal View History

package events
import (
"time"
"strings"
"fmt"
)
type EventsRequest struct {
Filter struct {
CreatedAt struct {
From, To time.Time
}
Type []string
Entity []string
}
Page, Limit uint
Ids, CreatedBy []int
With []string
}
func (req EventsRequest) Format() string {
opts := []string{}
if len(req.With) > 0 {
buf := fmt.Sprintf("with=%s", req.With[0])
for _, with := range req.With[1:] {
buf += ","+with
}
opts = append(opts, buf)
}
if req.Page > 0 {
opts = append(opts, fmt.Sprintf("page=%d", req.Page))
}
if req.Limit > 0 {
opts = append(opts, fmt.Sprintf("limit=%d", req.Limit))
}
if !req.Filter.CreatedAt.From.IsZero() && req.Filter.CreatedAt.To.IsZero() {
opts = append(opts, fmt.Sprintf("filter[created_at]=%d",
req.Filter.CreatedAt.From.Unix(),
))
} else if !req.Filter.CreatedAt.From.IsZero() && !req.Filter.CreatedAt.To.IsZero() {
opts = append(
opts,
fmt.Sprintf("filter[created_at][from]=%d", req.Filter.CreatedAt.From.Unix()),
fmt.Sprintf("filter[created_at][to]=%d", req.Filter.CreatedAt.To.Unix()),
)
}
for i, typ := range req.Filter.Type {
opts = append(opts, fmt.Sprintf("filter[type][%d]=%s", i, typ))
}
for i, ent := range req.Filter.Entity {
opts = append(opts, fmt.Sprintf("filter[entity][%d]=%s", i, ent))
}
ret := strings.Join(opts, "&")
return ret
}