Kleine Bugfixes und Aufräumarbeiten

This commit is contained in:
Jason Streifling 2023-10-28 10:52:06 +02:00
parent 34ef95ad76
commit 33cbf4b215
11 changed files with 165 additions and 128 deletions

View File

@ -68,21 +68,23 @@ func (db *DB) WriteParticipant(p *Participant) error {
return nil return nil
} }
func (db *DB) WriteGivenAnswer(b *Briefing, p *Participant, q *Question, g int) error { func (db *DB) WriteGivenAnswers(b Briefing, p Participant, sq []Question, givenAnswers []int) error {
for i, q := range sq {
_, err := db.Exec(` _, err := db.Exec(`
INSERT INTO given_answers INSERT INTO given_answers
(briefing_id, participant_id, question_id, given_answer) (briefing_id, participant_id, question_id, given_answer)
VALUES VALUES
(?, ?, ?, ?) (?, ?, ?, ?)
`, b.ID, p.ID, q.ID, g) `, b.ID, p.ID, q.ID, givenAnswers[i])
if err != nil { if err != nil {
return fmt.Errorf("*DB.writeGivenAnswers: db.Exec(): %v\n", err) return fmt.Errorf("*DB.WriteGivenAnswers: db.Exec(): %v\n", err)
}
} }
return nil return nil
} }
func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) { func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
rows, err := db.Query(` rows, err := db.Query(`
SELECT SELECT
i.first_name, i.first_name,
@ -115,7 +117,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
} }
defer rows.Close() defer rows.Close()
data := make([]*OverviewTableData, 0) data := make([]OverviewTableData, 0)
for rows.Next() { for rows.Next() {
otd := new(OverviewTableData) otd := new(OverviewTableData)
@ -135,7 +137,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err) return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err)
} }
data = append(data, otd) data = append(data, *otd)
} }
return data, nil return data, nil

View File

@ -28,7 +28,15 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
(*ss) = append((*ss), session) (*ss) = append((*ss), session)
cs <- session cs <- session
displayTable(w, db) data := new(tableHTMLData)
data.SessionID = session.ID
data.OTD, err = db.GetAllOverviewTableData()
if err != nil {
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", data)
return return
} }
} }
@ -41,6 +49,7 @@ func HandleSearch(db *data.DB) http.HandlerFunc {
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search")) bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
if err != nil { if err != nil {
http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayResults: db.ReadByName(r.PostFormValue()): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs) template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
} }
@ -61,6 +70,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler
p.Login, err = generateLogin() p.Login, err = generateLogin()
if err != nil { if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
} }
s.Participants = append(s.Participants, p) s.Participants = append(s.Participants, p)
cp <- p cp <- p
@ -70,6 +80,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler
data.Login = p.Login data.Login = p.Login
if err != nil { if err != nil {
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
} }
template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", data)
@ -93,7 +104,13 @@ func (s *Session) HandleBriefingForm(db *data.DB) http.HandlerFunc {
log.Panicln(err) log.Panicln(err)
} }
displayTable(w, db) bs, err := db.GetAllOverviewTableData()
if err != nil {
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
} }
} }
@ -134,11 +151,10 @@ func (s *Session) HandleParticipant(db *data.DB, p *data.Participant, sq *[]data
} }
} }
func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Question, i int64) http.HandlerFunc { func (s *Session) HandleAnswer(db *data.DB, p *SessionParticipant, sq *[]data.Question, i int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
log.Println(i, len(*sq))
if i < int64(len(*sq)) { if i < int64(len(*sq)) {
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil { if err := handleGivenAnswer(s, p, i-1, r); err != nil {
http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayQuestion: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err) log.Panicln(err)
} }
@ -151,64 +167,44 @@ func (s *Session) HandleAnswer(db *data.DB, p *data.Participant, sq *[]data.Ques
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} else { } else {
if err := handleGivenAnswer(s, p, i-1, r, db); err != nil { if err := handleGivenAnswer(s, p, i-1, r); err != nil {
http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayTestResults: handleGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err) log.Panicln(err)
} }
log.Println(p.GivenAnswers)
givenAnswers, err := db.GetGivenAnswers(s.Briefing.ID, p.ID, s.Questions)
if err != nil {
http.Error(w, "DisplayTestResults: db.GetGivenAnswers(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
data := new(resultHTMLData) data := new(resultHTMLData)
data.SessionID = s.ID data.SessionID = s.ID
data.Login = p.Login data.Login = p.Login
data.Incorrect = 0 data.Incorrect = 0
data.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
data.Questions = make([]htmlQuestion, 0) for i, q := range *sq {
for i, q := range s.Questions { if p.GivenAnswers[i] != q.Correct {
question := new(htmlQuestion)
question.Text = q.Text
question.Answers = make([]htmlAnswer, 0)
for j, a := range q.Answers {
answer := new(htmlAnswer)
answer.Text = a.Text
if j+1 == q.Correct {
answer.Correct = true
} else {
answer.Correct = false
}
if j+1 == givenAnswers[i] {
answer.Chosen = true
} else {
answer.Chosen = false
}
question.Answers = append(question.Answers, *answer)
}
data.Questions = append(data.Questions, *question)
if givenAnswers[i] != q.Correct {
data.Incorrect++ data.Incorrect++
} }
} }
if data.Incorrect == 0 {
if err := db.WriteGivenAnswers(*s.Briefing, *p.Participant, s.Questions, p.GivenAnswers); err != nil {
http.Error(w, "HandleAnswer: db.WriteGivenAnswer(): "+fmt.Sprint(err), http.StatusInternalServerError)
log.Panicln(err)
}
}
template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/result.html")).ExecuteTemplate(w, "content", data)
} }
} }
} }
func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question) http.HandlerFunc { func (s *Session) HandleRetry(p *data.Participant, sq *[]data.Question, i *int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
(*i) = 0
data := new(questionHTMLData) data := new(questionHTMLData)
data.SessionID = s.ID data.SessionID = s.ID
data.Login = p.Login data.Login = p.Login
data.Question = (*sq)[0] data.Question = (*sq)[*i]
data.QuestionID = 1 data.QuestionID = int64(*i + 1)
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
} }

