package models import ( "errors" "math/rand" "lmika.dev/pkg/modash/moslice" ) type QuestionSet struct { Questions []Question } type Choice struct { Text string IsRight bool } type Question struct { ID string Text string Choices []Choice Fact string } func (q Question) Render() (RenderedQuestion, error) { choices := moslice.MapIndex(q.Choices, func(c Choice, i int) RenderedChoice { return RenderedChoice{ ID: i + 1, Text: c.Text, } }) rand.Shuffle(len(choices), func(i, j int) { choices[i], choices[j] = choices[j], choices[i] }) _, idx, ok := moslice.FindWithIndexWhere(q.Choices, func(c Choice) bool { return c.IsRight }) if !ok { return RenderedQuestion{}, errors.New("question does not have a right answer") } return RenderedQuestion{ ID: q.ID, Question: q.Text, Fact: q.Fact, Choices: choices, RightChoice: idx + 1, }, nil } type RenderedChoice struct { ID int Text string } type RenderedQuestion struct { ID string Question string Fact string Choices []RenderedChoice RightChoice int }