-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrs.go
78 lines (61 loc) · 1.41 KB
/
srs.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 srs
import (
"time"
"github.com/revelaction/go-srs/db"
"github.com/revelaction/go-srs/review"
"github.com/revelaction/go-srs/uid"
)
// Srs encapsulates the db handler and the UID generator.
//
// Deck and card ids are provided by this package
type Srs struct {
Db db.Handler
UID uid.UID
}
func New(db db.Handler, id uid.UID) *Srs {
return &Srs{
Db: db,
UID: id,
}
}
// Update update the cards in Review, persist then in th db and returns the
// updated Cards due time.
func (h *Srs) Update(r review.Review) (due review.Due, err error) {
err = r.Validate()
if err != nil {
return due, err
}
//1) insert for no previous DeckId with the created boxId
if r.DeckId == "" {
// is external
deckId := h.UID.Create()
due, err = h.Db.Insert(r, deckId)
if err != nil {
return due, err
}
return due, nil
}
// 2) we have DeckId and all cards are new. Look up and insert after last
// index
if r.AllNewCards() {
due, err = h.Db.Insert(r, "")
if err != nil {
return review.Due{}, err
}
return due, nil
}
//3) review has DeckId and cardIds: update for existent
due, err = h.Db.Update(r)
if err != nil {
return review.Due{}, err
}
return due, nil
}
// Due returns all card ids that are due to be reviewed at time t
func (h *Srs) Due(deckId string, t time.Time) (due review.Due, err error) {
due, err = h.Db.Due(deckId, t)
if err != nil {
return due, err
}
return due, nil
}