forked from FrenchBen/godisco
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcategory.go
152 lines (143 loc) · 7.14 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package godisco
import (
"encoding/json"
"fmt"
)
//Category expected struct for a single category
type Category struct {
Id int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
TextColor string `json:"text_color"`
Slug string `json:"slug"`
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
Position int `json:"position"`
Description string `json:"description"`
DescriptionText string `json:"description_text"`
TopicUrl string `json:"topic_url"`
ReadRestricted bool `json:"read_restricted"`
Permission int `json:"permission"`
NotificationLevel string `json:"notification_level"`
TopicTemplate string `json:"topic_template"`
HasChildren bool `json:"has_children"`
SortOrder string `json:"sort_order"`
SortAscending string `json:"sort_ascending"`
ShowSubcategoryList bool `json:"show_subcategory_list"`
NumFeaturedTopics int `json:"num_featured_topics"`
DefaultView string `json:"default_view"`
SubcategoryListStyle string `json:"subcategory_list_style"`
DefaultTopPeriod string `json:"default_top_period"`
MinimumRequiredTags int `json:"minimum_required_tags"`
NavigateToFirstPostAfterRead bool `json:"navigate_to_first_post_after_read"`
TopicsDay int `json:"topics_day"`
TopicsWeek int `json:"topics_week"`
TopicsMonth int `json:"topics_month"`
TopicsYear int `json:"topics_year"`
TopicsAllTime int `json:"topics_all_time"`
DescriptionExcerpt string `json:"description_excerpt"`
SubcategoryIds []int `json:"subcategory_ids"`
UploadedLogo string `json:"uploaded_logo"`
UploadedBackground string `json:"uploaded_background"`
}
//CategoriesResponse expected struct for GetCategoryList response
type CategoriesResponse struct {
CategoryList struct {
CanCreateCategory bool `json:"can_create_category"`
CanCreateTopic bool `json:"can_create_topic"`
Draft bool `json:"draft"`
DraftKey string `json:"draft_key"`
DraftSequence int `json:"draft_sequence"`
Categories []*Category `json:"categories"`
} `json:"category_list"`
}
//GroupPermission expected struct for group_permissions in CategoryResponse
type GroupPermission struct {
PermissionType int `json:"permission_type"`
GroupName string `json:"group_name"`
}
//CategoryResponse expected struct for GetCategory and PostCategory response
type CategoryResponse struct {
Category struct {
Id int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
TextColor string `json:"text_color"`
Slug string `json:"slug"`
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
Position int `json:"position"`
Description string `json:"description"`
DescriptionText string `json:"description_text"`
TopicUrl string `json:"topic_url"`
ReadRestricted bool `json:"read_restricted"`
Permission int `json:"permission"`
NotificationLevel string `json:"notification_level"`
CanEdit bool `json:"can_edit"`
TopicTemplate string `json:"topic_template"`
HasChildren string `json:"has_children"`
SortOrder string `json:"sort_order"`
SortAscending string `json:"sort_ascending"`
ShowSubcategoryList bool `json:"show_subcategory_list"`
NumFeaturedTopics int `json:"num_featured_topics"`
DefaultView string `json:"default_view"`
SubcategoryListStyle string `json:"subcategory_list_style"`
DefaultTopPeriod string `json:"default_top_period"`
MinimumRequiredTags int `json:"minimum_required_tags"`
NavigateToFirstPostAfterRead bool `json:"navigate_to_first_post_after_read"`
AvailableGroups []string `json:"available_groups"`
AutoCloseHours string `json:"auto_close_hours"`
AutoCloseBasedOnLastPost bool `json:"auto_close_based_on_last_post"`
EmailIn string `json:"email_in"`
EmailInAllowStrangers bool `json:"email_in_allow_strangers"`
MailinglistMirror bool `json:"mailinglist_mirror"`
SuppressFromLatest bool `json:"suppress_from_latest"`
AllTopicsWiki bool `json:"all_topics_wiki"`
CannotDeleteReason string `json:"cannot_delete_reason"`
AllowBadges bool `json:"allow_badges"`
TopicFeaturedLinkAllowed bool `json:"topic_featured_link_allowed"`
UploadedLogo string `json:"uploaded_logo"`
UploadedBackground string `json:"uploaded_background"`
GroupPermissions []*GroupPermission `json:"group_permissions"`
} `json:"category"`
}
//CategoryRequest expected struct for PostCategory request
type CategoryRequest struct {
Name string `json:"name"`
Slug string `json:"slug"`
Color string `json:"color"`
TextColor string `json:"text_color"`
Description string `json:"description"`
ParentCategoryId string `json:"parent_category_id"`
}
//GetCategoryList show list of categories
func GetCategoryList(req Requester) (categories *CategoriesResponse, err error) {
endpoint := "/categories.json"
body, _, err := req.Get(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &categories)
return categories, err
}
//GetCategory show details of a category
func GetCategory(req Requester, id string) (category *CategoryResponse, err error) {
endpoint := fmt.Sprintf("/c/%s/show.json", id)
body, _, err := req.Get(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &category)
return category, err
}
//PostCategory add a new category
func PostCategory(req Requester, category *CategoryRequest) (response *CategoryResponse, err error) {
data, err := json.Marshal(category)
endpoint := "/categories.json"
body, _, err := req.Post(endpoint, data)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &response)
return response, err
}