-
Notifications
You must be signed in to change notification settings - Fork 0
/
factions.go
105 lines (84 loc) · 1.97 KB
/
factions.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package nrdb
import (
"fmt"
"net/url"
"time"
)
func (cl client) Faction(factionID string) (*Faction, error) {
var res Response[Faction]
if err := cl.nrdbReq(fmt.Sprintf("factions/%s", factionID), &res, nil); err != nil {
return nil, err
}
return &res.Data, nil
}
func (cl client) Factions(filter *FactionFilter) ([]*Faction, error) {
var res Response[[]*Faction]
var query url.Values
if filter != nil {
q, err := filter.Query()
if err != nil {
return nil, fmt.Errorf("encoding filter: %w", err)
}
query = q
}
if err := cl.nrdbReq("factions", &res, query); err != nil {
return nil, err
}
return res.Data, nil
}
type Faction struct {
Document[FactionAttributes, FactionRelationships]
}
func (doc Faction) Name() string {
if doc.Attributes == nil {
return ""
}
return doc.Attributes.Name
}
func (doc Faction) Description() string {
if doc.Attributes == nil {
return ""
}
return doc.Attributes.Description
}
func (doc Faction) IsMini() bool {
if doc.Attributes == nil {
return false
}
return doc.Attributes.IsMini
}
func (doc Faction) SideID() string {
if doc.Attributes == nil {
return ""
}
return doc.Attributes.SideID
}
type FactionAttributes struct {
Name string `json:"name"`
Description string `json:"description"`
IsMini bool `json:"is_mini"`
SideID string `json:"side_id"`
UpdatedAt time.Time `json:"updated_at"`
}
type FactionRelationships struct {
Side *Relationship `json:"side"`
Cards *Relationship `json:"cards"`
Printings *Relationship `json:"printings"`
}
type FactionFilter struct {
SideID *string
IsMini *bool
}
func (filter FactionFilter) Query() (url.Values, error) {
query := url.Values{}
if filter.SideID != nil {
query.Set("filter[side_id]", *filter.SideID)
}
if filter.IsMini != nil {
query.Set("filter[is_mini]", fmt.Sprintf("%t", *filter.IsMini))
}
return query, nil
}
func (doc Faction) String() string {
return fmt.Sprintf("%s (%s)", doc.Attributes.Name, doc.ID)
}