-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
374 lines (326 loc) · 10.4 KB
/
database.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package cooking
import (
"database/sql"
"encoding/base64"
"fmt"
"html"
"html/template"
"image/color"
"net/url"
"sort"
"strconv"
"strings"
"github.com/jduepmeier/go-cooking/database"
// import sqlite3 sql driver. Must be blank import for loading the init register function.
"github.com/gorilla/securecookie"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
)
const (
// Fresh if the reciped needs fresh ingredients.
Fresh Freshness = 1
// MiddleFresh if the reciped needs middle fresh ingredients.
MiddleFresh Freshness = 2
// NotFresh if no fresh ingredients are needed.
NotFresh Freshness = 3
// MaxFreshness is the currently highest value for freshness.
MaxFreshness int64 = int64(NotFresh)
)
// Freshness defines the freshness.
type Freshness int
// HTML return the freshness value as html formatted scala.
func (f Freshness) HTML() template.HTML {
var builder strings.Builder
for i := int(MaxFreshness); i >= int(f); i-- {
builder.WriteString(`<img class="freshness-img" src="/static/karotte.svg" />`)
}
return template.HTML(builder.String())
}
const (
// StatementAdd adds a recipe to database.
StatementAdd = "INSERT INTO recipes(name, description, length, freshness, source) VALUES(:name, :description, :length, :freshness, :source)"
// StatementUpdate updates a recipe.
StatementUpdate = "UPDATE recipes SET name = :name, description = :description, length = :length, freshness = :freshness, source = :source WHERE id = :id"
// StatementDelete deletes a recipe from database.
StatementDelete = "DELETE FROM recipes WHERE id = :id"
// StatementGetAll returns all recipes from database.
StatementGetAll = "SELECT id, name, description, length, freshness, source FROM recipes"
// StatementGet returns the recipe with the given id from database.
StatementGet = "SELECT id, name, description, length, freshness, source FROM recipes WHERE id = :id"
// StatementGetUser returns the user with the given username from database.
StatementGetUser = "SELECT username, password FROM user WHERE username = :username"
// StatementAddUser adds a user to the database.
StatementAddUser = "INSERT INTO user (username, password) VALUES(:username, :password)"
// StatementSetPassword sets the password for a user.
StatementSetPassword = "UPDATE user SET password = :password WHERE username = :username"
// StatementGetAllUsers returns all users.
StatementGetAllUsers = "SELECT username, password FROM user"
)
// Storage handles access to the storage.
type Storage struct {
Path string
db *sql.DB
Statements map[string]*sql.Stmt
}
// Recipe contains all infos about a recipe.
type Recipe struct {
ID uint64
Name string
Description string
Length string
Freshness Freshness
Source string
}
// SourceHTML returns the source field as html code.
// If it is a url it will be converted to qrcode.
// Otherwise it will be returned as text.
func (recipe *Recipe) SourceHTML() template.HTML {
_, err := url.ParseRequestURI(recipe.Source)
if err != nil {
return template.HTML(fmt.Sprintf("<p>%s</p>", recipe.Source))
}
size := 256
htmlSource := html.EscapeString(recipe.Source)
png, err := qrcode.New(recipe.Source, qrcode.Highest)
if err != nil {
logrus.Errorf("could not encode qr code for %s", recipe.Source)
return template.HTML(fmt.Sprintf(`<a href=%q>%q<a />`, htmlSource, htmlSource))
}
png.BackgroundColor = color.Transparent
pngEncoded, err := png.PNG(size)
if err != nil {
logrus.Errorf("could not encode qr code for %s", recipe.Source)
return template.HTML(fmt.Sprintf(`<a href=%q>%q<a />`, htmlSource, htmlSource))
}
png64 := base64.StdEncoding.EncodeToString(pngEncoded)
return template.HTML(fmt.Sprintf(`<a href=%q><img width="%d" height="%d" alt=%q src="data:image/png;base64,%s" /></a>`, htmlSource, size, size, htmlSource, png64))
}
// Validate validates the struct for missing or broken values.
func (recipe *Recipe) Validate() error {
if recipe.Name == "" {
return fmt.Errorf("missing key: name")
}
if recipe.Length == "" {
return fmt.Errorf("missing key: length")
}
return nil
}
// ParseFreshness parses the freshness value and adds it to the recipe.
func (recipe *Recipe) ParseFreshness(freshness string) error {
value, err := strconv.ParseInt(freshness, 10, 32)
if err != nil {
return err
}
if value > MaxFreshness && value < 1 {
return fmt.Errorf("freshness must be between 1 and %d", MaxFreshness)
}
recipe.Freshness = Freshness(value)
return nil
}
// Connect connects to the database.
func (storage *Storage) Connect() (err error) {
if storage.db == nil {
if storage.Path == "" {
storage.Path = ":memory:"
}
storage.db, err = sql.Open("sqlite3", storage.Path)
if err != nil {
return err
}
err = storage.checkDB()
if err != nil {
return err
}
err = storage.prepareStatements()
if err != nil {
return err
}
}
return nil
}
func (storage *Storage) prepareStatements() (err error) {
statements := map[string]string{
"get": StatementGet,
"getall": StatementGetAll,
"update": StatementUpdate,
"add": StatementAdd,
"delete": StatementDelete,
"setpassword": StatementSetPassword,
"adduser": StatementAddUser,
"getuser": StatementGetUser,
"getallusers": StatementGetAllUsers,
}
storage.Statements = make(map[string]*sql.Stmt, len(statements))
for identifier, statement := range statements {
stmt, err := storage.db.Prepare(statement)
if err != nil {
return err
}
storage.Statements[identifier] = stmt
}
return nil
}
// Close closes the database connection.
func (storage *Storage) Close() {
if storage.db != nil {
storage.db.Close()
}
}
func (storage *Storage) checkDB() (err error) {
row := storage.db.QueryRow("SELECT value FROM meta WHERE key = 'version'")
var currVersionString string
var currVersion int64
var maxVersion int64
err = row.Scan(&currVersionString)
logrus.Infof("error is %s and currVersionString=%s", err, currVersionString)
if err == nil {
currVersion, err = strconv.ParseInt(currVersionString, 10, 64)
if err != nil {
currVersion = -1
}
} else if strings.Contains(err.Error(), "no such table") {
currVersion = -1
} else {
return err
}
maxVersion = currVersion
logrus.Infof("current version is %d", currVersion)
sort.Sort(database.ByVersion(database.Versions))
for _, version := range database.Versions {
if currVersion < int64(version.Version) {
logrus.Infof("install version %d", version.Version)
_, err := storage.db.Exec(version.Statement)
if err != nil {
return fmt.Errorf("cannot install version %d: %s", version.Version, err)
}
maxVersion = int64(version.Version)
}
}
_, err = storage.db.Exec("UPDATE meta SET value = :version WHERE key = 'version';", sql.Named("version", &maxVersion))
return err
}
// GetRecipes returns all saved recipes.
func (storage *Storage) GetRecipes() ([]Recipe, error) {
var recipes []Recipe
rows, err := storage.Statements["getall"].Query()
if err != nil {
return recipes, err
}
for rows.Next() {
recipe := Recipe{}
err = rows.Scan(&recipe.ID, &recipe.Name, &recipe.Description, &recipe.Length, &recipe.Freshness, &recipe.Source)
if err != nil {
return recipes, err
}
recipes = append(recipes, recipe)
}
return recipes, rows.Err()
}
// Add adds a recipe to storage.
func (storage *Storage) Add(recipe Recipe) error {
logrus.Infof("add %v", recipe)
_, err := storage.Statements["add"].Exec(
sql.Named("name", &recipe.Name),
sql.Named("description", &recipe.Description),
sql.Named("length", &recipe.Length),
sql.Named("freshness", &recipe.Freshness),
sql.Named("source", &recipe.Source),
)
return err
}
// Update updates a recipe.
func (storage *Storage) Update(recipe Recipe) error {
if recipe.ID == 0 {
return fmt.Errorf("id must be defined")
}
logrus.Infof("update %v", recipe)
_, err := storage.Statements["update"].Exec(
sql.Named("id", &recipe.ID),
sql.Named("name", &recipe.Name),
sql.Named("description", &recipe.Description),
sql.Named("length", &recipe.Length),
sql.Named("freshness", &recipe.Freshness),
sql.Named("source", &recipe.Source),
)
return err
}
// Get returns the recipe with the given id.
func (storage *Storage) Get(id uint64) (Recipe, error) {
row := storage.Statements["get"].QueryRow(sql.Named("id", id))
recipe := Recipe{}
err := row.Scan(
&recipe.ID,
&recipe.Name,
&recipe.Description,
&recipe.Length,
&recipe.Freshness,
&recipe.Source,
)
return recipe, err
}
// Delete deletes a recipe.
func (storage *Storage) Delete(id uint64) error {
_, err := storage.Statements["delete"].Exec(sql.Named("id", &id))
return err
}
// GetUser returns the user with the given username.
func (storage *Storage) GetUser(username string) (User, error) {
row := storage.Statements["getuser"].QueryRow(sql.Named("username", username))
user := User{}
err := row.Scan(
&user.Username,
&user.Password,
)
return user, err
}
// AddUser adds a user to the database.
func (storage *Storage) AddUser(user User) error {
_, err := storage.Statements["adduser"].Exec(
sql.Named("username", user.Username),
sql.Named("password", user.Password),
)
return err
}
// SetPassword sets the password for a user.
func (storage *Storage) SetPassword(user User) error {
_, err := storage.Statements["setpassword"].Exec(
sql.Named("username", user.Username),
sql.Named("password", user.Password),
)
return err
}
// GetAllUsers returns all users.
func (storage *Storage) GetAllUsers() ([]User, error) {
var users []User
rows, err := storage.Statements["getallusers"].Query()
if err != nil {
return users, err
}
for rows.Next() {
user := User{}
err = rows.Scan(&user.Username, &user.Password)
if err != nil {
return users, err
}
users = append(users, user)
}
return users, rows.Err()
}
// GetOrCreateSessionCookieKey returns the session cookie key from database or creates it.
func (storage *Storage) GetOrCreateSessionCookieKey() ([]byte, error) {
row := storage.db.QueryRow("SELECT value FROM meta WHERE key = 'sessionkey'")
var base64Key string
err := row.Scan(&base64Key)
if err == nil {
key, err := base64.StdEncoding.DecodeString(base64Key)
if err == nil {
return key, err
}
}
logrus.Warnf("cannot get session key form db: %s", err)
key := securecookie.GenerateRandomKey(64)
base64Key = base64.StdEncoding.EncodeToString(key)
_, err = storage.db.Exec("INSERT INTO meta (key, value) VALUES ('sessionkey', :key)", sql.Named("key", base64Key))
return key, err
}