models

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoMigrate

func AutoMigrate(db *gorm.DB) error

func ValidEmail

func ValidEmail(email string) bool

Types

type Question

type Question struct {
	gorm.Model
	Index          int // used for sorting in the final quiz
	SessionEmail   string
	Session        Session      `gorm:"foreignKey:SessionEmail"`
	Text           string       `yaml:"text,omitempty"`
	Difficulty     int          `yaml:"difficulty,omitempty"`
	Type           QuestionType `yaml:"type,omitempty"`
	RightAnswer    int          `yaml:"rightAnswer,omitempty"`
	UserAnswer     int          `yaml:"userAnswer,omitempty"`
	Answers        Answers      `yaml:"answers,omitempty" gorm:"type:VARCHAR(255)"`
	AllowedSeconds int          `yaml:"allowedSeconds,omitempty"`
	StartedAt      time.Time
}

func (Question) Expired

func (q Question) Expired() bool

func (Question) Valid

func (q Question) Valid() bool

type QuestionList

type QuestionList []Question

func (QuestionList) InDifficultyRange

func (ql QuestionList) InDifficultyRange(min, max int) QuestionList

func (QuestionList) Limit

func (ql QuestionList) Limit(limit int) QuestionList

Limit return exactly `limit` number of questions. If the total questions are more than the requests, it will remove the additional ones, with preference to the questions with difficulty that is already represented. For example If there are 5 total questions like this: - 2 with difficulty 1 - 1 with difficulty 2 - 2 with difficutly 3 when limit is "3" the method will return one questions from each difficulty (instead for example: 2 with difficulty 1 and 1 with difficulty 2). In other words, it tries to have questions from all difficulties if possible. Desired questions are picked starting from lower to higher levels but when a difficulty level runs out of questions, one from a higher level is picked instead.

func (QuestionList) OrderedByDifficulty

func (ql QuestionList) OrderedByDifficulty() QuestionList

func (QuestionList) Score

func (ql QuestionList) Score() float64

func (QuestionList) Suffled

func (ql QuestionList) Suffled() QuestionList

func (QuestionList) Valid

func (ql QuestionList) Valid() QuestionList

type QuestionPool

type QuestionPool struct {
	Questions QuestionList `yaml:"questions,omitempty"`
}

func NewQuestionPool

func NewQuestionPool(template string) (QuestionPool, error)

func NewQuestionPoolFromFile

func NewQuestionPoolFromFile(filePath string) (QuestionPool, error)

type Quiz

type Quiz struct {
	Questions QuestionList `yaml:"questions,omitempty"`
}

Quiz is the collection of questions from a QuestionPool based on QuizOptions. It's an intermidiate model used to prepare the Questions that will be stored in the database.

func NewQuizWithOpts

func NewQuizWithOpts(opts QuizOptions) (Quiz, error)

func (Quiz) PersistForSessionEmail

func (quiz Quiz) PersistForSessionEmail(db *gorm.DB, email string) error

type QuizOptions

type QuizOptions struct {
	TotalQuestions     int
	MinDifficulty      int
	MaxDifficulty      int
	QuestionTimeoutSec int
	AvailableQuestions QuestionList
}

type Session

type Session struct {
	gorm.Model
	Email     string
	Questions []Question `gorm:"foreignKey:SessionEmail;references:Email"`
}

func NewSessionForEmail

func NewSessionForEmail(db *gorm.DB, email string) (Session, error)

func SessionForEmail

func SessionForEmail(db *gorm.DB, email string) (Session, error)

func (Session) CurrentQuestion

func (s Session) CurrentQuestion() (Question, error)

CurrentQuestion returns the first non-expired question If the session has an expired question, this method returns an error (there is no "CurrentQuestion" since the whole Session is expired)

func (Session) HasExpiredQuestions

func (s Session) HasExpiredQuestions() bool

HasExpiredQuestions returns `true` if there is at least one expired Question

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL