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"
"fmt"
"html/template"
"log"
"net/http"
"strconv"
"strings"
@ -108,7 +107,7 @@ func newParticipant(l string) (*types.Participant, error) {
var err error
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 {
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
}
func DisplayParticipantForm(ls *[]string, cp chan<- *types.Participant) http.HandlerFunc {
func DisplayParticipantForm(sl *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := r.PostFormValue("login")
if loginIsCorrect(l, ls) {
p, err := newParticipant(l)
if loginIsCorrect(r.PostFormValue("login"), sl) {
uuid, err := generateUUID()
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", p.ID)
template.Must(template.ParseFiles("templates/participant.html")).ExecuteTemplate(w, "content", uuid)
} else {
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 {
v, err := strconv.Atoi(r.PostFormValue("answer"))
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