2023-11-01 14:11:00 +01:00
|
|
|
/*
|
|
|
|
* Sicherheitsunterweisung
|
|
|
|
* Copyright (C) 2023 Jason Streifling
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
package data
|
2023-10-05 17:42:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func OpenDB(dbName string) (*DB, error) {
|
2023-10-05 17:42:47 +02:00
|
|
|
var err error
|
2023-10-07 16:58:34 +02:00
|
|
|
db := new(DB)
|
2023-10-05 17:42:47 +02:00
|
|
|
|
|
|
|
cfg := mysql.NewConfig()
|
|
|
|
cfg.DBName = dbName
|
|
|
|
cfg.User, cfg.Passwd, err = getCredentials()
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: OpenDB: getCredentials(): %v", err)
|
2023-10-05 17:42:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 19:52:11 +02:00
|
|
|
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
2023-10-05 17:42:47 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: OpenDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v", err)
|
2023-10-05 17:42:47 +02:00
|
|
|
}
|
|
|
|
if err := db.Ping(); err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: OpenDB: db.Ping(): %v", err)
|
2023-10-05 17:42:47 +02:00
|
|
|
}
|
|
|
|
|
2023-10-07 16:58:34 +02:00
|
|
|
return db, nil
|
2023-10-05 17:42:47 +02:00
|
|
|
}
|
2023-10-05 19:52:11 +02:00
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (db *DB) WriteBriefing(b *Briefing) error {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-17 18:28:09 +02:00
|
|
|
INSERT INTO briefings
|
2023-11-01 13:11:50 +01:00
|
|
|
(datetime, location, document, as_of, instructor_id)
|
2023-10-17 18:28:09 +02:00
|
|
|
VALUES
|
2023-11-01 13:11:50 +01:00
|
|
|
(?, ?, ?, ?, ?)
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
2023-11-01 13:11:50 +01:00
|
|
|
result, err := db.Exec(query, b.DateTime, b.Location, b.Document, b.AsOf, b.InstructorID)
|
2023-10-17 18:28:09 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return fmt.Errorf("error: *DB.writeBriefing: db.Exec(): %v", err)
|
2023-10-17 18:28:09 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 15:22:45 +02:00
|
|
|
b.ID, err = result.LastInsertId()
|
2023-10-17 18:28:09 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return fmt.Errorf("error: *DB.writeBriefing: result.LastInsertId(): %v", err)
|
2023-10-17 18:28:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (db *DB) WriteParticipant(p *Participant) error {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-19 15:22:45 +02:00
|
|
|
INSERT INTO participants
|
|
|
|
(first_name, last_name, company)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?)
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
result, err := db.Exec(query, p.FirstName, p.LastName, p.Company)
|
2023-10-19 15:22:45 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return fmt.Errorf("error: *DB.writeParticipants: db.Exec(): %v", err)
|
2023-10-19 15:22:45 +02:00
|
|
|
}
|
2023-10-06 19:07:56 +02:00
|
|
|
|
2023-10-19 15:22:45 +02:00
|
|
|
p.ID, err = result.LastInsertId()
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return fmt.Errorf("error: *DB.writeParticipants: result.LastInsertId(): %v", err)
|
2023-10-17 18:28:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 10:52:06 +02:00
|
|
|
func (db *DB) WriteGivenAnswers(b Briefing, p Participant, sq []Question, givenAnswers []int) error {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-28 10:52:06 +02:00
|
|
|
INSERT INTO given_answers
|
|
|
|
(briefing_id, participant_id, question_id, given_answer)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?)
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
for i, q := range sq {
|
|
|
|
_, err := db.Exec(query, b.ID, p.ID, q.ID, givenAnswers[i])
|
2023-10-28 10:52:06 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return fmt.Errorf("error: *DB.WriteGivenAnswers: db.Exec(): %v", err)
|
2023-10-28 10:52:06 +02:00
|
|
|
}
|
2023-10-05 19:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-06 19:07:56 +02:00
|
|
|
|
2023-10-28 10:52:06 +02:00
|
|
|
func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-17 17:30:39 +02:00
|
|
|
SELECT
|
|
|
|
i.first_name,
|
|
|
|
i.last_name,
|
2023-11-01 13:11:50 +01:00
|
|
|
b.datetime,
|
2023-10-17 17:30:39 +02:00
|
|
|
b.location,
|
2023-11-01 13:11:50 +01:00
|
|
|
b.document,
|
2023-10-17 17:30:39 +02:00
|
|
|
b.as_of,
|
|
|
|
p.first_name,
|
|
|
|
p.last_name,
|
|
|
|
p.company
|
|
|
|
FROM given_answers AS g
|
|
|
|
INNER JOIN briefings AS b
|
|
|
|
ON b.id = g.briefing_id
|
|
|
|
INNER JOIN participants AS p
|
|
|
|
ON p.id = g.participant_id
|
2023-10-19 20:04:32 +02:00
|
|
|
INNER JOIN questions AS q
|
|
|
|
ON q.id = g.question_id
|
2023-10-17 17:30:39 +02:00
|
|
|
INNER JOIN instructors AS i
|
|
|
|
ON i.id = b.instructor_id
|
2023-10-19 20:04:32 +02:00
|
|
|
WHERE
|
|
|
|
q.id = 1
|
2023-10-17 17:30:39 +02:00
|
|
|
ORDER BY
|
|
|
|
b.id DESC,
|
2023-10-17 17:45:26 +02:00
|
|
|
p.id
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
rows, err := db.Query(query)
|
2023-10-07 17:22:55 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: db.Query(): %v", err)
|
2023-10-07 17:22:55 +02:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-10-28 10:52:06 +02:00
|
|
|
data := make([]OverviewTableData, 0)
|
2023-10-07 17:22:55 +02:00
|
|
|
for rows.Next() {
|
2023-11-01 09:18:48 +01:00
|
|
|
otd := OverviewTableData{}
|
2023-10-17 17:30:39 +02:00
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&otd.InstructorFirstName,
|
|
|
|
&otd.InstructorLastName,
|
2023-11-01 13:11:50 +01:00
|
|
|
&otd.BriefingDateTime,
|
2023-10-17 17:30:39 +02:00
|
|
|
&otd.BriefingLocation,
|
2023-11-01 13:11:50 +01:00
|
|
|
&otd.BriefingDocument,
|
2023-10-17 17:30:39 +02:00
|
|
|
&otd.BriefingAsOf,
|
|
|
|
&otd.ParticipantFirstName,
|
|
|
|
&otd.ParticipantLastName,
|
|
|
|
&otd.ParticipantCompany,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
2023-10-07 17:22:55 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 09:18:48 +01:00
|
|
|
data = append(data, otd)
|
2023-10-07 17:22:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 20:04:32 +02:00
|
|
|
return data, nil
|
2023-10-07 17:22:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 11:38:56 +01:00
|
|
|
func (db *DB) GetOverviewTableDataByName(n string) ([]OverviewTableData, error) {
|
|
|
|
query := `
|
2023-10-17 17:45:26 +02:00
|
|
|
SELECT
|
|
|
|
i.first_name,
|
|
|
|
i.last_name,
|
2023-11-01 13:11:50 +01:00
|
|
|
b.datetime,
|
2023-10-17 17:45:26 +02:00
|
|
|
b.location,
|
2023-11-01 13:11:50 +01:00
|
|
|
b.document,
|
2023-10-17 17:45:26 +02:00
|
|
|
b.as_of,
|
|
|
|
p.first_name,
|
|
|
|
p.last_name,
|
|
|
|
p.company
|
|
|
|
FROM given_answers AS g
|
|
|
|
INNER JOIN briefings AS b
|
|
|
|
ON b.id = g.briefing_id
|
|
|
|
INNER JOIN participants AS p
|
|
|
|
ON p.id = g.participant_id
|
2023-11-01 08:11:37 +01:00
|
|
|
INNER JOIN questions AS q
|
|
|
|
ON q.id = g.question_id
|
2023-10-17 17:45:26 +02:00
|
|
|
INNER JOIN instructors AS i
|
|
|
|
ON i.id = b.instructor_id
|
|
|
|
WHERE
|
2023-10-19 20:04:32 +02:00
|
|
|
q.id = 1 AND
|
2023-11-01 08:11:37 +01:00
|
|
|
(
|
|
|
|
i.first_name LIKE ? OR
|
|
|
|
i.last_name LIKE ? OR
|
|
|
|
p.first_name LIKE ? OR
|
|
|
|
p.last_name LIKE ?
|
|
|
|
)
|
2023-10-17 17:45:26 +02:00
|
|
|
ORDER BY
|
|
|
|
b.id DESC,
|
|
|
|
p.id
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
rows, err := db.Query(query, "%"+n+"%", "%"+n+"%", "%"+n+"%", "%"+n+"%")
|
2023-10-06 19:07:56 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetOverviewTableDataByName: db.Query(): %v", err)
|
2023-10-06 19:07:56 +02:00
|
|
|
}
|
2023-10-07 16:58:34 +02:00
|
|
|
defer rows.Close()
|
2023-10-06 19:07:56 +02:00
|
|
|
|
2023-10-30 11:38:56 +01:00
|
|
|
data := make([]OverviewTableData, 0)
|
2023-10-06 19:07:56 +02:00
|
|
|
for rows.Next() {
|
2023-11-01 09:18:48 +01:00
|
|
|
otd := OverviewTableData{}
|
2023-10-17 17:45:26 +02:00
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&otd.InstructorFirstName,
|
|
|
|
&otd.InstructorLastName,
|
2023-11-01 13:11:50 +01:00
|
|
|
&otd.BriefingDateTime,
|
2023-10-17 17:45:26 +02:00
|
|
|
&otd.BriefingLocation,
|
2023-11-01 13:11:50 +01:00
|
|
|
&otd.BriefingDocument,
|
2023-10-17 17:45:26 +02:00
|
|
|
&otd.BriefingAsOf,
|
|
|
|
&otd.ParticipantFirstName,
|
|
|
|
&otd.ParticipantLastName,
|
|
|
|
&otd.ParticipantCompany,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.ReadAllBriefings: rows.Scan(): %v", err)
|
2023-10-06 19:07:56 +02:00
|
|
|
}
|
2023-10-17 17:45:26 +02:00
|
|
|
|
2023-11-01 09:18:48 +01:00
|
|
|
data = append(data, otd)
|
2023-10-16 18:51:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 11:38:56 +01:00
|
|
|
return data, nil
|
2023-10-16 18:51:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 16:21:44 +02:00
|
|
|
func (db *DB) GetLastID(table string) (int, error) {
|
|
|
|
var id int
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-18 16:21:44 +02:00
|
|
|
SELECT id
|
|
|
|
FROM ?
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT 0, 1
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
2023-10-18 16:21:44 +02:00
|
|
|
|
2023-10-30 11:38:56 +01:00
|
|
|
row := db.QueryRow(query, table)
|
2023-10-18 16:21:44 +02:00
|
|
|
if err := row.Scan(&id); err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return -1, fmt.Errorf("error: *DB.GetLastID: row.Scan(): %v", err)
|
2023-10-06 19:07:56 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 16:21:44 +02:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (db *DB) GetInstructors() ([]*Instructor, error) {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-18 16:21:44 +02:00
|
|
|
SELECT *
|
|
|
|
FROM instructors
|
2023-10-20 16:33:00 +02:00
|
|
|
ORDER BY
|
|
|
|
last_name,
|
|
|
|
first_name
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
rows, err := db.Query(query)
|
2023-10-18 16:21:44 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetInstructors: db.Query(): %v", err)
|
2023-10-18 16:21:44 +02:00
|
|
|
}
|
2023-10-28 08:01:34 +02:00
|
|
|
defer rows.Close()
|
2023-10-18 16:21:44 +02:00
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
instructors := make([]*Instructor, 0)
|
2023-10-18 16:21:44 +02:00
|
|
|
for rows.Next() {
|
2023-10-28 08:49:28 +02:00
|
|
|
instructor := new(Instructor)
|
2023-11-01 09:18:48 +01:00
|
|
|
|
2023-10-28 08:01:34 +02:00
|
|
|
if err = rows.Scan(&instructor.ID, &instructor.FirstName, &instructor.LastName); err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetInstructors: rows.Scan(): %v", err)
|
2023-10-18 16:21:44 +02:00
|
|
|
}
|
2023-11-01 09:18:48 +01:00
|
|
|
|
2023-10-18 16:21:44 +02:00
|
|
|
instructors = append(instructors, instructor)
|
|
|
|
}
|
|
|
|
|
2023-10-20 16:33:00 +02:00
|
|
|
return instructors, nil
|
2023-10-06 19:07:56 +02:00
|
|
|
}
|
2023-10-28 08:01:34 +02:00
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (db *DB) GetQuestions(nums []string) ([]Question, error) {
|
2023-10-30 11:38:56 +01:00
|
|
|
query := `
|
2023-10-28 08:01:34 +02:00
|
|
|
SELECT *
|
|
|
|
FROM questions
|
|
|
|
WHERE id IN (` + strings.Join(nums, ", ") + `)
|
2023-10-30 11:38:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
rows, err := db.Query(query)
|
2023-10-28 08:01:34 +02:00
|
|
|
if err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetQuestions: db.Query(): %v", err)
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-10-30 11:38:56 +01:00
|
|
|
// TODO: not scalable
|
2023-10-28 08:49:28 +02:00
|
|
|
questions := make([]Question, 0)
|
2023-10-28 08:01:34 +02:00
|
|
|
for rows.Next() {
|
2023-11-01 11:56:44 +01:00
|
|
|
q := Question{Answers: make([]Answer, 4)}
|
2023-11-01 13:11:50 +01:00
|
|
|
q.Answers[0].ID = 1
|
|
|
|
q.Answers[1].ID = 2
|
|
|
|
q.Answers[2].ID = 3
|
|
|
|
q.Answers[3].ID = 4
|
2023-10-28 08:01:34 +02:00
|
|
|
|
2023-11-01 13:11:50 +01:00
|
|
|
if err := rows.Scan(&q.ID, &q.Text, &q.Answers[0].Text, &q.Answers[1].Text, &q.Answers[2].Text, &q.Answers[3].Text, &q.Correct); err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetQuestions: rows.Scan(): %v", err)
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 09:18:48 +01:00
|
|
|
questions = append(questions, q)
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return questions, nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 08:49:28 +02:00
|
|
|
func (db *DB) GetGivenAnswers(bid, pid int64, sq []Question) ([]int, error) {
|
2023-11-01 09:23:10 +01:00
|
|
|
answers := make([]int, len(sq))
|
2023-10-28 08:01:34 +02:00
|
|
|
query := `
|
|
|
|
SELECT given_answer
|
|
|
|
FROM given_answers
|
|
|
|
WHERE
|
|
|
|
briefing_id = ? AND
|
|
|
|
participant_id = ? AND
|
|
|
|
question_id = ?
|
|
|
|
`
|
|
|
|
|
2023-11-01 09:23:10 +01:00
|
|
|
for i, q := range sq {
|
2023-10-28 08:01:34 +02:00
|
|
|
row := db.QueryRow(query, bid, pid, q.ID)
|
2023-11-01 11:43:06 +01:00
|
|
|
if err := row.Scan(&answers[i]); err != nil {
|
2023-10-30 11:38:56 +01:00
|
|
|
return nil, fmt.Errorf("error: *DB.GetGivenAnswers: row.Scan(): %v", err)
|
2023-10-28 08:01:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers, nil
|
|
|
|
}
|