-
Notifications
You must be signed in to change notification settings - Fork 4
/
apps.go
254 lines (195 loc) · 4.96 KB
/
apps.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package hermes
import (
"context"
"database/sql"
"github.com/rugwirobaker/hermes/observ"
"github.com/rugwirobaker/hermes/sqlite"
)
// hermes is a service that exposes an API for sending SMS messages to other internal apps(services)
type App struct {
// Name of the app
Name string `json:"name"`
// ID of the app
ID string `json:"id"`
// APIKey of the app
APIKey string `json:"token"`
// Sender is the sender of the message
Sender string `json:"sender"`
// CreatedAt is the time the app was created
CreatedAt string `json:"created_at"`
// UpdatedAt is the time the app was updated
UpdatedAt string `json:"updated_at"`
// MessageCount is the number of messages sent by the app
MessageCount int64 `json:"message_count"`
}
type AppStore interface {
// Register a new app
Register(context.Context, *App) error
// Get an app by id
Get(context.Context, string) (*App, error)
// FindByToken finds an app by token
FindByToken(context.Context, string) (*App, error)
// Update an app
Update(context.Context, *App) error
// Delete an app
Delete(context.Context, string) error
// List all apps
List(context.Context) ([]*App, error)
}
func NewAppStore(db *sqlite.DB) AppStore {
return &appStore{db: db}
}
type appStore struct {
db *sqlite.DB
}
func (s *appStore) Register(ctx context.Context, app *App) error {
const op = "appStore.Register"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
opts := sqlite.TxOptions(false)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return err
}
defer tx.Rollback()
if err = tx.QueryRowContext(
ctx,
`INSERT INTO apps (
name,
token,
sender
) VALUES (?, ?, ?) RETURNING id, created_at, updated_at`,
app.Name, app.APIKey, app.Sender,
).Scan(&app.ID, &app.CreatedAt, &app.UpdatedAt); err != nil {
if sqlite.IsUniqueConstraintError(err) {
return ErrAlreadyExists
}
return err
}
return tx.Commit()
}
func (s *appStore) Get(ctx context.Context, id string) (*App, error) {
const op = "appStore.Get"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
var app App
opts := sqlite.TxOptions(true)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
defer tx.Rollback()
var row = tx.QueryRowContext(ctx, `
SELECT
id, name, token, sender, created_at, updated_at, message_count
FROM apps WHERE id = ?
`, id)
err = row.Scan(&app.ID, &app.Name, &app.APIKey, &app.Sender, &app.CreatedAt, &app.UpdatedAt, &app.MessageCount)
if err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return &app, tx.Commit()
}
func (s *appStore) Update(ctx context.Context, app *App) error {
const op = "appStore.Update"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
opts := sqlite.TxOptions(false)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `
UPDATE apps
SET name = ?, token = ?, sender = ?, created_at = ?, updated_at = ?, message_count = ?
WHERE id = ?
`, app.Name, app.APIKey, app.Sender, app.CreatedAt, app.UpdatedAt, app.MessageCount, app.ID)
if err != nil {
return err
}
return tx.Commit()
}
func (s *appStore) Delete(ctx context.Context, id string) error {
const op = "appStore.Delete"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
opts := sqlite.TxOptions(false)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `
DELETE FROM apps
WHERE id = ?
`, id)
if err != nil {
return err
}
return tx.Commit()
}
func (s *appStore) List(ctx context.Context) ([]*App, error) {
const op = "appStore.List"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
var apps []*App
opts := sqlite.TxOptions(true)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
defer tx.Rollback()
rows, err := tx.QueryContext(ctx, `
SELECT
id,
name,
token,
sender,
created_at,
updated_at,
message_count
FROM apps
`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var app App
err = rows.Scan(&app.ID, &app.Name, &app.APIKey, &app.Sender, &app.CreatedAt, &app.UpdatedAt, &app.MessageCount)
if err != nil {
return nil, err
}
apps = append(apps, &app)
}
return apps, tx.Commit()
}
func (s *appStore) FindByToken(ctx context.Context, token string) (*App, error) {
const op = "appStore.FindByToken"
ctx, span := observ.StartSpan(ctx, op)
defer span.End()
var app App
opts := sqlite.TxOptions(true)
tx, err := s.db.BeginTx(ctx, opts)
if err != nil {
return nil, err
}
defer tx.Rollback()
var row = tx.QueryRowContext(ctx, `
SELECT
id, name, token, sender, created_at, updated_at, message_count
FROM apps WHERE token = ?
`, token)
err = row.Scan(&app.ID, &app.Name, &app.APIKey, &app.Sender, &app.CreatedAt, &app.UpdatedAt, &app.MessageCount)
if err != nil {
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
return nil, err
}
return &app, tx.Commit()
}