-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_root.go
170 lines (139 loc) · 4.47 KB
/
api_root.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
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
package sqlite
import (
"context"
"database/sql"
"strings"
// import sqlite dependency
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/pladdy/cabby"
)
// APIRootService implements a SQLite version of the APIRootService interface
type APIRootService struct {
DB *sql.DB
DataStore *DataStore
}
// APIRoot will read from the data store and return the resource
func (s APIRootService) APIRoot(ctx context.Context, path string) (cabby.APIRoot, error) {
resource, action := "APIRoot", "read"
start := cabby.LogServiceStart(ctx, resource, action)
result, err := s.apiRoot(path)
cabby.LogServiceEnd(ctx, resource, action, start)
return result, err
}
func (s APIRootService) apiRoot(path string) (cabby.APIRoot, error) {
sql := `select api_root_path, title, description, versions, max_content_length
from api_root
where api_root_path = ?`
args := []interface{}{path}
a := cabby.APIRoot{}
rows, err := s.DB.Query(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return a, err
}
defer rows.Close()
for rows.Next() {
var versions string
if err := rows.Scan(&a.Path, &a.Title, &a.Description, &versions, &a.MaxContentLength); err != nil {
return a, err
}
a.Versions = strings.Split(versions, ",")
}
err = rows.Err()
return a, err
}
// APIRoots will read from the data store and return the resource
func (s APIRootService) APIRoots(ctx context.Context) ([]cabby.APIRoot, error) {
resource, action := "APIRoots", "read"
start := cabby.LogServiceStart(ctx, resource, action)
result, err := s.apiRoots()
cabby.LogServiceEnd(ctx, resource, action, start)
return result, err
}
func (s APIRootService) apiRoots() ([]cabby.APIRoot, error) {
sql := `select api_root_path, title, description, versions, max_content_length
from api_root`
args := []interface{}{}
as := []cabby.APIRoot{}
rows, err := s.DB.Query(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return as, err
}
for rows.Next() {
var a cabby.APIRoot
var versions string
if err := rows.Scan(&a.Path, &a.Title, &a.Description, &versions, &a.MaxContentLength); err != nil {
return as, err
}
a.Versions = strings.Split(versions, ",")
as = append(as, a)
}
err = rows.Err()
return as, err
}
// CreateAPIRoot creates a user in the data store
func (s APIRootService) CreateAPIRoot(ctx context.Context, a cabby.APIRoot) error {
resource, action := "APIRoot", "create"
start := cabby.LogServiceStart(ctx, resource, action)
err := a.Validate()
if err == nil {
err = s.createAPIRoot(a)
} else {
log.WithFields(log.Fields{"api_root": a, "error": err}).Error("Invalid API Root")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s APIRootService) createAPIRoot(a cabby.APIRoot) error {
sql := `insert into api_root (api_root_path, title, description, versions, max_content_length)
values (?, ?, ?, ?, ?)`
args := []interface{}{a.Path, a.Title, a.Description, strings.Join(a.Versions, ","), a.MaxContentLength}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// DeleteAPIRoot creates a user in the data store
func (s APIRootService) DeleteAPIRoot(ctx context.Context, id string) error {
resource, action := "APIRoot", "delete"
start := cabby.LogServiceStart(ctx, resource, action)
err := s.deleteAPIRoot(id)
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s APIRootService) deleteAPIRoot(path string) error {
sql := `delete from api_root where api_root_path = ?`
args := []interface{}{path}
_, err := s.DB.Exec(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// UpdateAPIRoot creates a user in the data store
func (s APIRootService) UpdateAPIRoot(ctx context.Context, a cabby.APIRoot) error {
resource, action := "APIRoot", "update"
start := cabby.LogServiceStart(ctx, resource, action)
err := a.Validate()
if err == nil {
err = s.updateAPIRoot(a)
} else {
log.WithFields(log.Fields{"api_root": a, "error": err}).Error("Invalid API Root")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s APIRootService) updateAPIRoot(a cabby.APIRoot) error {
sql := `update api_root
set title = ?, description = ?, versions = ?, max_content_length = ?
where api_root_path = ?`
args := []interface{}{a.Title, a.Description, strings.Join(a.Versions, ","), a.MaxContentLength, a.Path}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}