forked from sourcegraph/thesrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.go
153 lines (119 loc) · 3.11 KB
/
posts.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
153
package thesrc
import (
"errors"
"net/http"
"strconv"
"time"
"github.com/sourcegraph/thesrc/router"
)
// A Post is a link and short body submitted to and displayed on thesrc.
type Post struct {
// ID a unique identifier for this post.
ID int `json:",omitempty"`
// Title of the post.
Title string
// LinkURL is the URL to a link that this post is about.
LinkURL string
// Body of the post.
Body string
// SubmittedAt is when the post was submitted.
SubmittedAt time.Time
// AuthorUserID is the user ID of this post's author.
AuthorUserID int
// Score in points.
Score int
// Classification is the output of the classifier on this post.
Classification string
}
// PostsService interacts with the post-related endpoints in thesrc's API.
type PostsService interface {
// Get a post.
Get(id int) (*Post, error)
// List posts.
List(opt *PostListOptions) ([]*Post, error)
// Submit a post. If this post's link URL has never been submitted, post.ID
// will be a new ID, and created will be true. If it has been submitted
// before, post.ID will be the ID of the previous post, and created will be
// false.
Submit(post *Post) (created bool, err error)
}
var (
ErrPostNotFound = errors.New("post not found")
)
type postsService struct{ client *Client }
func (s *postsService) Get(id int) (*Post, error) {
url, err := s.client.url(router.Post, map[string]string{"ID": strconv.Itoa(id)}, nil)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
var post *Post
_, err = s.client.Do(req, &post)
if err != nil {
return nil, err
}
return post, nil
}
type PostListOptions struct {
// CodeOnly filters the result set to only those posts whose links contain code.
CodeOnly bool
ListOptions
}
func (s *postsService) List(opt *PostListOptions) ([]*Post, error) {
url, err := s.client.url(router.Posts, nil, opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
var posts []*Post
_, err = s.client.Do(req, &posts)
if err != nil {
return nil, err
}
return posts, nil
}
func (s *postsService) Submit(post *Post) (bool, error) {
url, err := s.client.url(router.SubmitPost, nil, nil)
if err != nil {
return false, err
}
req, err := s.client.NewRequest("POST", url.String(), post)
if err != nil {
return false, err
}
resp, err := s.client.Do(req, &post)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusCreated, nil
}
type MockPostsService struct {
Get_ func(id int) (*Post, error)
List_ func(opt *PostListOptions) ([]*Post, error)
Submit_ func(post *Post) (bool, error)
}
var _ PostsService = &MockPostsService{}
func (s *MockPostsService) Get(id int) (*Post, error) {
if s.Get_ == nil {
return nil, nil
}
return s.Get_(id)
}
func (s *MockPostsService) List(opt *PostListOptions) ([]*Post, error) {
if s.List_ == nil {
return nil, nil
}
return s.List_(opt)
}
func (s *MockPostsService) Submit(post *Post) (bool, error) {
if s.Submit_ == nil {
return false, nil
}
return s.Submit_(post)
}