-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
73 lines (60 loc) · 1.64 KB
/
store.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
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
)
// https://gorm.io/docs/
type SqlStore struct {
DB *gorm.DB
DBPath string
}
type LabSubmission struct {
gorm.Model
EdxAnonId string
LabName string
LabAnswers string
}
// =============================================================================
// global mutable state, be careful!
var globalStore = SqlStore{}
// -----------------------------------------------------------------------------
func OpenSqlStore(path string) (SqlStore, error) {
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
if err != nil {
return SqlStore{}, Err(err, "Could not open the answer database")
}
db.AutoMigrate(&LabSubmission{})
return SqlStore{db, path}, nil
}
func initGlobalStore(path string) error {
store, err := OpenSqlStore(path)
if err != nil {
return Err(err, "could not initialize global store database")
}
globalStore = store
return nil
}
func (store SqlStore) InsertAnswer(edxAnonId, labName, labAnswers string) error {
store.DB.Create(&LabSubmission{
EdxAnonId: edxAnonId,
LabName: labName,
LabAnswers: labAnswers,
})
return store.DB.Error
}
func (store SqlStore) GetAnswers(edxAnonId, labName string) (LabSubmission, error) {
// get the latest answers submitted by user: edxAnonId with labName.
var labSub LabSubmission
err := store.DB.
Where("edx_anon_id = ?", edxAnonId).
Where("lab_name = ?", labName).
Last(&labSub).Error
log.Printf("getting answers for: user: %s, labName: %s\n", edxAnonId, labName)
log.Println("This is what the database got: ", labSub)
if err != nil {
return labSub, err
} else {
return labSub, nil
}
}