Kleine Bugfixes und Aufräumarbeiten
This commit is contained in:
parent
34ef95ad76
commit
33cbf4b215
@ -68,21 +68,23 @@ func (db *DB) WriteParticipant(p *Participant) error {
|
||||
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(`
|
||||
INSERT INTO given_answers
|
||||
(briefing_id, participant_id, question_id, given_answer)
|
||||
VALUES
|
||||
(?, ?, ?, ?)
|
||||
`, b.ID, p.ID, q.ID, g)
|
||||
`, b.ID, p.ID, q.ID, givenAnswers[i])
|
||||
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
|
||||
}
|
||||
|
||||
func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
|
||||
func (db *DB) GetAllOverviewTableData() ([]OverviewTableData, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
i.first_name,
|
||||
@ -115,7 +117,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
data := make([]*OverviewTableData, 0)
|
||||
data := make([]OverviewTableData, 0)
|
||||
for rows.Next() {
|
||||
otd := new(OverviewTableData)
|
||||
|
||||
@ -135,7 +137,7 @@ func (db *DB) GetAllOverviewTableData() ([]*OverviewTableData, error) {
|
||||
return nil, fmt.Errorf("*DB.ReadAllBriefings: rows.Scan(): %v\n", err)
|
||||
}
|
||||
|
||||
data = append(data, otd)
|
||||
data = append(data, *otd)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
|
@ -28,7 +28,15 @@ func HandleInternalLogin(db *data.DB, ss *[]*Session, cs chan<- *Session) http.H
|
||||
(*ss) = append((*ss), 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
|
||||
}
|
||||
}
|
||||
@ -41,6 +49,7 @@ func HandleSearch(db *data.DB) http.HandlerFunc {
|
||||
bs, err := db.GetOverviewTableDataByName(r.PostFormValue("search"))
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
@ -61,6 +70,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler
|
||||
p.Login, err = generateLogin()
|
||||
if err != nil {
|
||||
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
s.Participants = append(s.Participants, p)
|
||||
cp <- p
|
||||
@ -70,6 +80,7 @@ func (s *Session) HandleNewParticipant(cp chan<- *data.Participant) http.Handler
|
||||
data.Login = p.Login
|
||||
if err != nil {
|
||||
http.Error(w, "AddParticipant: generateLogin(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
log.Println(i, 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)
|
||||
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)
|
||||
} 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)
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
log.Println(p.GivenAnswers)
|
||||
|
||||
data := new(resultHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Incorrect = 0
|
||||
|
||||
data.Questions = make([]htmlQuestion, 0)
|
||||
for i, q := range s.Questions {
|
||||
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.Questions = makeHTMLQuestions(s.Questions, p.GivenAnswers)
|
||||
for i, q := range *sq {
|
||||
if p.GivenAnswers[i] != q.Correct {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
(*i) = 0
|
||||
|
||||
data := new(questionHTMLData)
|
||||
data.SessionID = s.ID
|
||||
data.Login = p.Login
|
||||
data.Question = (*sq)[0]
|
||||
data.QuestionID = 1
|
||||
data.Question = (*sq)[*i]
|
||||
data.QuestionID = int64(*i + 1)
|
||||
|
||||
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data)
|
||||
}
|
||||
|
@ -4,19 +4,25 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
func displayTable(w http.ResponseWriter, db *data.DB) {
|
||||
bs, err := db.GetAllOverviewTableData()
|
||||
if err != nil {
|
||||
http.Error(w, "displayTable: *DB.GetAllOverviewTableData(): "+fmt.Sprint(err), http.StatusInternalServerError)
|
||||
func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *data.Participant, s *Session) {
|
||||
for p := range cp {
|
||||
sessionParticipant := new(SessionParticipant)
|
||||
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) {
|
||||
@ -52,15 +58,42 @@ func newParticipant(l string) (*data.Participant, error) {
|
||||
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"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: strconv.Atoi(): %v\n", err)
|
||||
}
|
||||
|
||||
if err := db.WriteGivenAnswer(s.Briefing, p, &s.Questions[i], answer); err != nil {
|
||||
return fmt.Errorf("handleGivenAnswer: db.WriteGivenAnswer(): %v\n", err)
|
||||
}
|
||||
|
||||
p.GivenAnswers[i] = answer
|
||||
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
|
||||
}
|
||||
|
@ -5,11 +5,9 @@ import (
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
ID uuid.UUID
|
||||
*data.Briefing
|
||||
Participants []*data.Participant
|
||||
Questions []data.Question
|
||||
type tableHTMLData struct {
|
||||
SessionID uuid.UUID
|
||||
OTD []data.OverviewTableData
|
||||
}
|
||||
|
||||
type briefingHTMLData struct {
|
||||
|
@ -8,26 +8,12 @@ import (
|
||||
"streifling.com/jason/sicherheitsunterweisung/packages/data"
|
||||
)
|
||||
|
||||
type Mux struct {
|
||||
*http.ServeMux
|
||||
}
|
||||
|
||||
func NewMux() *Mux {
|
||||
mux := new(Mux)
|
||||
mux.ServeMux = http.NewServeMux()
|
||||
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) {
|
||||
for s := range cs {
|
||||
(*ss) = append(*ss, s)
|
||||
@ -44,7 +30,7 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
|
||||
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("/submit-form/"+fmt.Sprint(s.ID)+"/", s.HandleBriefingForm(db))
|
||||
|
23
packages/session/sessionStructs.go
Normal file
23
packages/session/sessionStructs.go
Normal 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
|
||||
}
|
@ -1,21 +1,21 @@
|
||||
{{ define "add-buttons" }}
|
||||
{{define "add-buttons"}}
|
||||
<div id="briefing-buttons">
|
||||
<button type="button" hx-post="/new-participant/{{ .SessionID }}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
|
||||
<button type="button" hx-post="/new-participant/{{.SessionID}}/" hx-target="#briefing-buttons" hx-swap="outerHTML">
|
||||
Neuer Teilnehmer
|
||||
</button>
|
||||
|
||||
<button type="submit" hx-post="/submit-form/{{ .SessionID }}/" hx-target="#content" hx-swap="innerHTML">
|
||||
<button type="submit" hx-post="/submit-form/{{.SessionID}}/" hx-target="#content" hx-swap="innerHTML">
|
||||
Fertig
|
||||
</button>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
||||
{{ define "new" }}
|
||||
{{ template "add-buttons" . }}
|
||||
<p>{{ .Login }}</p>
|
||||
{{ end }}
|
||||
{{define "new"}}
|
||||
{{template "add-buttons" .}}
|
||||
<p>{{.Login}}</p>
|
||||
{{end}}
|
||||
|
||||
{{ define "content" }}
|
||||
{{define "content"}}
|
||||
<form>
|
||||
<div>
|
||||
<label for="location">Ort</label>
|
||||
@ -32,6 +32,6 @@
|
||||
<input id="as-of" name="as-of" required type="date" />
|
||||
</div>
|
||||
|
||||
{{ template "add-buttons" . }}
|
||||
{{template "add-buttons" .}}
|
||||
</form>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
@ -12,7 +12,7 @@
|
||||
<h1>Sicherheitsunterweisung</h1>
|
||||
|
||||
<div id="content">
|
||||
{{ template "content" . }}
|
||||
{{template "content" .}}
|
||||
</div>
|
||||
|
||||
<script src="/static/js/htmx.min.js" type="text/javascript"></script>
|
||||
|
@ -1,4 +1,4 @@
|
||||
{{ define "content" }}
|
||||
{{define "content"}}
|
||||
<h2>Anmeldung</h2>
|
||||
|
||||
<form>
|
||||
@ -14,4 +14,4 @@
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
@ -1,16 +1,16 @@
|
||||
{{ define "content" }}
|
||||
{{define "content"}}
|
||||
<form>
|
||||
<label for="first-{{ .Login }}">Vorname</label>
|
||||
<input type="text" name="first-{{ .Login }}" id="first-{{ .Login }}" />
|
||||
<label for="first-{{.Login}}">Vorname</label>
|
||||
<input type="text" name="first-{{.Login}}" id="first-{{.Login}}" />
|
||||
|
||||
<label for="last-{{ .Login }}">Nachname</label>
|
||||
<input type="text" name="last-{{ .Login }}" id="last-{{ .Login }}" />
|
||||
<label for="last-{{.Login}}">Nachname</label>
|
||||
<input type="text" name="last-{{.Login}}" id="last-{{.Login}}" />
|
||||
|
||||
<label for="company-{{ .Login }}">Firma</label>
|
||||
<input type="text" name="company-{{ .Login }}" id="company-{{ .Login }}" />
|
||||
<label for="company-{{.Login}}">Firma</label>
|
||||
<input type="text" name="company-{{.Login}}" id="company-{{.Login}}" />
|
||||
|
||||
<button type="button" hx-post="/submit-participant/{{ .SessionID }}/{{ .Login }}/" hx-target="#content">
|
||||
<button type="button" hx-post="/submit-participant/{{.SessionID}}/{{.Login}}/" hx-target="#content">
|
||||
Fertig
|
||||
</button>
|
||||
</form>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
@ -1,29 +1,28 @@
|
||||
{{ define "rows" }}
|
||||
{{ range . }}
|
||||
{{define "rows"}}
|
||||
{{range .OTD}}
|
||||
<tr>
|
||||
<td>{{ .InstructorFirstName }}</td>
|
||||
<td>{{ .InstructorLastName }}</td>
|
||||
<td>{{ .BriefingDate }}</td>
|
||||
<td>{{ .BriefingTime }}</td>
|
||||
<td>{{ .BriefingLocation }}</td>
|
||||
<td>{{ .BriefingDocumentName }}</td>
|
||||
<td>{{ .BriefingAsOf }}</td>
|
||||
<td>{{ .ParticipantFirstName }}</td>
|
||||
<td>{{ .ParticipantLastName }}</td>
|
||||
<td>{{ .ParticipantCompany }}</td>
|
||||
<td>{{.InstructorFirstName}}</td>
|
||||
<td>{{.InstructorLastName}}</td>
|
||||
<td>{{.BriefingDate}}</td>
|
||||
<td>{{.BriefingTime}}</td>
|
||||
<td>{{.BriefingLocation}}</td>
|
||||
<td>{{.BriefingDocumentName}}</td>
|
||||
<td>{{.BriefingAsOf}}</td>
|
||||
<td>{{.ParticipantFirstName}}</td>
|
||||
<td>{{.ParticipantLastName}}</td>
|
||||
<td>{{.ParticipantCompany}}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{ define "content" }}
|
||||
{{define "content"}}
|
||||
<form>
|
||||
<label for="search-input">Suche</label>
|
||||
<input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results" hx-swap="innerHTML"
|
||||
hx-trigger="keyup changed delay:200ms" />
|
||||
<input type="text" name="search" id="search-input" hx-post="/search/" hx-target="#results">
|
||||
</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
|
||||
</button>
|
||||
</form>
|
||||
@ -43,7 +42,7 @@
|
||||
</thead>
|
||||
|
||||
<tbody id="results">
|
||||
{{ template "rows" . }}
|
||||
{{template "rows" .}}
|
||||
</tbody>
|
||||
</table>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user