forked from WEB-IMB-WS2526/lab-development-imb
150 lines
4.2 KiB
Go
150 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type Registrierung struct {
|
|
Vorname string `json:"vorname"`
|
|
Nachname string `json:"nachname"`
|
|
Email string `json:"email"`
|
|
Telefon string `json:"telefon"`
|
|
Sessions []string `json:"sessions"`
|
|
AGBAkzeptiert string `json:"agb"`
|
|
Newsletter string `json:"newsletter"`
|
|
Equipment string `json:"equipment"`
|
|
Format string `json:"format"`
|
|
}
|
|
|
|
func parseCheckboxValue(value string) string {
|
|
if value == "" || (value != "ja" && value != "on") {
|
|
return "nein"
|
|
}
|
|
return "ja"
|
|
}
|
|
|
|
func parseRadiobuttonValue(value string) (string, error) {
|
|
if value == "" || (value != "praesenz" && value != "online") {
|
|
return "", errors.New("Auswahl nicht erlaubt.")
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func registrierungHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Nur POST-Anfragen erlaubt", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var data Registrierung
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
if contentType == "application/json" {
|
|
// JSON-Daten verarbeiten
|
|
decoder := json.NewDecoder(r.Body)
|
|
if err := decoder.Decode(&data); err != nil {
|
|
http.Error(w, "Ungültiges JSON", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if data.Vorname == "" {
|
|
http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if data.Nachname == "" {
|
|
http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
data.AGBAkzeptiert = parseCheckboxValue(data.AGBAkzeptiert)
|
|
if data.AGBAkzeptiert == "nein" {
|
|
http.Error(w, "AGB wurde nicht akzeptiert.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
data.Newsletter = parseCheckboxValue(data.Newsletter)
|
|
data.Equipment = parseCheckboxValue(data.Equipment)
|
|
format, err := parseRadiobuttonValue(data.Format)
|
|
if err != nil {
|
|
http.Error(w, "Ungültige Auswahl des Formats.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
data.Format = format
|
|
} else {
|
|
// Formulardaten parsen und Überprüfen, ob ungültige Formulardaten gesendet wurde
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "Fehler beim Parsen des Formulars", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Überprüfen, ob leeres Formular gesendet wurde
|
|
if len(r.PostForm) == 0 {
|
|
http.Error(w, "Kein Formular gesendet", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Formulardaten auslesen und ggf. überprüfen
|
|
vorname := r.PostForm.Get("vorname")
|
|
if vorname == "" {
|
|
http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
nachname := r.PostForm.Get("nachname")
|
|
if nachname == "" {
|
|
http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
email := r.PostForm.Get("email")
|
|
telefon := r.PostForm.Get("telefon")
|
|
sessions := r.PostForm["sessions"]
|
|
agb := parseCheckboxValue(r.PostForm.Get("agb"))
|
|
if agb == "" {
|
|
http.Error(w, "AGB wurde nicht akzeptiert.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
newsletter := parseCheckboxValue(r.PostForm.Get("newsletter"))
|
|
equipment := parseCheckboxValue(r.PostForm.Get("equipment"))
|
|
format, err := parseRadiobuttonValue(r.PostForm.Get("format"))
|
|
if err != nil {
|
|
http.Error(w, "Ungültige Auswahl des Formats.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
data = Registrierung{
|
|
Vorname: vorname,
|
|
Nachname: nachname,
|
|
Email: email,
|
|
Telefon: telefon,
|
|
Sessions: sessions,
|
|
AGBAkzeptiert: agb,
|
|
Newsletter: newsletter,
|
|
Equipment: equipment,
|
|
Format: format,
|
|
}
|
|
}
|
|
|
|
// Ausgabe im Terminal
|
|
fmt.Fprintf(w, "Neue Registrierung erhalten:\n")
|
|
fmt.Fprintf(w, "Vorname: %s\n", data.Vorname)
|
|
fmt.Fprintf(w, "Nachname: %s\n", data.Nachname)
|
|
fmt.Fprintf(w, "E-Mail: %s\n", data.Email)
|
|
fmt.Fprintf(w, "Telefon: %s\n", data.Telefon)
|
|
fmt.Fprintf(w, "Bevorzugte Sessions: \n")
|
|
for _, session := range data.Sessions {
|
|
fmt.Fprintf(w, " - %s\n", session)
|
|
}
|
|
fmt.Fprintf(w, "AGB akzeptiert: %s\n", data.AGBAkzeptiert)
|
|
fmt.Fprintf(w, "Newsletter: %s\n", data.Newsletter)
|
|
fmt.Fprintf(w, "Equipment benötigt: %s\n", data.Equipment)
|
|
fmt.Fprintf(w, "Teilnahmeformat: %s", data.Format)
|
|
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/registrierung", registrierungHandler)
|
|
fmt.Println("Server läuft auf localhost:8080...")
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|