From fcb509c9fe847d966361a1c19329a5a06e565f97 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Thu, 5 Oct 2023 18:27:21 +0200 Subject: [PATCH] Verwendung von Pointern, um Vermischung der Pakete zu vermeiden --- main.go | 12 ++++++------ server/server.go | 25 ++++++++++++++----------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 1f7cc69..08ef011 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "log" "net/http" - "streifling.com/jason/sicherheitsunterweisung/db" + // "streifling.com/jason/sicherheitsunterweisung/db" "streifling.com/jason/sicherheitsunterweisung/server" ) @@ -14,15 +14,15 @@ func main() { i, j = 1, 1 mux := http.NewServeMux() - _, err := db.OpenDB("sicherheitsunterweisung") + /* _, err := db.OpenDB("sicherheitsunterweisung") if err != nil { log.Fatalln(err) - } + } */ mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) - mux.HandleFunc("/", server.DisplayForm(i)) - mux.HandleFunc("/add-participant/", server.AddParticipant(i)) - mux.HandleFunc("/submit/", server.SubmitForm(b, i, j)) + mux.HandleFunc("/", server.DisplayForm(&i)) + mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) + mux.HandleFunc("/submit/", server.SubmitForm(&b, &i, &j)) log.Fatalln(http.ListenAndServe(":8080", mux)) } diff --git a/server/server.go b/server/server.go index cda63fa..119ac72 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package server import ( "fmt" "html/template" + "log" "net/http" ) @@ -28,37 +29,39 @@ type Briefing struct { Participants []Participant } -func DisplayForm(i int64) http.HandlerFunc { +func DisplayForm(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) } } -func AddParticipant(i int64) http.HandlerFunc { +func AddParticipant(i *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - i++ + *i++ template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) } } -func SubmitForm(b Briefing, i, j int64) http.HandlerFunc { +func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - b.Instructor.FirstName = r.PostFormValue("instructor-first") - b.Instructor.LastName = r.PostFormValue("instructor-last") + b.FirstName = r.PostFormValue("instructor-first") + b.LastName = r.PostFormValue("instructor-last") b.Date = r.PostFormValue("date") b.Time = r.PostFormValue("time") b.State = r.PostFormValue("state") b.Location = r.PostFormValue("location") - for ; j <= i; j++ { + for ; *j <= *i; *j++ { b.Participants = append(b.Participants, Participant{ - ID: j, + ID: *j, Person: Person{ - FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), - LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), + FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)), + LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(*j))), }, - Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), + Company: r.PostFormValue(("participant-company-" + fmt.Sprint(*j))), }) } + + log.Println("Received ", *b) } }