Total Lines | 60 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package models |
||
2 | |||
3 | import ( |
||
4 | "gorm.io/gorm" |
||
5 | "math/rand" |
||
6 | "memnixrest/pkg/database" |
||
7 | "time" |
||
8 | ) |
||
9 | |||
10 | // Card structure |
||
11 | type Card struct { |
||
12 | gorm.Model `swaggerignore:"true"` |
||
13 | Question string `json:"card_question" example:"What's the answer to life ?"` |
||
14 | Answer string `json:"card_answer" example:"42"` |
||
15 | DeckID uint `json:"deck_id" example:"1"` |
||
16 | Deck Deck |
||
17 | Tips string `json:"card_tips" example:"The answer is from a book"` |
||
18 | Explication string `json:"card_explication" example:"The number 42 is the answer to life has written in a very famous book"` |
||
19 | Type CardType `json:"card_type" example:"0" gorm:"type:Int"` |
||
20 | Format string `json:"card_format" example:"Date / Name / Country"` |
||
21 | Image string `json:"card_image"` // Should be an url |
||
22 | } |
||
23 | |||
24 | type CardType int64 |
||
25 | |||
26 | const ( |
||
27 | CardString CardType = iota |
||
28 | CardInt |
||
29 | CardMCQ |
||
30 | ) |
||
31 | |||
32 | func (s CardType) ToString() string { |
||
33 | switch s { |
||
34 | case CardString: |
||
35 | return "Card String" |
||
36 | case CardInt: |
||
37 | return "Card Int" |
||
38 | case CardMCQ: |
||
39 | return "Card MCQ" |
||
40 | default: |
||
41 | return "Unknown" |
||
42 | } |
||
43 | } |
||
44 | |||
45 | func (card *Card) GetMCQAnswers() []string { |
||
46 | db := database.DBConn // DB Conn |
||
47 | var answersList []string |
||
48 | var answers []Answer |
||
49 | |||
50 | if err := db.Joins("Card").Where("answers.card_id = ?", card.ID).Limit(3).Order("random()").Find(&answers).Error; err != nil { |
||
51 | return nil |
||
52 | } |
||
53 | |||
54 | if len(answers) >= 3 { |
||
55 | answersList = append(answersList, answers[0].Answer, answers[1].Answer, answers[2].Answer, card.Answer) |
||
56 | rand.Seed(time.Now().UnixNano()) |
||
57 | rand.Shuffle(len(answersList), func(i, j int) { answersList[i], answersList[j] = answersList[j], answersList[i] }) |
||
58 | } |
||
59 | |||
60 | return answersList |
||
61 | } |
||
62 |