-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
138 lines (115 loc) · 3.62 KB
/
discovery.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
package sqlite
import (
"context"
"database/sql"
// import sqlite dependency
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/pladdy/cabby"
)
// DiscoveryService implements a SQLite version of the DiscoveryService interface
type DiscoveryService struct {
DB *sql.DB
DataStore *DataStore
}
// CreateDiscovery creates a user in the data store
func (s DiscoveryService) CreateDiscovery(ctx context.Context, d cabby.Discovery) error {
resource, action := "Discovery", "create"
start := cabby.LogServiceStart(ctx, resource, action)
err := d.Validate()
if err == nil {
err = s.createDiscovery(d)
} else {
log.WithFields(log.Fields{"discovery": d, "error": err}).Error("Invalid Discovery")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s DiscoveryService) createDiscovery(d cabby.Discovery) error {
sql := `insert into discovery (title, description, contact, default_url) values (?, ?, ?, ?)`
args := []interface{}{d.Title, d.Description, d.Contact, d.Default}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// DeleteDiscovery creates a user in the data store
func (s DiscoveryService) DeleteDiscovery(ctx context.Context) error {
resource, action := "Discovery", "delete"
start := cabby.LogServiceStart(ctx, resource, action)
err := s.deleteDiscovery()
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s DiscoveryService) deleteDiscovery() error {
sql := `delete from discovery`
args := []interface{}{}
_, err := s.DB.Exec(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}
// Discovery will read from the data store and return the resource
func (s DiscoveryService) Discovery(ctx context.Context) (cabby.Discovery, error) {
resource, action := "Discovery", "read"
start := cabby.LogServiceStart(ctx, resource, action)
result, err := s.discovery()
cabby.LogServiceEnd(ctx, resource, action, start)
return result, err
}
func (s DiscoveryService) discovery() (cabby.Discovery, error) {
sql := `select td.title, td.description, td.contact, td.default_url,
case
when tar.api_root_path is null then 'No API Roots defined' else tar.api_root_path
end discovery_path
from
discovery td
left join api_root tar
on td.id = tar.discovery_id`
args := []interface{}{}
d := cabby.Discovery{}
var apiRoots []string
rows, err := s.DB.Query(sql, args...)
if err != nil {
logSQLError(sql, args, err)
return d, err
}
defer rows.Close()
for rows.Next() {
var apiRoot string
if err := rows.Scan(&d.Title, &d.Description, &d.Contact, &d.Default, &apiRoot); err != nil {
return d, err
}
if apiRoot != "No API Roots defined" {
apiRoots = append(apiRoots, apiRoot)
}
}
err = rows.Err()
d.APIRoots = apiRoots
return d, err
}
// UpdateDiscovery creates a user in the data store
func (s DiscoveryService) UpdateDiscovery(ctx context.Context, d cabby.Discovery) error {
resource, action := "Discovery", "update"
start := cabby.LogServiceStart(ctx, resource, action)
err := d.Validate()
if err == nil {
err = s.updateDiscovery(d)
} else {
log.WithFields(log.Fields{"discovery": d, "error": err}).Error("Invalid Discovery")
}
cabby.LogServiceEnd(ctx, resource, action, start)
return err
}
func (s DiscoveryService) updateDiscovery(d cabby.Discovery) error {
sql := `update discovery
set title = ?, description = ?, contact = ?, default_url = ?`
args := []interface{}{d.Title, d.Description, d.Contact, d.Default}
err := s.DataStore.write(sql, args...)
if err != nil {
logSQLError(sql, args, err)
}
return err
}