-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.go
91 lines (71 loc) · 2.21 KB
/
collections.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
package wdbgo
import (
"encoding/json"
"fmt"
"github.com/TanmoySG/wdb-go/internal/methods"
"github.com/TanmoySG/wdb-go/internal/routes"
"github.com/TanmoySG/wdb-go/schema"
requestModels "github.com/TanmoySG/wdb-go/requestModels"
wdbModels "github.com/TanmoySG/wunderDB/model"
)
func (wdb wdbClient) CreateCollection(databaseName, collectionName string, schema schema.CollectionSchema) error {
queryEndpoint := routes.CreateCollection.Format(wdb.ConnectionURI, databaseName).String()
queryMethod := methods.CreateCollection.String()
queryPayload := requestModels.CreateCollection{
Name: collectionName,
Schema: schema,
}
_, queryResponse, err := wdb.QueryClient.Query(queryEndpoint, queryMethod, queryPayload)
if err != nil {
return err
}
apiResponse, err := queryResponse.ApiResponse()
if err != nil {
return err
}
if apiResponse.IsSuccess() {
return nil
}
return fmt.Errorf(apiResponse.Error.Code)
}
func (wdb wdbClient) GetCollection(databaseName, collectionName string) (*wdbModels.Collection, error) {
queryEndpoint := routes.FetchCollection.Format(wdb.ConnectionURI, databaseName, collectionName).String()
queryMethod := methods.FetchCollection.String()
_, queryResponse, err := wdb.QueryClient.Query(queryEndpoint, queryMethod, nil)
if err != nil {
return nil, err
}
apiResponse, err := queryResponse.ApiResponse()
if err != nil {
return nil, err
}
if apiResponse.IsSuccess() {
var collection wdbModels.Collection
dataBytes, err := apiResponse.MarshalData()
if err != nil {
return nil, err
}
err = json.Unmarshal(dataBytes, &collection)
if err != nil {
return nil, err
}
return &collection, nil
}
return nil, fmt.Errorf(apiResponse.Error.Code)
}
func (wdb wdbClient) DeleteCollection(databaseName, collectionName string) error {
queryEndpoint := routes.DeleteCollection.Format(wdb.ConnectionURI, databaseName, collectionName).String()
queryMethod := methods.DeleteCollection.String()
_, queryResponse, err := wdb.QueryClient.Query(queryEndpoint, queryMethod, nil)
if err != nil {
return err
}
apiResponse, err := queryResponse.ApiResponse()
if err != nil {
return err
}
if apiResponse.IsSuccess() {
return nil
}
return fmt.Errorf(apiResponse.Error.Code)
}