forked from WEB-IB-SS26/development-ib
07: Lösungen
parent
9f95ddee80
commit
a26dd68636
|
|
@ -0,0 +1,161 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"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"`
|
||||
}
|
||||
|
||||
type workshopHandler int
|
||||
|
||||
func parseCheckboxValue(value string) string {
|
||||
if value == "" || (value != "ja") {
|
||||
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 (worksh workshopHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
|
||||
if r.Method == "OPTIONS" {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
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 == "nein" {
|
||||
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() {
|
||||
var workshop workshopHandler
|
||||
fmt.Println("Server läuft auf localhost:8080...")
|
||||
log.Fatal(http.ListenAndServe(":8080", workshop))
|
||||
}
|
||||
Loading…
Reference in New Issue