View File

@ -4,19 +4,25 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"strconv" "strconv"
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
) )
func displayTable(w http.ResponseWriter, db *data.DB) { func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) {
bs, err := db.GetAllOverviewTableData() for p := range cp {
if err != nil { sessionParticipant := new(SessionParticipant)
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError) sessionParticipant.Participant = p
sessionParticipant.GivenAnswers = make([]int, len(s.Questions))
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleParticipant(db, p, &s.Questions))
var i int
for i = range s.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, sessionParticipant, &s.Questions, int64(i+1)))
}
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(p.Login)+"/", s.HandleRetry(p, &s.Questions, &i))
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
} }
func generateLogin() (string, error) { func generateLogin() (string, error) {
@ -52,15 +58,42 @@ func newParticipant(l string) (*data.Participant, error) {
return p, nil return p, nil
} }
func handleGivenAnswer(s *Session, p *data.Participant, i int64, r *http.Request, db *data.DB) error { func handleGivenAnswer(s *Session, p *SessionParticipant, i int64, r *http.Request) error {
answer, err := strconv.Atoi(r.PostFormValue("answer")) answer, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil { if err != nil {
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err) return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err)
} }
if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil { p.GivenAnswers[i] = answer
return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err)
}
return nil return nil
} }
func makeHTMLQuestions(sq []data.Question, givenAnswers []int) []htmlQuestion {
questions := make([]htmlQuestion, 0)
for i, q := range sq {
question := new(htmlQuestion)
question.Text = q.Text
question.Answers = make([]htmlAnswer, 0)
for j, a := range q.Answers {
answer := new(htmlAnswer)
answer.Text = a.Text
if j+1 == q.Correct {
answer.Correct = true
} else {
answer.Correct = false
}
if j+1 == givenAnswers[i] {
answer.Chosen = true
} else {
answer.Chosen = false
}
question.Answers = append(question.Answers, *answer)
}
questions = append(questions, *question)
}
return questions
}

View File

@ -5,11 +5,9 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
) )
type Session struct { type tableHTMLData struct {
ID uuid.UUID SessionID uuid.UUID
*data.Briefing OTD []data.OverviewTableData
Participants []*data.Participant
Questions []data.Question
} }
type briefingHTMLData struct { type briefingHTMLData struct {

View File

@ -8,26 +8,12 @@ import (
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
) )
type Mux struct {
*http.ServeMux
}
func NewMux() *Mux { func NewMux() *Mux {
mux := new(Mux) mux := new(Mux)
mux.ServeMux = http.NewServeMux() mux.ServeMux = http.NewServeMux()
return mux return mux
} }
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) {
for participant := range cp {
mux.HandleFunc("/submit-participant/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleParticipant(db, participant, &s.Questions))
for i := range s.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/"+fmt.Sprint(i+1)+"/", s.HandleAnswer(db, participant, &s.Questions, int64(i+1)))
}
mux.HandleFunc("/retry/"+fmt.Sprint(s.ID)+"/"+fmt.Sprint(participant.Login)+"/", s.HandleRetry(participant, &s.Questions))
}
}
func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) { func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session) {
for s := range cs { for s := range cs {
(*ss) = append(*ss, s) (*ss) = append(*ss, s)
@ -44,7 +30,7 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
log.Fatalln(err) log.Fatalln(err)
} }
mux.HandleFunc("/new-briefing/", s.HandleNewBriefing()) mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.ID)+"/", s.HandleNewBriefing())
mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan)) mux.HandleFunc("/new-participant/"+fmt.Sprint(s.ID)+"/", s.HandleNewParticipant(participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db)) mux.HandleFunc("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))

View File

@ -0,0 +1,23 @@
package session
import (
"github.com/google/uuid"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
)
type Mux struct {
*http.ServeMux
}
type Session struct {
ID uuid.UUID
*data.Briefing
Participants []*data.Participant
Questions []data.Question
}
type SessionParticipant struct {
*data.Participant
GivenAnswers []int
}

View File

@ -1,5 +1,5 @@
{{define "rows"}} {{define "rows"}}
{{ range . }} {{range .OTD}}
<tr> <tr>
<td>{{.InstructorFirstName}}</td> <td>{{.InstructorFirstName}}</td>
<td>{{.InstructorLastName}}</td> <td>{{.InstructorLastName}}</td>
@ -18,12 +18,11 @@
{{define "content"}} {{define "content"}}
<form> <form>
<label for="search-input">Suche</label> <label for="search-input">Suche</label>
<input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results" hx-swap="innerHTML" <input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results">
hx-trigger="keyup changed delay:200ms" />
</form> </form>
<form> <form>
<button type="submit" hx-post="/new-briefing/" hx-target="#content" hx-swap="innerHTML"> <button type="submit" hx-post="/new-briefing/{{.SessionID}}" hx-target="#content">
Neue Unterweisung Neue Unterweisung
</button> </button>
</form> </form>