Documentation ¶
Index ¶
- Variables
- func Limit(n int) qm.QueryMod
- func OrderByCreatedAtAsc() qm.QueryMod
- func OrderByCreatedAtDesc() qm.QueryMod
- func WhereChoiceIDIn(ids ...uuid.UUID) qm.QueryMod
- func WhereIDIn(ids ...uuid.UUID) qm.QueryMod
- func WhereNotAnsweredBy(userID uuid.UUID) qm.QueryMod
- func WhereTopicEq(topic enums.Topic) qm.QueryMod
- type Choice
- type Question
- type Repository
- func (r *Repository) GetMany(ctx context.Context, qms ...qm.QueryMod) ([]Question, error)
- func (r *Repository) GetOne(ctx context.Context, qms ...qm.QueryMod) (*Question, error)
- func (r *Repository) GetRemainingTopics(ctx context.Context, userID uuid.UUID) (map[enums.Topic]uint, error)
- func (r *Repository) Insert(ctx context.Context, q Question) error
- type Service
- func (s Service) FromChoice(ctx context.Context, choiceID uuid.UUID) (*Question, error)
- func (s Service) FromChoices(ctx context.Context, ids ...uuid.UUID) ([]Question, error)
- func (s Service) Insert(ctx context.Context, q *Question) error
- func (s Service) Latest(ctx context.Context, topic enums.Topic, amount int) ([]Question, error)
- func (s Service) NextFor(ctx context.Context, userID uuid.UUID, topic enums.Topic) (*Question, error)
- func (s Service) RemainingTopicsFor(ctx context.Context, userID uuid.UUID) (map[enums.Topic]uint, error)
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidRecord = errors.New("invalid question record")
var ErrNoQuestionsLeft = errors.New("no questions left")
ErrNoQuestionsLeft is returned when there are no questions left to be answered for a user.
var Module = fx.Module( "question", fx.Provide(fx.Private, NewRepository), fx.Provide(NewService), )
Functions ¶
func OrderByCreatedAtAsc ¶
func OrderByCreatedAtDesc ¶
Types ¶
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 (*Question) CorrectChoice ¶
CorrectChoices returns the choices that are correct.
func (*Question) Validate ¶
Validate checks if a question is valid or not, and returns an error if it's not.
func (*Question) WithChoice ¶
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.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
func NewRepository ¶
func NewRepository(db *sql.DB) *Repository
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).
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 ¶
FromChoice returns the question associated with a given choice.
func (Service) FromChoices ¶
FromChoices returns the questions associated with a given set of choices.
func (Service) Latest ¶
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.