Paket data zu db umbenannt, den type Question zur SQL-Abfrage hinzugefügt, um Duplikate zu vermeiden und db.OpenDB() zu db.Open() umbenannt

This commit is contained in:
Jason Streifling 2023-10-19 20:04:32 +02:00
parent 6359caf3e9
commit 35d565ec7d
2 changed files with 10 additions and 48 deletions

View File

@ -1,42 +0,0 @@
package data
import "streifling.com/jason/sicherheitsunterweisung/packages/types"
func InitQuestions() []types.Question {
Q := make([]types.Question, 0)
Q = append(Q, types.Question{
Text: "Wie viel ist 1+1?",
Answers: []types.Answer{
{ID: 0, Text: "1"},
{ID: 1, Text: "2"},
{ID: 2, Text: "3"},
{ID: 3, Text: "4"},
},
Correct: 1,
})
Q = append(Q, types.Question{
Text: "Wie viel ist 2+2?",
Answers: []types.Answer{
{ID: 0, Text: "1"},
{ID: 1, Text: "2"},
{ID: 2, Text: "3"},
{ID: 3, Text: "4"},
},
Correct: 3,
})
Q = append(Q, types.Question{
Text: "Wie viel ist 1+2?",
Answers: []types.Answer{
{ID: 0, Text: "1"},
{ID: 1, Text: "2"},
{ID: 2, Text: "3"},
{ID: 3, Text: "4"},
},
Correct: 2,
})
return Q
}

View File

@ -1,4 +1,4 @@
package data
package db
import (
"bufio"
@ -8,10 +8,9 @@ import (
"strings"
"syscall"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
"github.com/go-sql-driver/mysql"
"golang.org/x/term"
"streifling.com/jason/sicherheitsunterweisung/packages/types"
)
type DB struct {
@ -60,7 +59,7 @@ func getCredentials() (string, string, error) {
return user, pass, nil
}
func OpenDB(dbName string) (*DB, error) {
func Open(dbName string) (*DB, error) {
var err error
db := new(DB)
@ -155,7 +154,7 @@ func (db *DB) WriteAllDataOfBriefing(b *types.Briefing, sp *[]*types.Participant
return nil
}
func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) {
func (db *DB) GetAllOverviewTableData() ([]*types.OverviewTableData, error) {
rows, err := db.Query(`
SELECT
i.first_name,
@ -173,8 +172,12 @@ func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) {
ON b.id = g.briefing_id
INNER JOIN participants AS p
ON p.id = g.participant_id
INNER JOIN questions AS q
ON q.id = g.question_id
INNER JOIN instructors AS i
ON i.id = b.instructor_id
WHERE
q.id = 1
ORDER BY
b.id DESC,
p.id
@ -207,7 +210,7 @@ func (db *DB) GetAllOverviewTableData() (*[]*types.OverviewTableData, error) {
data = append(data, otd)
}
return &data, nil
return data, nil
}
func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData, error) {
@ -231,6 +234,7 @@ func (db *DB) GetOverviewTableDataByName(n string) (*[]*types.OverviewTableData,
INNER JOIN instructors AS i
ON i.id = b.instructor_id
WHERE
q.id = 1 AND
i.first_name LIKE ? OR
i.last_name LIKE ? OR
p.first_name LIKE ? OR