Passed
Pull Request — main (#45)
by Yume
01:43
created

models.*Mcq.UpdateLinkedAnswers   A

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 0
dl 0
loc 16
rs 9.85
c 0
b 0
f 0
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
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
32
		answers := mcq.QueryLinkedAnswers()
33
		if len(answers) != len(answersList) {
34
35
			mcq.UpdateLinkedAnswers()
36
			return answers
37
		}
38
	}
39
40
	return answersList
41
}
42
43
// ExtractAnswers method
44
func (mcq *Mcq) ExtractAnswers() []string {
45
	return strings.Split(mcq.Answers, ";")
46
}
47
48
// SetAnswers method
49
func (mcq *Mcq) SetAnswers(answers []string) {
50
	mcq.Answers = strings.Join(answers, ";")
51
}
52
53
// QueryLinkedAnswers returns linked answers
54
func (mcq *Mcq) QueryLinkedAnswers() []string {
55
	db := database.DBConn // DB Conn
56
	var cards []Card
57
	var responses []string
58
59
	if err := db.Joins("Mcq").Where("cards.mcq_id = ?", mcq.ID).Find(&cards).Error; err != nil {
60
		return responses
61
		//TODO: Error logging
62
	}
63
64
	for i := range cards {
65
		responses = append(responses, cards[i].Answer)
66
	}
67
68
	return responses
69
}
70
71
// NotValidate performs validation of the mcq
72
func (mcq *Mcq) NotValidate() bool {
73
	return mcq.Type == McqStandalone && (len(mcq.Answers) < utils.MinMcqAnswersLen || len(mcq.Answers) > utils.MaxMcqAnswersLen) || len(mcq.Name) > utils.MaxMcqName || mcq.Name == ""
74
}
75
76
// FillWithLinkedAnswers method
77
func (mcq *Mcq) FillWithLinkedAnswers() *ResponseHTTP {
78
	res := new(ResponseHTTP)
79
80
	answers := mcq.QueryLinkedAnswers()
81
	if len(answers) == 0 {
82
		res.GenerateError("Couldn't query linked answers")
83
		return res
84
	}
85
	mcq.SetAnswers(answers)
86
87
	res.GenerateSuccess("Success fill mcq with linked answers", answers, len(answers))
88
	return res
89
}
90
91
// UpdateLinkedAnswers method to update the db
92
func (mcq *Mcq) UpdateLinkedAnswers() *ResponseHTTP {
93
	db := database.DBConn // DB Conn
94
	res := new(ResponseHTTP)
95
96
	if err := mcq.FillWithLinkedAnswers(); !err.Success {
97
		res.GenerateError(err.Message)
98
		return res
99
	}
100
101
	if err := db.Save(mcq).Error; err != nil {
102
		res.GenerateError(err.Error())
103
		return res
104
	}
105
106
	res.GenerateSuccess("Success update mcq with linked answers", nil, 0)
107
	return res
108
}
109