-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
267 lines (226 loc) · 8.83 KB
/
Copy pathmanager.go
File metadata and controls
267 lines (226 loc) · 8.83 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package endpoints
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/maniac-en/req/internal/backend/crud"
"github.com/maniac-en/req/internal/backend/database"
"github.com/maniac-en/req/internal/log"
)
func NewEndpointsManager(db *database.Queries) *EndpointsManager {
return &EndpointsManager{DB: db}
}
func (e *EndpointsManager) Create(ctx context.Context, name string) (EndpointEntity, error) {
return EndpointEntity{}, fmt.Errorf("use CreateEndpoint to create an endpoint with full data")
}
func (e *EndpointsManager) Read(ctx context.Context, id int64) (EndpointEntity, error) {
if err := crud.ValidateID(id); err != nil {
log.Warn("endpoint read failed validation", "id", id)
return EndpointEntity{}, crud.ErrInvalidInput
}
log.Debug("reading endpoint", "id", id)
endpoint, err := e.DB.GetEndpoint(ctx, id)
if err != nil {
if err == sql.ErrNoRows {
log.Debug("endpoint not found", "id", id)
return EndpointEntity{}, crud.ErrNotFound
}
log.Error("failed to read endpoint", "id", id, "error", err)
return EndpointEntity{}, err
}
return EndpointEntity{Endpoint: endpoint}, nil
}
func (e *EndpointsManager) Update(ctx context.Context, id int64, name string) (EndpointEntity, error) {
return EndpointEntity{}, fmt.Errorf("use UpdateEndpoint to update an endpoint with full data")
}
func (e *EndpointsManager) Delete(ctx context.Context, id int64) error {
if err := crud.ValidateID(id); err != nil {
log.Warn("endpoint delete failed validation", "id", id)
return crud.ErrInvalidInput
}
log.Debug("deleting endpoint", "id", id)
err := e.DB.DeleteEndpoint(ctx, id)
if err != nil {
log.Error("failed to delete endpoint", "id", id, "error", err)
return err
}
log.Info("deleted endpoint", "id", id)
return nil
}
func (e *EndpointsManager) List(ctx context.Context) ([]EndpointEntity, error) {
return nil, fmt.Errorf("use ListByCollection to list endpoints for a specific collection")
}
func (e *EndpointsManager) ListByCollectionByPage(ctx context.Context, collectionID int64, limit, offset int) (*PaginatedEndpoints, error) {
if err := crud.ValidateID(collectionID); err != nil {
log.Warn("endpoint list failed collection validation", "collection_id", collectionID)
return nil, crud.ErrInvalidInput
}
log.Debug("listing paginated endpoints", "collection_id", collectionID, "limit", limit, "offset", offset)
total, err := e.DB.CountEndpointsByCollection(ctx, collectionID)
if err != nil {
log.Error("failed to count endpoints", "collection_id", collectionID, "error", err)
return nil, err
}
endpoints, err := e.DB.ListEndpointsPaginated(ctx, database.ListEndpointsPaginatedParams{
CollectionID: collectionID,
Limit: int64(limit),
Offset: int64(offset),
})
if err != nil {
log.Error("failed to get paginated endpoints", "collection_id", collectionID, "limit", limit, "offset", offset, "error", err)
return nil, err
}
entities := make([]EndpointEntity, len(endpoints))
for i, endpoint := range endpoints {
entities[i] = EndpointEntity{Endpoint: endpoint}
}
pagination := crud.CalculatePagination(total, limit, offset)
result := &PaginatedEndpoints{
Endpoints: entities,
PaginationMetadata: pagination,
}
log.Info("retrieved endpoints", "collection_id", collectionID, "count", len(entities), "total", pagination.Total, "page", pagination.CurrentPage, "total_pages", pagination.TotalPages)
return result, nil
}
func (e *EndpointsManager) ListByCollection(ctx context.Context, collectionID int64) ([]EndpointEntity, error) {
if err := crud.ValidateID(collectionID); err != nil {
log.Warn("endpoint list failed collection validation", "collection_id", collectionID)
return nil, crud.ErrInvalidInput
}
log.Debug("listing endpoints", "collection_id", collectionID, "limit")
endpoints, err := e.DB.ListEndpointsByCollection(context.Background(), collectionID)
if err != nil && err != sql.ErrNoRows {
log.Warn("error occured while fetching endpoints", "collection_id", collectionID)
return nil, err
}
entities := make([]EndpointEntity, len(endpoints))
for i, endpoint := range endpoints {
entities[i] = EndpointEntity{Endpoint: endpoint}
}
log.Info("retrieved endpoints", "collection_id", collectionID, "count")
return entities, nil
}
func (e *EndpointsManager) CreateEndpoint(ctx context.Context, data EndpointData) (EndpointEntity, error) {
if err := crud.ValidateID(data.CollectionID); err != nil {
log.Warn("endpoint creation failed collection validation", "collection_id", data.CollectionID)
return EndpointEntity{}, crud.ErrInvalidInput
}
if err := crud.ValidateName(data.Name); err != nil {
log.Warn("endpoint creation failed name validation", "name", data.Name)
return EndpointEntity{}, crud.ErrInvalidInput
}
if data.Method == "" {
log.Warn("endpoint creation failed - method required", "method", data.Method)
return EndpointEntity{}, crud.ErrInvalidInput
}
headersJSON := data.Headers
if headersJSON == "" {
headersJSON = "{}"
}
queryParamsJSON := "{}"
if len(data.QueryParams) > 0 {
qpBytes, err := json.Marshal(data.QueryParams)
if err != nil {
log.Error("failed to marshal query params", "error", err)
return EndpointEntity{}, err
}
queryParamsJSON = string(qpBytes)
}
log.Debug("creating endpoint", "collection_id", data.CollectionID, "name", data.Name, "method", data.Method, "url", data.URL)
endpoint, err := e.DB.CreateEndpoint(ctx, database.CreateEndpointParams{
CollectionID: data.CollectionID,
Name: data.Name,
Method: data.Method,
Url: data.URL,
Headers: headersJSON,
QueryParams: queryParamsJSON,
RequestBody: data.RequestBody,
})
if err != nil {
log.Error("failed to create endpoint", "collection_id", data.CollectionID, "name", data.Name, "error", err)
return EndpointEntity{}, err
}
log.Info("created endpoint", "id", endpoint.ID, "name", endpoint.Name, "collection_id", endpoint.CollectionID)
return EndpointEntity{Endpoint: endpoint}, nil
}
func (e *EndpointsManager) UpdateEndpointName(ctx context.Context, id int64, name string) (EndpointEntity, error) {
if err := crud.ValidateID(id); err != nil {
log.Warn("endpoint update failed ID validation", "id", id)
return EndpointEntity{}, crud.ErrInvalidInput
}
if err := crud.ValidateName(name); err != nil {
log.Warn("endpoint update failed name validation", "name", name)
return EndpointEntity{}, crud.ErrInvalidInput
}
log.Debug("updating endpoint name", "id", id, "name", name)
endpoint, err := e.DB.UpdateEndpointName(ctx, database.UpdateEndpointNameParams{
Name: name,
ID: id,
})
if err != nil {
if err == sql.ErrNoRows {
log.Debug("endpoint not found for update", "id", id)
return EndpointEntity{}, crud.ErrNotFound
}
log.Error("failed to update endpoint", "id", id, "name", name, "error", err)
return EndpointEntity{}, err
}
log.Info("updated endpoint", "id", endpoint.ID, "name", endpoint.Name)
return EndpointEntity{Endpoint: endpoint}, nil
}
func (e *EndpointsManager) UpdateEndpoint(ctx context.Context, id int64, data EndpointData) (EndpointEntity, error) {
if err := crud.ValidateID(id); err != nil {
log.Warn("endpoint update failed ID validation", "id", id)
return EndpointEntity{}, crud.ErrInvalidInput
}
if err := crud.ValidateName(data.Name); err != nil {
log.Warn("endpoint update failed name validation", "name", data.Name)
return EndpointEntity{}, crud.ErrInvalidInput
}
if data.Method == "" {
log.Warn("endpoint update failed - method required", "method", data.Method)
return EndpointEntity{}, crud.ErrInvalidInput
}
headersJSON := data.Headers
if headersJSON == "" {
headersJSON = "{}"
}
queryParamsJSON := "{}"
if len(data.QueryParams) > 0 {
qpBytes, err := json.Marshal(data.QueryParams)
if err != nil {
log.Error("failed to marshal query params", "error", err)
return EndpointEntity{}, err
}
queryParamsJSON = string(qpBytes)
}
log.Debug("updating endpoint", "id", id, "name", data.Name, "method", data.Method, "url", data.URL)
endpoint, err := e.DB.UpdateEndpoint(ctx, database.UpdateEndpointParams{
Name: data.Name,
Method: data.Method,
Url: data.URL,
Headers: headersJSON,
QueryParams: queryParamsJSON,
RequestBody: data.RequestBody,
ID: id,
})
if err != nil {
if err == sql.ErrNoRows {
log.Debug("endpoint not found for update", "id", id)
return EndpointEntity{}, crud.ErrNotFound
}
log.Error("failed to update endpoint", "id", id, "name", data.Name, "error", err)
return EndpointEntity{}, err
}
log.Info("updated endpoint", "id", endpoint.ID, "name", endpoint.Name)
return EndpointEntity{Endpoint: endpoint}, nil
}
func (e *EndpointsManager) GetCountsByCollections(ctx context.Context) ([]database.GetEndpointCountsByCollectionsRow, error) {
counts, err := e.DB.GetEndpointCountsByCollections(ctx)
if err != nil {
log.Error("failed to get endpoint counts", "error", err)
return nil, err
}
return counts, nil
}