67 lines
1.6 KiB
Go

package session
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)
}
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "content", bs)
}
func generateLogin() (string, error) {
bs := make([]byte, 4)
if _, err := rand.Read(bs); err != nil {
return "", fmt.Errorf("generateLogin: rand.Read(bs): %v\n", err)
}
return hex.EncodeToString(bs), nil
}
func findCorrectLogin(l string, ss *[]*Session) (*Session, *data.Participant, bool) {
for _, session := range *ss {
for _, p := range session.Participants {
if l == p.Login {
return session, p, true
}
}
}
return nil, nil, false
}
func newParticipant(l string) (*data.Participant, error) {
var err error
p := new(data.Participant)
p.ID, err = strconv.ParseInt(l, 10, 64)
if err != nil {
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
}
return p, nil
}
func handleGivenAnswer(s *Session, p *data.Participant, i int64, r *http.Request, db *data.DB) 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)
}
return nil
}