-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocs.go
129 lines (98 loc) · 3.03 KB
/
docs.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
package guildedgo
import (
"errors"
"net/url"
"strconv"
)
type Doc struct {
ID int `json:"id"`
ServerID string `json:"serverId"`
ChannelID string `json:"channelId"`
Title string `json:"title"`
Content string `json:"content"`
Mentions `json:"mentions,omitempty"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
UpdatedAt string `json:"updatedAt,omitempty"`
UpdatedBy string `json:"updatedBy,omitempty"`
}
type DocObject struct {
title string `json:"title"`
content string `json:"content"`
}
type DocResponse struct {
Doc `json:"doc"`
}
type DocsResponse struct {
Docs []Doc `json:"docs"`
}
type docsEndpoints struct{}
func (e *docsEndpoints) Default(channelId string) string {
return guildedApi + "/channels/" + channelId + "/docs"
}
func (e *docsEndpoints) Get(channelId string, docId int) string {
return guildedApi + "/channels/" + channelId + "/docs/" + strconv.Itoa(docId)
}
type docsService struct {
client *Client
endpoints *docsEndpoints
}
type DocsService interface {
Create(channelId string) (*Doc, error)
}
var _ DocsService = &docsService{}
func (service *docsService) Create(channelId string) (*Doc, error) {
var docResponse DocResponse
endpoint := service.endpoints.Default(channelId)
err := service.client.PostRequestV2("POST", endpoint, nil)
if err != nil {
return nil, errors.New("Error creating doc. Error: " + err.Error())
}
return &docResponse.Doc, nil
}
func (service *docsService) GetDocs(channelId string, doc *DocObject) ([]Doc, error) {
var docsResponse DocsResponse
url, err := url.Parse(service.endpoints.Default(channelId))
if err != nil {
return nil, errors.New("Error parsing URL. Error: " + err.Error())
}
query := url.Query()
if doc.title != "" {
query.Add("title", doc.title)
}
if doc.content != "" {
query.Add("content", doc.content)
}
url.RawQuery = query.Encode()
err = service.client.GetRequestV2(url.String(), &docsResponse)
if err != nil {
return nil, errors.New("Error getting docs. Error: " + err.Error())
}
return docsResponse.Docs, nil
}
func (service *docsService) GetDoc(channelId string, docId int) (*Doc, error) {
var docResponse DocResponse
endpoint := service.endpoints.Get(channelId, docId)
err := service.client.GetRequestV2(endpoint, &docResponse)
if err != nil {
return nil, errors.New("Error getting doc. Error: " + err.Error())
}
return &docResponse.Doc, nil
}
func (service *docsService) UpdateDoc(channelId string, docId int, doc *DocObject) (*Doc, error) {
var docResponse DocResponse
endpoint := service.endpoints.Get(channelId, docId)
err := service.client.PutRequestV2(endpoint, &doc, &docResponse)
if err != nil {
return nil, errors.New("Error updating doc. Error: " + err.Error())
}
return &docResponse.Doc, nil
}
func (service *docsService) DeleteDoc(channelId string, docId int) error {
endpoint := service.endpoints.Get(channelId, docId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New("Error deleting doc. Error: " + err.Error())
}
return nil
}