forked from r--w/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcollection.go
114 lines (92 loc) · 2.88 KB
/
collection.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
package pocketbase
import (
"encoding/json"
"fmt"
"net/url"
)
type Collection[T any] struct {
*Client
Name string
BaseCollectionPath string
}
func CollectionSet[T any](client *Client, collection string) *Collection[T] {
return &Collection[T]{
Client: client,
Name: collection,
BaseCollectionPath: client.url + "/api/collections/" + url.QueryEscape(collection),
}
}
func (c *Collection[T]) Update(id string, body T) error {
return c.Client.Update(c.Name, id, body)
}
func (c *Collection[T]) Create(body T) (ResponseCreate, error) {
return c.Client.Create(c.Name, body)
}
func (c *Collection[T]) Delete(id string) error {
return c.Client.Delete(c.Name, id)
}
func (c *Collection[T]) List(params ParamsList) (ResponseList[T], error) {
var response ResponseList[T]
params.hackResponseRef = &response
_, err := c.Client.List(c.Name, params)
return response, err
}
func (c *Collection[T]) FullList(params ParamsList) (ResponseList[T], error) {
var response ResponseList[T]
params.hackResponseRef = &response
_, err := c.Client.FullList(c.Name, params)
return response, err
}
func (c *Collection[T]) One(id string) (T, error) {
var response T
if err := c.Authorize(); err != nil {
return response, err
}
request := c.client.R().
SetHeader("Content-Type", "application/json").
SetPathParam("collection", c.Name).
SetPathParam("id", id)
resp, err := request.Get(c.url + "/api/collections/{collection}/records/{id}")
if err != nil {
return response, fmt.Errorf("[one] can't send update request to pocketbase, err %w", err)
}
if resp.IsError() {
return response, fmt.Errorf("[one] pocketbase returned status: %d, msg: %s, err %w",
resp.StatusCode(),
resp.String(),
ErrInvalidResponse,
)
}
if err := json.Unmarshal(resp.Body(), &response); err != nil {
return response, fmt.Errorf("[one] can't unmarshal response, err %w", err)
}
return response, nil
}
// Get one record with params (only fields and expand supported)
func (c *Collection[T]) OneWithParams(id string, params ParamsList) (T, error) {
var response T
if err := c.Authorize(); err != nil {
return response, err
}
request := c.client.R().
SetHeader("Content-Type", "application/json").
SetPathParam("collection", c.Name).
SetPathParam("id", id).
SetQueryParam("fields", params.Fields).
SetQueryParam("expand", params.Expand)
resp, err := request.Get(c.url + "/api/collections/{collection}/records/{id}")
if err != nil {
return response, fmt.Errorf("[one] can't send update request to pocketbase, err %w", err)
}
if resp.IsError() {
return response, fmt.Errorf("[one] pocketbase returned status: %d, msg: %s, err %w",
resp.StatusCode(),
resp.String(),
ErrInvalidResponse,
)
}
if err := json.Unmarshal(resp.Body(), &response); err != nil {
return response, fmt.Errorf("[one] can't unmarshal response, err %w", err)
}
return response, nil
}