-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrud.go
More file actions
112 lines (95 loc) · 2.93 KB
/
crud.go
File metadata and controls
112 lines (95 loc) · 2.93 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
package helpers
import (
"context"
"fmt"
"github.com/fatih/structs"
scAPI "github.com/spaceuptech/space-api-go"
"github.com/spaceuptech/space-api-go/db"
"net/http"
"os"
"github.com/spaceuptech/space-api-go/types"
)
type Crud struct {
DbClient *db.DB
}
const (
scEnvKey = "SPACE_CLOUD_ADDR"
scToken = "SPACE_CLOUD_TOKEN"
)
func InitCrud() (*Crud, error) {
value := os.Getenv(scEnvKey)
if value == "" {
return nil, fmt.Errorf("couldn't find env variable %s", scEnvKey)
}
// Create space api object
a := scAPI.New("spacecloud", value, false)
value = os.Getenv(scToken)
if value == "" {
return nil, fmt.Errorf("couldn't find env variable %s", scToken)
}
a.SetToken(value)
c := a.DB("db")
if c == nil {
return nil, fmt.Errorf("unable to initialize space api go, check if space cloud is running")
}
return &Crud{DbClient: c}, nil
}
func (m *Crud) Insert(ctx context.Context, tableName string, obj interface{}) error {
_, err := m.CheckErrors(m.DbClient.Insert(tableName).Doc(obj).Apply(ctx))
if err != nil {
return err
}
return nil
}
func (m *Crud) Upsert(ctx context.Context, tableName string, whereClause types.M, obj interface{}) error {
_, err := m.CheckErrors(m.DbClient.Upsert(tableName).Where(whereClause).Set(structs.Map(obj)).Apply(ctx))
if err != nil {
return err
}
return nil
}
func (m *Crud) Update(ctx context.Context, tableName string, whereClause types.M, obj interface{}) error {
_, err := m.CheckErrors(m.DbClient.Update(tableName).Where(whereClause).Set(structs.Map(obj)).Apply(ctx))
if err != nil {
return err
}
return nil
}
func (m *Crud) GetOne(ctx context.Context, tableName string, whereClause types.M, response interface{}) error {
result, err := m.CheckErrors(m.DbClient.GetOne(tableName).Where(whereClause).Apply(ctx))
if err != nil {
return err
}
if err := result.Unmarshal(response); err != nil {
return fmt.Errorf("unable to un-marshal database response (%v)", err)
}
return nil
}
func (m *Crud) GetAll(ctx context.Context, tableName string, whereClause types.M, response interface{}) error {
result, err := m.CheckErrors(m.DbClient.Get(tableName).Where(whereClause).Apply(ctx))
if err != nil {
return err
}
if err := result.Unmarshal(response); err != nil {
return fmt.Errorf("unable to un-marshal database response (%v)", err)
}
return nil
}
func (m *Crud) Delete(ctx context.Context, tableName string, whereClause types.M) error {
_, err := m.CheckErrors(m.DbClient.DeleteOne(tableName).Where(whereClause).Apply(ctx))
if err != nil {
return err
}
return nil
}
func (m *Crud) CheckErrors(result *types.Response, err error) (*types.Response, error) {
if err != nil {
// Network error
return nil, fmt.Errorf("network error occurred while querying database (%v)", err)
}
if result.Status != http.StatusOK || result.Error != "" {
// Query processing error
return nil, fmt.Errorf("invalid status code (%d) received - (%v)", result.Status, result.Error)
}
return result, nil
}