This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle.go
156 lines (131 loc) · 4.2 KB
/
article.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
package strichliste
import (
"fmt"
"github.com/jktr/go-strichliste/schema"
"net/http"
)
// An ArticleClient carries the necessary context to interact
// with the /article endpoint
type ArticleClient struct {
client *Client
}
// POST /article
// - ErrorParameterMissing
// - ErrorParameterInvalid
//
// Creates a new article and returns it.
func (s *ArticleClient) Create(article *schema.ArticleCreateRequest) (*schema.Article, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, schema.EndpointArticle, article)
if err != nil {
return nil, nil, err
}
var body schema.SingleArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return &body.Article, resp, nil
}
// GET /article/{articleId}
//
// Retrieves an article by ID.
func (s *ArticleClient) Get(id int) (*schema.Article, *Response, error) {
path := fmt.Sprintf("%s/%d", schema.EndpointArticle, id)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var body schema.SingleArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return &body.Article, resp, nil
}
// GET /article
//
// Retrieves a list of articles (both active and inactive).
// Pagination is possible via ListOpts, which can be nil.
func (s *ArticleClient) List(opt *ListOpts) ([]schema.Article, *Response, error) {
path := fmt.Sprintf("%s?%s", schema.EndpointArticle, opt.values().Encode())
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var body schema.MultiArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return body.Articles, resp, nil
}
func (s *ArticleClient) searchByX(x, query string, opt *ListOpts) ([]schema.Article, *Response, error) {
v := opt.values()
v.Add(x, query)
path := fmt.Sprintf("%s?%s", schema.EndpointArticleSearch, v.Encode())
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var body schema.MultiArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return body.Articles, resp, nil
}
// GET /article/search
//
// Retrieves a list of articles whose names match, or include, the
// passed name. Pagination is possible via ListOpts, which can be nil.
func (s *ArticleClient) SearchByName(name string, opt *ListOpts) ([]schema.Article, *Response, error) {
return s.searchByX("query", name, opt)
}
// GET /article/search
//
// Retrieves a list of articles whose barcodes match, or include, the
// passed barcode. Pagination is possible via ListOpts, which can be nil.
func (s *ArticleClient) SearchByBarcode(barcode string, opt *ListOpts) ([]schema.Article, *Response, error) {
return s.searchByX("barcode", string(barcode), opt)
}
// POST /article/{articleId}
// - ErrorArticleNotFound
// - ErrorParameterMissing
// - ErrorParameterInvalid
//
// Updates an article by ID. Note that this operation checks for
// referential integrity and may not update the article, but instead
// create a new one, referencing and deactivating the old version.
// The returned article is always new version — either replaced or
// updated.
func (s *ArticleClient) Update(id int, article *schema.ArticleUpdateRequest) (*schema.Article, *Response, error) {
path := fmt.Sprintf("%s/%d", schema.EndpointArticle, id)
req, err := s.client.NewRequest(http.MethodPost, path, article)
if err != nil {
return nil, nil, err
}
var body schema.SingleArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return &body.Article, resp, nil
}
// DELETE /article/{articleId}
// - ErrorArticleNotFound
//
// Deactivates an article by ID; returns the deactivated article.
// Note that actual deletion is not possible.
func (s *ArticleClient) Deactivate(id int) (*schema.Article, *Response, error) {
path := fmt.Sprintf("%s/%d", schema.EndpointArticle, id)
req, err := s.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, nil, err
}
var body schema.SingleArticleResponse
resp, err := s.client.Do(req, &body)
if err != nil {
return nil, resp, err
}
return &body.Article, resp, nil
}