74 lines
1.7 KiB
Go
Raw Normal View History

package sicherheitsunterweisung
2023-10-04 17:13:03 +02:00
2023-10-04 17:29:22 +02:00
import (
"fmt"
2023-10-04 17:29:22 +02:00
"html/template"
"log"
"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
}
2023-10-04 17:29:22 +02:00
func main() {
var i, j int64
var b Briefing
i, j = 1, 1
mux := http.NewServeMux()
db, err := OpenDB("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
2023-10-04 17:29:22 +02:00
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html")).Execute(w, i)
2023-10-04 17:29:22 +02:00
})
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))),
})
}
})
2023-10-04 17:29:22 +02:00
log.Fatalln(http.ListenAndServe(":8080", mux))
}