-
Notifications
You must be signed in to change notification settings - Fork 13
/
category.go
111 lines (101 loc) · 2.81 KB
/
category.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
106
107
108
109
110
111
package opslevel
import (
"fmt"
"github.com/gosimple/slug"
"github.com/hasura/go-graphql-client"
)
type Category struct {
Id ID `json:"id"`
Name string
}
type CategoryConnection struct {
Nodes []Category
PageInfo PageInfo
TotalCount int
}
func (category *Category) Alias() string {
return slug.Make(category.Name)
}
func (client *Client) CreateCategory(input CategoryCreateInput) (*Category, error) {
var m struct {
Payload struct {
Category Category
Errors []OpsLevelErrors
} `graphql:"categoryCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("CategoryCreate"))
return &m.Payload.Category, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetCategory(id ID) (*Category, error) {
var q struct {
Account struct {
Category Category `graphql:"category(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
err := client.Query(&q, v, WithName("CategoryGet"))
if q.Account.Category.Id == "" {
err = graphql.Errors{graphql.Error{
Message: fmt.Sprintf("category with ID '%s' not found", id),
Path: []any{"account", "category"},
}}
}
return &q.Account.Category, HandleErrors(err, nil)
}
func (client *Client) ListCategories(variables *PayloadVariables) (*CategoryConnection, error) {
var q struct {
Account struct {
Rubric struct {
Categories CategoryConnection `graphql:"categories(after: $after, first: $first)"`
}
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("CategoryList")); err != nil {
return nil, err
}
for q.Account.Rubric.Categories.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Rubric.Categories.PageInfo.End
resp, err := client.ListCategories(variables)
if err != nil {
return nil, err
}
q.Account.Rubric.Categories.Nodes = append(q.Account.Rubric.Categories.Nodes, resp.Nodes...)
q.Account.Rubric.Categories.PageInfo = resp.PageInfo
q.Account.Rubric.Categories.TotalCount += resp.TotalCount
}
return &q.Account.Rubric.Categories, nil
}
func (client *Client) UpdateCategory(input CategoryUpdateInput) (*Category, error) {
var m struct {
Payload struct {
Category Category
Errors []OpsLevelErrors
} `graphql:"categoryUpdate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("CategoryUpdate"))
return &m.Payload.Category, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) DeleteCategory(id ID) error {
var m struct {
Payload struct {
Id ID `graphql:"deletedCategoryId"`
Errors []OpsLevelErrors
} `graphql:"categoryDelete(input: $input)"`
}
v := PayloadVariables{
"input": CategoryDeleteInput{Id: id},
}
err := client.Mutate(&m, v, WithName("CategoryDelete"))
return HandleErrors(err, m.Payload.Errors)
}