question

package
v0.0.0-...-c99e1c3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRecord = errors.New("invalid question record")
View Source
var ErrNoQuestionsLeft = errors.New("no questions left")

ErrNoQuestionsLeft is returned when there are no questions left to be answered for a user.

Functions

func Limit

func Limit(n int) qm.QueryMod

func OrderByCreatedAtAsc

func OrderByCreatedAtAsc() qm.QueryMod

func OrderByCreatedAtDesc

func OrderByCreatedAtDesc() qm.QueryMod

func WhereChoiceIDIn

func WhereChoiceIDIn(ids ...uuid.UUID) qm.QueryMod

func WhereIDIn

func WhereIDIn(ids ...uuid.UUID) qm.QueryMod

func WhereNotAnsweredBy

func WhereNotAnsweredBy(userID uuid.UUID) qm.QueryMod

func WhereTopicEq

func WhereTopicEq(topic enums.Topic) qm.QueryMod

Types

type Choice

type Choice struct {
	ID        uuid.UUID
	Choice    string
	IsCorrect bool
}

Choice represents a possible answer to a question.

type Question

type Question struct {
	ID         uuid.UUID
	Topic      enums.Topic
	Question   string
	Hint       string
	MoreInfo   string
	Difficulty enums.Difficulty
	Choices    []Choice
	CreatedAt  time.Time
}

Question represents a question about history that users need to answer.

func Mock

func Mock(applyOverrides func(*Question)) Question

Mock creates a new random Question for testing purposes with the specified overrides.

func New

func New(question, hint, moreInfo string) *Question

New creates a new Question with a random ID, a question, and a hint.

func (*Question) CorrectChoice

func (q *Question) CorrectChoice() (*Choice, error)

CorrectChoices returns the choices that are correct.

func (*Question) Validate

func (q *Question) Validate() error

Validate checks if a question is valid or not, and returns an error if it's not.

func (*Question) WithChoice

func (q *Question) WithChoice(choice string, isCorrect bool) *Question

WithChoice adds a choice to a question.

func (*Question) WithDifficulty

func (q *Question) WithDifficulty(difficulty enums.Difficulty) *Question

WithDifficulty sets the difficulty of a question.

func (*Question) WithTopic

func (q *Question) WithTopic(topic enums.Topic) *Question

WithTopic sets the topic of a question.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository(db *sql.DB) *Repository

func (*Repository) GetMany

func (r *Repository) GetMany(ctx context.Context, qms ...qm.QueryMod) ([]Question, error)

func (*Repository) GetOne

func (r *Repository) GetOne(ctx context.Context, qms ...qm.QueryMod) (*Question, error)

func (*Repository) GetRemainingTopics

func (r *Repository) GetRemainingTopics(
	ctx context.Context,
	userID uuid.UUID,
) (map[enums.Topic]uint, error)

GetRemainingTopics returns a map such that each key is a topic for which the user still has unanswered questions, and each value is the amount of remaining questions for that topic. This might look like it does not belong in the repository, but if we do this at the service level by loading all unanswered questions for a user and grouping via code, we risk loading ALL questions in the database in the worst case (e.g. if the user has not answered any question).

func (*Repository) Insert

func (r *Repository) Insert(ctx context.Context, q Question) error

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service represents the service that manages questions.

func NewService

func NewService(repo *Repository) *Service

NewService creates a new instance of question.Service.

func (Service) FromChoice

func (s Service) FromChoice(ctx context.Context, choiceID uuid.UUID) (*Question, error)

FromChoice returns the question associated with a given choice.

func (Service) FromChoices

func (s Service) FromChoices(ctx context.Context, ids ...uuid.UUID) ([]Question, error)

FromChoices returns the questions associated with a given set of choices.

func (Service) Insert

func (s Service) Insert(ctx context.Context, q *Question) error

Insert inserts a new question.

func (Service) Latest

func (s Service) Latest(ctx context.Context, topic enums.Topic, amount int) ([]Question, error)

Latest returns the most recent questions about a given topic, capped to amount.

func (Service) NextFor

func (s Service) NextFor(
	ctx context.Context,
	userID uuid.UUID,
	topic enums.Topic,
) (*Question, error)

NextFor returns the next question that a user should answer.

func (Service) RemainingTopicsFor

func (s Service) RemainingTopicsFor(
	ctx context.Context,
	userID uuid.UUID,
) (map[enums.Topic]uint, error)

RemainingTopicsFor returns a map such that each key is a topic for which the user still has unanswered questions, and each value is the amount of remaining questions for that topic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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