-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
131 lines (102 loc) · 2.83 KB
/
group.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
package docbase
import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// GroupService implements interface with API /groups endpoint.
// See https://help.docbase.io/posts/45703#%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97
type GroupService interface {
List(opts *GroupListOptions) (*GroupListResponse, *Response, error)
Get(id int) (*Group, *Response, error)
Create(createRequest *GroupCreateRequest) (*Group, *Response, error)
}
// groupService handles communication with API
type groupService struct {
client *Client
}
// Group represents a docbase minimum Group
type SimpleGroup struct {
ID int `json:"id"`
Name string `json:"name"`
}
// Group represents a docbase Group
type Group struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
PostsCount int `json:"posts_count"`
LastActivityAt time.Time `json:"last_activity_at"`
CreatedAt time.Time `json:"created_at"`
Users []SimpleUser `json:"users"`
}
// GroupListOptions identifies as query params of List request
type GroupListOptions struct {
Name string `url:"name,omitempty"`
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}
type GroupCreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
}
// GroupListResponse represents a List simple group
type GroupListResponse []SimpleGroup
// List Group
func (s *groupService) List(opts *GroupListOptions) (*GroupListResponse, *Response, error) {
u, err := url.Parse("/groups")
if err != nil {
return nil, nil, err
}
q := u.Query()
q.Set("per_page", strconv.Itoa(opts.PerPage))
q.Set("page", strconv.Itoa(opts.Page))
q.Set("q", opts.Name)
u.RawQuery = q.Encode()
req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}
res := &GroupListResponse{}
resp, err := s.client.Do(req, res)
if err != nil {
return nil, resp, err
}
return res, resp, err
}
// Get Group
func (s *groupService) Get(id int) (*Group, *Response, error) {
u, err := url.Parse(fmt.Sprintf("/groups/%d", id))
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}
res := &Group{}
resp, err := s.client.Do(req, res)
if err != nil {
return nil, nil, err
}
return res, resp, err
}
// Create Group
func (s *groupService) Create(createRequest *GroupCreateRequest) (*Group, *Response, error) {
u, err := url.Parse("/groups")
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPost, u.String(), createRequest)
if err != nil {
return nil, nil, err
}
cResp := &Group{}
resp, err := s.client.Do(req, cResp)
if err != nil {
return nil, resp, err
}
return cResp, resp, err
}