-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventapi_contests.go
81 lines (69 loc) · 1.56 KB
/
eventapi_contests.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package webapi
import (
"encoding/json"
model "github.com/raceresult/go-model"
)
// Contests contains all api endpoints regarding contests
type Contests struct {
api *EventAPI
}
// newContests creates a new Contests api endpoint group
func newContests(api *EventAPI) *Contests {
return &Contests{
api: api,
}
}
// PDF returns a PDF with all contests
func (q *Contests) PDF() ([]byte, error) {
return q.api.get("contests/pdf", nil)
}
// GetOne returns the contest with the given ID
func (q *Contests) GetOne(id int) (*model.Contest, error) {
values := urlValues{
"id": id,
}
bts, err := q.api.get("contests/get", values)
if err != nil {
return nil, err
}
var dest model.Contest
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return &dest, nil
}
// Get returns all contests
func (q *Contests) Get() ([]model.Contest, error) {
bts, err := q.api.get("contests/get", nil)
if err != nil {
return nil, err
}
var dest []model.Contest
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// Delete deletes a contest
func (q *Contests) Delete(id int) error {
values := urlValues{
"id": id,
}
_, err := q.api.get("contests/delete", values)
return err
}
// Save saves a contest and returns the ID
func (q *Contests) Save(item model.Contest, oldID int) (int, error) {
values := urlValues{
"oldID": oldID,
}
bts, err := q.api.post("contests/save", values, item)
if err != nil {
return 0, err
}
var id int
if err := json.Unmarshal(bts, &id); err != nil {
return 0, err
}
return id, nil
}