-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.go
82 lines (64 loc) · 1.81 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
package docbase
import (
"fmt"
"net/http"
"net/url"
"time"
)
// CommentService implements interface with API /groups endpoint.
// https://help.docbase.io/posts/45703#%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88
type CommentService interface {
Create(postID int, commentRequest *CommentCreateRequest) (*Comment, *Response, error)
Delete(commentID int) (*Response, error)
}
// commentService handles communication with API
type commentService struct {
client *Client
}
// Comment represents a docbase Comment
type Comment struct {
ID int `json:"id"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
SimpleUser `json:"user"`
}
// CommentCreateRequest identifies Comment for the Create request
type CommentCreateRequest struct {
Body string `json:"body"`
Notice bool `json:"notice,omitempty"`
AuthorID string `json:"author_id,omitempty"`
PublishedAt time.Time `json:"published_at,omitempty"`
}
// Create Comment
func (s *commentService) Create(postID int, commentRequest *CommentCreateRequest) (*Comment, *Response, error) {
u, err := url.Parse(fmt.Sprintf("/posts/%d/comments", postID))
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodPost, u.String(), commentRequest)
if err != nil {
return nil, nil, err
}
cResp := &Comment{}
resp, err := s.client.Do(req, cResp)
if err != nil {
return nil, resp, err
}
return cResp, resp, err
}
// Delete Comment
func (s *commentService) Delete(commentID int) (*Response, error) {
u, err := url.Parse(fmt.Sprintf("/comments/%d", commentID))
if err != nil {
return nil, err
}
req, err := s.client.NewRequest(http.MethodDelete, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.client.Do(req, nil)
if err != nil {
return resp, err
}
return resp, err
}