-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistings.go
188 lines (148 loc) · 4.86 KB
/
listings.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package dev
import (
"context"
"fmt"
"github.com/google/go-querystring/query"
)
type Listing struct {
TypeOf string `json:"type_of"`
ID int64 `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
BodyMarkdown string `json:"body_markdown"`
TagList string `json:"tag_list"`
Tags []string `json:"tags"`
Category ListingCategory `json:"category"`
ProcessedHTML string `json:"processed_html"`
Published bool `json:"published"`
User *User `json:"user"`
Organization *Organization `json:"organization"`
}
type ListingBodySchema struct {
Listing struct {
Title string `json:"title"`
BodyMarkdown string `json:"body_markdown"`
Category ListingCategory `json:"category"`
Tags []string `json:"tags"`
TagList string `json:"tag_list"`
ExpiresAt string `json:"expires_at"`
ContactViaConnect bool `json:"contact_via_connect"`
Location string `json:"location"`
OrganizationID int64 `json:"organization_id"`
Action Action `json:"action"`
} `json:"listing"`
}
type ListingCategory string
const (
ListingCategoryCfp = ListingCategory("cfp")
ListingCategoryForhire = ListingCategory("forhire")
ListingCategoryCollabs = ListingCategory("collabs")
ListingCategoryEducation = ListingCategory("education")
ListingCategoryJobs = ListingCategory("jobs")
ListingCategoryMentors = ListingCategory("mentors")
ListingCategoryProducts = ListingCategory("products")
ListingCategoryMentees = ListingCategory("mentees")
ListingCategoryForsale = ListingCategory("forsale")
ListingCategoryEvents = ListingCategory("events")
ListingCategoryMisc = ListingCategory("misc")
)
type Action string
const (
Bump = Action("bump")
Publish = Action("publish")
Unpublish = Action("unpublish")
)
type ListingQueryParams struct {
Page int32 `url:"page"`
PerPage int32 `url:"per_page"`
Category string `url:"category"`
}
// GetPublishedListings allows the client retrieve a list of listings
func (c *Client) GetPublishedListings(q ListingQueryParams) ([]Listing, error) {
var listings []Listing
query, err := query.Values(q)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/listings?%s", query.Encode())
req, err := c.NewRequest(context.Background(), "GET", path, nil)
if err != nil {
return nil, err
}
if err := c.SendHttpRequest(req, &listings); err != nil {
return nil, err
}
return listings, nil
}
// CreateListing allows the client to create a listing.
// Listings are classified as ads that users create on DEV
func (c *Client) CreateListing(payload ListingBodySchema, filepath interface{}) (*Listing, error) {
path := "/listings"
if filepath != nil {
content, err := parseMarkdownFile(filepath.(string))
if err != nil {
return nil, err
}
payload.Listing.BodyMarkdown = content
}
req, err := c.NewRequest(context.Background(), "POST", path, payload)
if err != nil {
return nil, err
}
listing := new(Listing)
if err := c.SendHttpRequest(req, &listing); err != nil {
return nil, err
}
return listing, nil
}
// GetPublishedListingsByCategory allows the client to retrieve a list
// of listings belonging to the given category
func (c *Client) GetPublishedListingsByCategory(category string, q ListingQueryParams) ([]Listing, error) {
var listings []Listing
query, err := query.Values(q)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/listings/category/%s?%s", category, query.Encode())
req, err := c.NewRequest(context.Background(), "GET", path, nil)
if err != nil {
return nil, err
}
if err := c.SendHttpRequest(req, &listings); err != nil {
return nil, err
}
return listings, nil
}
// GetListingByID allows the client to retrieve a single listing given
// its Id
func (c *Client) GetListingByID(listingID string) (*Listing, error) {
path := fmt.Sprintf("/listings/%s", listingID)
req, err := c.NewRequest(context.Background(), "GET", path, nil)
if err != nil {
return nil, err
}
listing := new(Listing)
if err := c.SendHttpRequest(req, &listing); err != nil {
return nil, err
}
return listing, nil
}
func (c *Client) UpdateListing(listingID string, payload ListingBodySchema, filepath interface{}) (*Listing, error) {
path := fmt.Sprintf("/listings/%s", listingID)
if filepath != nil {
content, err := parseMarkdownFile(filepath.(string))
if err != nil {
return nil, err
}
payload.Listing.BodyMarkdown = content
}
req, err := c.NewRequest(context.Background(), "PUT", path, payload)
if err != nil {
return nil, err
}
listing := new(Listing)
if err := c.SendHttpRequest(req, &listing); err != nil {
return nil, err
}
return listing, nil
}