-
Notifications
You must be signed in to change notification settings - Fork 9
/
db.go
78 lines (65 loc) · 1.34 KB
/
db.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
package main
import (
"math/rand"
"time"
"github.com/asdine/storm/v3"
)
type MasherPatch struct {
Id string `storm:"id"`
DateCreated int64
DateModified int64
Title string
Author string
Files map[string]string
}
type MasherUser struct {
Name string `storm:"id"`
Password string
Email string
DateCreated int64
}
type SearchFilter struct {
Author string
}
var db *storm.DB
func InitDB() {
var err error
db, err = storm.Open(MasherConfig.DBPath)
if err != nil {
panic(err)
}
rand.Seed(time.Now().UnixNano())
}
func RetrievePatch(id string) (MasherPatch, error) {
var result MasherPatch
err := db.One("Id", id, &result)
return result, err
}
func RetrievePatches(filter SearchFilter) ([]MasherPatch, error) {
var results []MasherPatch
var err error
if filter.Author != "" {
err = db.Find("Author", filter.Author, &results)
} else {
err = db.All(&results)
}
return results, err
}
func PutPatch(patch MasherPatch) error {
err := db.Save(&patch)
return err
}
func RetrieveUser(name string) (MasherUser, error) {
var result MasherUser
err := db.One("Name", name, &result)
return result, err
}
func RetrieveUsers() ([]MasherUser, error) {
var results []MasherUser
err := db.All(&results)
return results, err
}
func AddUser(user MasherUser) error {
err := db.Save(&user)
return err
}