-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.go
68 lines (51 loc) · 1.16 KB
/
record.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
package pocketclient
import "github.com/avila-r/pocketclient/validation"
func Insert(collection string, data any) error {
res, err := RequestPostRecord(collection, data)
if err != nil {
return err
}
if err := validation.VerifyResponse(res); err != nil {
return err
}
if err := Unmarshal(res.Body(), &data); err != nil {
return err
}
return nil
}
func List[T any](collection string, p ...PaginationParams) (*Pagination[T], error) {
return Collection[T](collection).
List(p...)
}
type Query struct {
Collection string
ID string
}
func Find[T any](q Query) (*T, error) {
return Collection[T](q.Collection).
Find(q.ID)
}
func FindIn[T any](collection string, id string) (*T, error) {
return Collection[T](collection).
Find(id)
}
func Fetch(q Query, to any) error {
res, err := RequestGetRecord(q.Collection, q.ID)
if err != nil {
return err
}
if err := validation.VerifyResponse(res); err != nil {
return err
}
if err := Unmarshal(res.Body(), &to); err != nil {
return err
}
return nil
}
func FetchIn(collection, id string, to any) error {
query := Query{
Collection: collection,
ID: id,
}
return Fetch(query, &to)
}