From 39d8108521a9486a4bd7d5349d532f05fdb32613 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Wed, 18 Oct 2023 17:08:27 +0200 Subject: [PATCH] =?UTF-8?q?Viel=20Code=20erstmal=20auskommentiert,=20um=20?= =?UTF-8?q?der=20Logik=20besser=20folgen=20zu=20k=C3=B6nnen=20+=20ein=20pa?= =?UTF-8?q?ar=20=C3=84nderungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 49 ++++++------- packages/server/server.go | 140 +++++++++++++++++++------------------- 2 files changed, 91 insertions(+), 98 deletions(-) diff --git a/main.go b/main.go index ca55fdb..80be3a2 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "html/template" "log" "net/http" @@ -11,22 +10,24 @@ import ( "streifling.com/jason/sicherheitsunterweisung/packages/types" ) -func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) { - for p := range cp { - p.Questions = data.InitQuestions() - sp = append(sp, p) - - var i int - for i = range p.Questions { - m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p)) - } - m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p)) - } -} +// func waitForParticipants(sb []*types.Briefing, sp []*types.Participant, cp <-chan *types.Participant, m *http.ServeMux) { +// for p := range cp { +// sg := make([]*types.GivenAnswer, 4) +// +// p.Questions = data.InitQuestions() +// sp = append(sp, p) +// +// var i int +// for i = range p.Questions { +// m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i)+"/", server.DisplayQuestion(i, p)) +// } +// m.HandleFunc("/display-question-"+fmt.Sprintf("%d", p.ID)+"-"+fmt.Sprintf("%d", i+1)+"/", server.DisplayTestResults(sb, p)) +// } +// } func main() { logins := make([]string, 0) - participants := make([]*types.Participant, 0) + // participants := make([]*types.Participant, 0) briefings := make([]*types.Briefing, 0) mux := http.NewServeMux() @@ -35,26 +36,20 @@ func main() { log.Fatalln(err) } - var i, j int64 - if err := db.GetLastID(&i); err != nil { - log.Fatalln(err) - } - j = i - - participantChan := make(chan *types.Participant) - defer close(participantChan) - go waitForParticipants(briefings, participants, participantChan, mux) + // participantChan := make(chan *types.Participant) + // defer close(participantChan) + // go waitForParticipants(briefings, participants, participantChan, mux) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil) }) mux.HandleFunc("/search/", server.DisplaySearchResults(db)) - mux.HandleFunc("/new-briefing/", server.DisplayForm(&i)) - mux.HandleFunc("/add-participant/", server.AddParticipant(&i, &logins)) - mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j)) + mux.HandleFunc("/new-briefing/", server.DisplayInstructorForm()) + mux.HandleFunc("/add-participant/", server.AddParticipant(&logins)) + mux.HandleFunc("/submit-form/", server.SubmitBriefingForm(&briefings)) mux.HandleFunc("/internal-login/", server.DisplayTable(db)) - mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins, participantChan)) + mux.HandleFunc("/external-login/", server.DisplayParticipantForm(&logins)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/packages/server/server.go b/packages/server/server.go index ad186a7..cbcc33f 100644 --- a/packages/server/server.go +++ b/packages/server/server.go @@ -41,9 +41,9 @@ func DisplaySearchResults(db *data.DB) http.HandlerFunc { } } -func DisplayForm(i *int64) http.HandlerFunc { +func DisplayInstructorForm() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", i) + template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "content", nil) } } @@ -57,15 +57,13 @@ func generateUUID() (string, error) { return hex.EncodeToString(bs), nil } -func AddParticipant(i *int64, sl *[]string) http.HandlerFunc { +func AddParticipant(sl *[]string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - uuid, err := generateUUID() + login, err := generateUUID() if err != nil { http.Error(w, "AddParticipant: generateUUID(): "+fmt.Sprint(err), http.StatusInternalServerError) } - *i++ - login := fmt.Sprintf("%d", *i) + "-" + uuid (*sl) = append(*sl, login) template.Must(template.ParseFiles("templates/briefing.html")).ExecuteTemplate(w, "new", login) } @@ -73,7 +71,7 @@ func AddParticipant(i *int64, sl *[]string) http.HandlerFunc { // TODO: Hier weiter machen, irgendwie die b.ID herausgeben, // am besten hier auch die p.IDs rausgeben, damit diese später verknüpft werden können -func SubmitBriefingForm(sb *[]types.Briefing) http.HandlerFunc { +func SubmitBriefingForm(sb *[]*types.Briefing) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { now := time.Now() briefing := new(types.Briefing) @@ -88,7 +86,7 @@ func SubmitBriefingForm(sb *[]types.Briefing) http.HandlerFunc { briefing.DocumentName = r.PostFormValue("document") // TODO: in HTML einfügen briefing.AsOf = r.PostFormValue("state") // TODO: Umbenennen // briefing.InstructorID = r.PostFormValue("instructor-id") // TODO: aus Dropdown holen - (*sb) = append(*sb, *briefing) + (*sb) = append(*sb, briefing) } } @@ -130,66 +128,66 @@ func DisplayParticipantForm(sl *[]string) http.HandlerFunc { } } -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(): %v\n", err) - } - - p.Questions[i].Chosen = v - - return nil -} - -func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if i == 0 { - p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID)) - p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID)) - p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID)) - } else { - if err := readAnswer(r, p, i-1); err != nil { - http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) - } - } - - data := new(questionData) - data.ID = p.ID - data.Q = p.Questions[i] - data.I = i - data.J = i + 1 - - template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) - } -} - -func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - numQuestions := len(p.Questions) - wrongAnswers := make([]int, 0) - fmt.Println(wrongAnswers) - - if err := readAnswer(r, p, numQuestions-1); err != nil { - http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) - } - - for i, q := range p.Questions { - if q.Chosen != q.Correct { - wrongAnswers = append(wrongAnswers, i) - } - } - - if wrongAnswers == nil { - b.Participants = append(b.Participants, p) - } else { - data := new(questionData) - data.ID = p.ID - data.Q = p.Questions[0] - data.I = 0 - data.J = data.I + 1 - template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) - } - - template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil) - } -} +// 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(): %v\n", err) +// } +// +// p.Questions[i].Chosen = v +// +// return nil +// } +// +// func DisplayQuestion(i int, p *types.Participant) http.HandlerFunc { +// return func(w http.ResponseWriter, r *http.Request) { +// if i == 0 { +// p.FirstName = r.PostFormValue("participant-first-" + fmt.Sprintf("%d", p.ID)) +// p.LastName = r.PostFormValue("participant-last-" + fmt.Sprintf("%d", p.ID)) +// p.Company = r.PostFormValue("participant-company-" + fmt.Sprintf("%d", p.ID)) +// } else { +// if err := readAnswer(r, p, i-1); err != nil { +// http.Error(w, "DisplayQuestion: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) +// } +// } +// +// data := new(questionData) +// data.ID = p.ID +// data.Q = p.Questions[i] +// data.I = i +// data.J = i + 1 +// +// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) +// } +// } +// +// func DisplayTestResults(b *types.Briefing, p *types.Participant) http.HandlerFunc { +// return func(w http.ResponseWriter, r *http.Request) { +// numQuestions := len(p.Questions) +// wrongAnswers := make([]int, 0) +// fmt.Println(wrongAnswers) +// +// if err := readAnswer(r, p, numQuestions-1); err != nil { +// http.Error(w, "DisplayTestResults: readAnswer(r, p, i): "+fmt.Sprint(err), http.StatusInternalServerError) +// } +// +// for i, q := range p.Questions { +// if q.Chosen != q.Correct { +// wrongAnswers = append(wrongAnswers, i) +// } +// } +// +// if wrongAnswers == nil { +// b.Participants = append(b.Participants, p) +// } else { +// data := new(questionData) +// data.ID = p.ID +// data.Q = p.Questions[0] +// data.I = 0 +// data.J = data.I + 1 +// template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", data) +// } +// +// template.Must(template.ParseFiles("templates/results.html")).ExecuteTemplate(w, "content", nil) +// } +// }