-
Notifications
You must be signed in to change notification settings - Fork 1
/
category.go
85 lines (64 loc) · 2.06 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
package guildedgo
import (
"fmt"
)
type Category struct {
ID int `json:"id"`
ServerID string `json:"serverId"`
GroupID string `json:"groupId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Name string `json:"name"`
}
type CreateCategory struct {
Name string `json:"name"`
GroupID string `json:"groupId,omitempty"`
}
type CategoryService interface {
Read(categoryID int) (*Category, error)
Create(options *CreateCategory) (*Category, error)
Update(categoryID int, name string) (*Category, error)
Delete(categoryID int) error
}
type categoryService struct {
client *Client
}
var _ CategoryService = &categoryService{}
func (s *categoryService) Read(categoryID int) (*Category, error) {
endpoint := fmt.Sprintf("%s/servers/%s/categories/%d", guildedApi, s.client.ServerID, categoryID)
var category Category
err := s.client.GetRequestV2(endpoint, &category)
if err != nil {
return nil, fmt.Errorf("failed to get category: %w", err)
}
return &category, nil
}
func (s *categoryService) Create(options *CreateCategory) (*Category, error) {
endpoint := fmt.Sprintf("%s/servers/%s/categories", guildedApi, s.client.ServerID)
var category Category
err := s.client.PostRequestV2(endpoint, options, &category)
if err != nil {
return nil, fmt.Errorf("failed to create category: %w", err)
}
return &category, nil
}
func (s *categoryService) Update(categoryID int, name string) (*Category, error) {
endpoint := fmt.Sprintf("%s/servers/%s/categories/%d", guildedApi, s.client.ServerID, categoryID)
var category Category
body := map[string]interface{}{
"name": name,
}
err := s.client.PatchRequest(endpoint, body, &category)
if err != nil {
return nil, fmt.Errorf("failed to update category: %w", err)
}
return &category, nil
}
func (s *categoryService) Delete(categoryID int) error {
endpoint := fmt.Sprintf("%s/servers/%s/categories/%d", guildedApi, s.client.ServerID, categoryID)
_, err := s.client.DeleteRequest(endpoint)
if err != nil {
return fmt.Errorf("failed to delete category: %w", err)
}
return nil
}