-
Notifications
You must be signed in to change notification settings - Fork 4
/
comment.go
121 lines (108 loc) · 2.95 KB
/
comment.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
package ganboard
import "encoding/json"
// CreateComment https://docs.kanboard.org/en/latest/api/comment_procedures.html#createcomment
func (c *Client) CreateComment(taskID int, userID int, content string) (int, error) {
query := request{
Client: c,
Method: "createComment",
Params: struct {
TaskID int `json:"task_id,string"`
UserID int `json:"user_id,string"`
Content string `json:"content"`
}{
TaskID: taskID,
UserID: userID,
Content: content,
},
}
response, err := query.decodeInt()
return response, err
}
// GetComment https://docs.kanboard.org/en/latest/api/comment_procedures.html#getcomment
func (c *Client) GetComment(commentID int) (Comment, error) {
query := request{
Client: c,
Method: "getComment",
Params: map[string]int{
"comment_id": commentID,
},
}
response, err := query.decodeComment()
return response, err
}
// GetAllComments https://docs.kanboard.org/en/latest/api/comment_procedures.html#getallcomments
func (c *Client) GetAllComments(taskID int) ([]Comment, error) {
query := request{
Client: c,
Method: "getAllComments",
Params: map[string]int{
"task_id": taskID,
},
}
response, err := query.decodeComments()
return response, err
}
// UpdateComment https://docs.kanboard.org/en/latest/api/comment_procedures.html#updatecomment
func (c *Client) UpdateComment(commentID int, content string) (bool, error) {
query := request{
Client: c,
Method: "updateComment",
Params: struct {
CommentID int `json:"id,string"`
Content string `json:"content"`
}{
CommentID: commentID,
Content: content,
},
}
response, err := query.decodeBoolean()
return response, err
}
// RemoveComment https://docs.kanboard.org/en/latest/api/comment_procedures.html#removecomment
func (c *Client) RemoveComment(commentID int) (bool, error) {
query := request{
Client: c,
Method: "updateComment",
Params: map[string]int{
"comment_id": commentID,
},
}
response, err := query.decodeBoolean()
return response, err
}
// Comment type
type Comment struct {
ID int `json:"id,string"`
TaskID int `json:"task_id,string"`
UserID int `json:"user_id,string"`
DateCreation int `json:"date_creation,string"`
Comment string `json:"comment"`
Username string `json:"username"`
Name string `json:"name"`
}
func (r *request) decodeComments() ([]Comment, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return nil, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result []Comment `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}
func (r *request) decodeComment() (Comment, error) {
rsp, err := r.Client.Request(*r)
if err != nil {
return Comment{}, err
}
body := struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result Comment `json:"result"`
}{}
err = json.NewDecoder(rsp.Body).Decode(&body)
return body.Result, err
}