-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventapi_vouchers.go
57 lines (49 loc) · 1.16 KB
/
eventapi_vouchers.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
package webapi
import (
"encoding/json"
"strconv"
"strings"
model "github.com/raceresult/go-model"
)
// Vouchers contains all api endpoints regarding vouchers
type Vouchers struct {
api *EventAPI
}
// newVouchers creates a new Vouchers api endpoint group
func newVouchers(api *EventAPI) *Vouchers {
return &Vouchers{
api: api,
}
}
// Get returns one or all vouchers
func (q *Vouchers) Get(code string) ([]model.Voucher, error) {
values := urlValues{
"code": code,
}
bts, err := q.api.get("vouchers/get", values)
if err != nil {
return nil, err
}
var dest []model.Voucher
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// Delete deletes vouchers
func (q *Vouchers) Delete(ids []int) error {
sids := make([]string, 0, len(ids))
for _, id := range ids {
sids = append(sids, strconv.Itoa(id))
}
_, err := q.api.post("vouchers/delete", nil, strings.Join(sids, ";"))
return err
}
// Save saves vouchers and returns the IDs
func (q *Vouchers) Save(items []model.Voucher) ([]int, error) {
bts, err := q.api.post("vouchers/save", nil, items)
if err != nil {
return nil, err
}
return parseJsonIntArr(bts)
}