aboutsummaryrefslogtreecommitdiffstats
path: root/query.go
blob: 361195831a2790676da62f264411cfc87e808470 (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package lulu

import (
	"fmt"
	"net/url"
	"time"
)

type PrintJobQuery func(*printJobQueries)

type printJobQueries struct {
	creatAfter, creatBefore, modAfter, modBefore *time.Time
	status                                       *OrderStatus
	id, orderId                                  *uint
	excludeLineItems                             bool
}

func (q *printJobQueries) apply(f ...PrintJobQuery) {
	for i := range f {
		f[i](q)
	}
}

func (q *printJobQueries) vals() url.Values {
	v := url.Values{}
	if q.creatAfter != nil {
		v.Set("created_after", q.creatAfter.UTC().Format(time.RFC3339))
	}
	if q.creatBefore != nil {
		v.Set("created_before", q.creatBefore.UTC().Format(time.RFC3339))
	}
	if q.modAfter != nil {
		v.Set("modified_after", q.modAfter.UTC().Format(time.RFC3339))
	}
	if q.modBefore != nil {
		v.Set("modified_before", q.modBefore.UTC().Format(time.RFC3339))
	}
	if q.status != nil {
		v.Set("status", string(*q.status))
	}
	if q.id != nil {
		v.Set("id", fmt.Sprint(*q.id))
	}
	if q.orderId != nil {
		v.Set("order_id", fmt.Sprint(*q.orderId))
	}
	if q.excludeLineItems {
		v.Set("exclude_line_items", "true")
	}
	return v
}

// Include only print jobs created after t.
func FilterCreatedAfter(t time.Time) PrintJobQuery {
	return func(q *printJobQueries) {
		q.creatAfter = &t
	}
}

// Include only print jobs created before t.
func FilterCreatedBefore(t time.Time) PrintJobQuery {
	return func(q *printJobQueries) {
		q.creatBefore = &t
	}
}

// Include only print jobs modified after t.
func FilterModifiedAfter(t time.Time) PrintJobQuery {
	return func(q *printJobQueries) {
		q.modAfter = &t
	}
}

// Include only print jobs modified before t.
func FilterModifiedBefore(t time.Time) PrintJobQuery {
	return func(q *printJobQueries) {
		q.modBefore = &t
	}
}

// Include only print jobs with status s.
func FilterStatus(s OrderStatus) PrintJobQuery {
	return func(q *printJobQueries) {
		q.status = &s
	}
}

// Include only print jobs with the given id.
func FilterId(id uint) PrintJobQuery {
	return func(q *printJobQueries) {
		q.id = &id
	}
}

// Include only print jobs with the given order_id.
func FilterOrderId(orderId uint) PrintJobQuery {
	return func(q *printJobQueries) {
		q.orderId = &orderId
	}
}

// Leave the list of line items out of the print jobs response.
func ExcludeLineItems() PrintJobQuery {
	return func(q *printJobQueries) {
		q.excludeLineItems = true
	}
}