summaryrefslogtreecommitdiff
path: root/internal/database/repos/questions.go
blob: 9d0396ec039edb11e4422f383231c19337634f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package repos

import (
	"demoon/internal/models"
	"fmt"
)

type QuestionsRepo interface {
	DBGetQuestion(id string) (*models.Question, error)
	DBGetMixedUbung(id uint32) (*models.MixedUbung, error)
	DBListMixed(limit uint32) ([]models.MixedUbung, error)
	DBGetQuestionsByMixedID(id string) ([]models.Question, error)
}

func (p *Provider) DBGetMixedUbung(id uint32) (*models.MixedUbung, error) {
	var ubung models.MixedUbung
	err := p.db.Get(&ubung, "SELECT * FROM Table_Mixed WHERE mixedid = ?", id)
	if err != nil {
		return nil, err
	}
	return &ubung, nil
}

func (p *Provider) DBListMixed(limit uint32) ([]models.MixedUbung, error) {
	var ubungs []models.MixedUbung
	query := "SELECT * FROM Table_Mixed ORDER BY MixedID"
	if limit > 0 {
		query = fmt.Sprintf("SELECT * FROM Table_Mixed ORDER BY MixedID LIMIT %d", limit)
	}
	err := p.db.Select(&ubungs, query)
	if err != nil {
		return nil, err
	}
	return ubungs, nil
}

func (p *Provider) DBGetQuestionsByMixedID(id string) ([]models.Question, error) {
	var questions []models.Question
	err := p.db.Select(&questions, "SELECT * FROM questions WHERE mixed_id = ? ORDER BY id", id)
	if err != nil {
		return nil, err
	}
	return questions, nil
}

func (p *Provider) DBGetQuestion(id string) (*models.Question, error) {
	var question models.Question
	err := p.db.Get(&question, "SELECT * FROM questions WHERE id = ?", id)
	if err != nil {
		return nil, err
	}
	return &question, nil
}