|
1
|
|
|
package models |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/memnix/memnixrest/pkg/database" |
|
5
|
|
|
"time" |
|
6
|
|
|
|
|
7
|
|
|
"gorm.io/gorm" |
|
8
|
|
|
) |
|
9
|
|
|
|
|
10
|
|
|
// MemDate structure |
|
11
|
|
|
type MemDate struct { |
|
12
|
|
|
gorm.Model |
|
13
|
|
|
UserID uint `json:"user_id" example:"1"` |
|
14
|
|
|
User User |
|
15
|
|
|
CardID uint `json:"card_id" example:"1"` |
|
16
|
|
|
Card Card |
|
17
|
|
|
DeckID uint `json:"deck_id" example:"1"` |
|
18
|
|
|
Deck Deck |
|
19
|
|
|
NextDate time.Time `json:"next_date" example:"06/01/2003"` // gorm:"autoCreateTime"` |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// ComputeNextDate calculates and sets the NextDate |
|
23
|
|
|
func (m *MemDate) ComputeNextDate(interval int) { |
|
24
|
|
|
m.NextDate = time.Now().AddDate(0, 0, interval) |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// SetDefaultNextDate fills MemDate values and sets NextDate as time.Now() |
|
28
|
|
|
func (m *MemDate) SetDefaultNextDate(userID, cardID, deckID uint) { |
|
29
|
|
|
m.UserID = userID |
|
30
|
|
|
m.CardID = cardID |
|
31
|
|
|
m.DeckID = deckID |
|
32
|
|
|
m.NextDate = time.Now() |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// GetNextToday fills MemDate with the next today card to review for a given user |
|
36
|
|
|
// It returns a ResponseHTTP for error handling purpose |
|
37
|
|
|
func (m *MemDate) GetNextToday(userID uint) *ResponseHTTP { |
|
38
|
|
|
db := database.DBConn // DB Conn |
|
39
|
|
|
res := new(ResponseHTTP) |
|
40
|
|
|
|
|
41
|
|
|
// Get next card with date condition |
|
42
|
|
|
t := time.Now() |
|
43
|
|
|
if err := db.Joins( |
|
44
|
|
|
"left join accesses ON mem_dates.deck_id = accesses.deck_id AND accesses.user_id = ?", |
|
45
|
|
|
userID).Joins("Card").Joins("Deck").Where("mem_dates.user_id = ? AND mem_dates.next_date < ? AND accesses.permission >= ?", |
|
46
|
|
|
userID, t.AddDate(0, 0, 1).Add( |
|
47
|
|
|
time.Duration(-t.Hour())*time.Hour), AccessStudent).Limit(1).Order("next_date asc").Find(&m).Error; err != nil { |
|
48
|
|
|
res.GenerateError("Next today memDate not found") |
|
49
|
|
|
return res |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
res.GenerateSuccess("Success getting next today memDate", nil, 0) |
|
53
|
|
|
|
|
54
|
|
|
return res |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// GetNext fills MemDate with the next card to review for a given user |
|
58
|
|
|
// It returns a ResponseHTTP for error handling purpose |
|
59
|
|
|
func (m *MemDate) GetNext(userID uint) *ResponseHTTP { |
|
60
|
|
|
db := database.DBConn // DB Conn |
|
61
|
|
|
res := new(ResponseHTTP) |
|
62
|
|
|
|
|
63
|
|
|
// Get next card |
|
64
|
|
|
if err := db.Joins( |
|
65
|
|
|
"left join accesses ON mem_dates.deck_id = accesses.deck_id AND accesses.user_id = ?", |
|
66
|
|
|
userID).Joins("Card").Joins("Deck").Where("mem_dates.user_id = ? AND accesses.permission >= ?", |
|
67
|
|
|
userID, AccessStudent).Limit(1).Order("next_date asc").Find(&m).Error; err != nil { |
|
68
|
|
|
res.GenerateError("Next memDate not found") |
|
69
|
|
|
return res |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
res.GenerateSuccess("Success getting next memDate", nil, 0) |
|
73
|
|
|
|
|
74
|
|
|
return res |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// GetNextByDeck fills MemDate with the next card to review for a given user and deck |
|
78
|
|
|
// It returns a ResponseHTTP for error handling purpose |
|
79
|
|
|
func (m *MemDate) GetNextByDeck(userID, deckID uint) *ResponseHTTP { |
|
80
|
|
|
db := database.DBConn // DB Conn |
|
81
|
|
|
res := new(ResponseHTTP) |
|
82
|
|
|
|
|
83
|
|
|
// Get next card with deck condition |
|
84
|
|
|
if err := db.Joins("Card").Joins("User").Joins("Deck").Where("mem_dates.user_id = ? AND mem_dates.deck_id = ?", |
|
85
|
|
|
userID, deckID).Limit(1).Order("next_date asc").Find(&m).Error; err != nil { |
|
86
|
|
|
res.GenerateError("Next memDate by deck not found") |
|
87
|
|
|
return res |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
res.GenerateSuccess("Success getting next memDate by deck", nil, 0) |
|
91
|
|
|
|
|
92
|
|
|
return res |
|
93
|
|
|
} |
|
94
|
|
|
|