1
|
|
|
package models |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"github.com/memnix/memnixrest/pkg/database" |
5
|
|
|
"github.com/memnix/memnixrest/pkg/utils" |
6
|
|
|
"gorm.io/gorm" |
7
|
|
|
"strings" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
type Mcq struct { |
11
|
|
|
gorm.Model `swaggerignore:"true"` |
12
|
|
|
Name string `json:"mcq_name"` |
13
|
|
|
Answers string `json:"mcq_answers"` |
14
|
|
|
Type McqType `json:"mcq_type"` |
15
|
|
|
DeckID uint `json:"deck_id" example:"1"` |
16
|
|
|
Deck Deck `swaggerignore:"true" json:"-"` |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
type McqType int64 |
20
|
|
|
|
21
|
|
|
const ( |
22
|
|
|
McqStandalone McqType = iota |
23
|
|
|
McqLinked |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
// GetAnswers returns a list of answers |
27
|
|
|
func (mcq *Mcq) GetAnswers() []string { |
28
|
|
|
answersList := mcq.ExtractAnswers() |
29
|
|
|
|
30
|
|
|
if mcq.Type == McqLinked { |
31
|
|
|
answers := mcq.QueryLinkedAnswers() |
32
|
|
|
if len(answers) != len(answersList) { |
33
|
|
|
mcq.UpdateLinkedAnswers() |
34
|
|
|
return answers |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return answersList |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// ExtractAnswers method |
42
|
|
|
func (mcq *Mcq) ExtractAnswers() []string { |
43
|
|
|
return strings.Split(mcq.Answers, ";") |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// SetAnswers method |
47
|
|
|
func (mcq *Mcq) SetAnswers(answers []string) { |
48
|
|
|
mcq.Answers = strings.Join(answers, ";") |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// QueryLinkedAnswers returns linked answers |
52
|
|
|
func (mcq *Mcq) QueryLinkedAnswers() []string { |
53
|
|
|
db := database.DBConn // DB Conn |
54
|
|
|
var cards []Card |
55
|
|
|
|
56
|
|
|
if err := db.Joins("Mcq").Where("cards.mcq_id = ?", mcq.ID).Find(&cards).Error; err != nil { |
57
|
|
|
return make([]string, 0) |
58
|
|
|
//TODO: Error logging |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
responses := make([]string, len(cards)) |
62
|
|
|
for i := range cards { |
63
|
|
|
responses[i] = cards[i].Answer |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return responses |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// NotValidate performs validation of the mcq |
70
|
|
|
func (mcq *Mcq) NotValidate() bool { |
71
|
|
|
return mcq.Type == McqStandalone && (len(mcq.Answers) < utils.MinMcqAnswersLen || len(mcq.Answers) > utils.MaxMcqAnswersLen) || len(mcq.Name) > utils.MaxMcqName || mcq.Name == "" |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// FillWithLinkedAnswers method |
75
|
|
|
func (mcq *Mcq) FillWithLinkedAnswers() *ResponseHTTP { |
76
|
|
|
res := new(ResponseHTTP) |
77
|
|
|
|
78
|
|
|
answers := mcq.QueryLinkedAnswers() |
79
|
|
|
if len(answers) == 0 { |
80
|
|
|
res.GenerateError("Couldn't query linked answers") |
81
|
|
|
return res |
82
|
|
|
} |
83
|
|
|
mcq.SetAnswers(answers) |
84
|
|
|
|
85
|
|
|
res.GenerateSuccess("Success fill mcq with linked answers", answers, len(answers)) |
86
|
|
|
return res |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// UpdateLinkedAnswers method to update the db |
90
|
|
|
func (mcq *Mcq) UpdateLinkedAnswers() *ResponseHTTP { |
91
|
|
|
db := database.DBConn // DB Conn |
92
|
|
|
res := new(ResponseHTTP) |
93
|
|
|
|
94
|
|
|
if err := mcq.FillWithLinkedAnswers(); !err.Success { |
95
|
|
|
res.GenerateError(err.Message) |
96
|
|
|
return res |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if err := db.Save(mcq).Error; err != nil { |
100
|
|
|
res.GenerateError(err.Error()) |
101
|
|
|
return res |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
res.GenerateSuccess("Success update mcq with linked answers", nil, 0) |
105
|
|
|
return res |
106
|
|
|
} |
107
|
|
|
|