diff --git a/db.go b/db/db.go similarity index 97% rename from db.go rename to db/db.go index 3f0d155..0566930 100644 --- a/db.go +++ b/db/db.go @@ -1,4 +1,4 @@ -package sicherheitsunterweisung +package db import ( "bufio" diff --git a/main.go b/main.go index f0006f6..1f7cc69 100644 --- a/main.go +++ b/main.go @@ -1,73 +1,28 @@ -package sicherheitsunterweisung +package main import ( - "fmt" - "html/template" "log" "net/http" + "streifling.com/jason/sicherheitsunterweisung/db" + "streifling.com/jason/sicherheitsunterweisung/server" ) -type Person struct { - FirstName string - LastName string -} - -type Instructor Person - -type Participant struct { - ID int64 - Person - Company string -} - -type Briefing struct { - Instructor - Date string - Time string - State string - Location string - Participants []Participant -} - func main() { var i, j int64 - var b Briefing + var b server.Briefing i, j = 1, 1 mux := http.NewServeMux() - db, err := 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("/", func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("templates/index.html")).Execute(w, i) - }) - mux.HandleFunc("/add-participant/", func(w http.ResponseWriter, r *http.Request) { - i++ - template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) - }) - mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) { - b.Instructor.FirstName = r.PostFormValue("instructor-first") - b.Instructor.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++ { - b.Participants = append(b.Participants, Participant{ - ID: j, - Person: Person{ - FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), - LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), - }, - Company: r.PostFormValue(("participant-company-" + fmt.Sprint(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 new file mode 100644 index 0000000..cda63fa --- /dev/null +++ b/server/server.go @@ -0,0 +1,64 @@ +package server + +import ( + "fmt" + "html/template" + "net/http" +) + +type Person struct { + FirstName string + LastName string +} + +type Instructor Person + +type Participant struct { + ID int64 + Person + Company string +} + +type Briefing struct { + Instructor + Date string + Time string + State string + Location string + Participants []Participant +} + +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 { + return func(w http.ResponseWriter, r *http.Request) { + i++ + template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i) + } +} + +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.Date = r.PostFormValue("date") + b.Time = r.PostFormValue("time") + b.State = r.PostFormValue("state") + b.Location = r.PostFormValue("location") + + for ; j <= i; j++ { + b.Participants = append(b.Participants, Participant{ + ID: j, + Person: Person{ + FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)), + LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))), + }, + Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))), + }) + } + } +}