server.DisplayParticipantForm aufgeräumt

This commit is contained in:
Jason Streifling 2023-10-18 16:59:02 +02:00
parent b605217625
commit 82ced65513

View File

@ -5,7 +5,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"html/template" "html/template"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -108,7 +107,7 @@ func newParticipant(l string) (*types.Participant, error) {
var err error var err error
p := new(types.Participant) p := new(types.Participant)
p.ID, err = strconv.Atoi(strings.Split(l, "-")[0]) p.ID, err = strconv.ParseInt(strings.Split(l, "-")[0], 10, 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err) return nil, fmt.Errorf("newParticipant: strconv.Atoi(idString): %v\n", err)
} }
@ -116,18 +115,15 @@ func newParticipant(l string) (*types.Participant, error) {
return p, nil return p, nil
} }
func DisplayParticipantForm(ls *[]string, cp chan<- *types.Participant) http.HandlerFunc { func DisplayParticipantForm(sl *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
l := r.PostFormValue("login") if loginIsCorrect(r.PostFormValue("login"), sl) {
uuid, err := generateUUID()
if loginIsCorrect(l, ls) {
p, err := newParticipant(l)
if err != nil { if err != nil {
http.Error(w, "GetParticipantData: newParticipant(l): "+fmt.Sprint(err), http.StatusInternalServerError) http.Error(w, "DisplayParticipantForm: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError)
} }
cp <- p template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", uuid)
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", p.ID)
} else { } else {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil) template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
} }
@ -137,7 +133,7 @@ func DisplayParticipantForm(ls *[]string, cp chan<- *types.Participant) http.Han
func readAnswer(r *http.Request, p *types.Participant, i int) error { func readAnswer(r *http.Request, p *types.Participant, i int) error {
v, err := strconv.Atoi(r.PostFormValue("answer")) v, err := strconv.Atoi(r.PostFormValue("answer"))
if err != nil { if err != nil {
return fmt.Errorf("readAnswer: strconv.Atoi(r.PostFormValue(\"answer\")): %v\n", err) return fmt.Errorf("readAnswer: strconv.Atoi(): %v\n", err)
} }
p.Questions[i].Chosen = v p.Questions[i].Chosen = v