Aufgabe 7 und änderungen an anderen Dateien hierfür

branch-francesca
Francesca Bläse 2025-11-24 08:45:21 +00:00
parent a0b6a56ef7
commit b48789e805
9 changed files with 376 additions and 7 deletions

View File

@ -7,17 +7,17 @@
<body>
<h1>Workshopanmeldung</h1>
<form action="https://web-637691723779.europe-west2.run.app/registrierung" method="POST">
<form action="http://localhost:8080/registrierung" method="POST">
<fieldset>
<legend>Persönliche Angaben:</legend>
<label for="vorname">Vorname:</label><br>
<input type="text" id="vorname" name="vorname" placeholder="Vorname"><br><br>
<input type="text" id="vorname" name="vorname" placeholder="Vorname" required><br><br>
<label for="nachname">Nachname:</label><br>
<input type="text" id="nachname" name="nachname" placeholder="Nachname"><br><br>
<input type="text" id="nachname" name="nachname" placeholder="Nachname" required><br><br>
<label for="email">E-Mail:</label><br>
<input type="email" id="email" name="email"><br><br>
<input type="email" id="email" name="email" required><br><br>
<label for="handy">Handynummer:</label><br>
<input type="tel" id="handy" name="handy"><br>
@ -46,10 +46,10 @@
<input type="checkbox" id="agb" name="agb" required>
<label for="agb">Ich akzeptiere die Teilnahmebedingungen.</label><br>
<input type="checkbox" id="newsletter" name="newsletter">
<input type="checkbox" id="newsletter" name="newsletter" value="ja">
<label for="newsletter">Newsletter abonnieren.</label><br>
<input type="checkbox" id="equipment" name="equipment">
<input type="checkbox" id="equipment" name="equipment" value="ja">
<label for="equipment">Ich benötige spezielles Equipment.</label>
</fieldset>

View File

@ -3,7 +3,7 @@ info:
title: Workshop-Anmeldung API
version: 1.0.0
servers:
- url: https://web-637691723779.europe-west2.run.app
- url: http://localhost:8080
paths:
/registrierung:
post:

View File

@ -0,0 +1,79 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type Book struct {
ID int `json:"id"`
Titel string `json:"titel"`
Autor string `json:"autor"`
Read bool `json:"read"`
}
type Library struct {
Books []Book `json:"books"`
}
// fügt ein Buch der Library hinzu
func (l *Library) Add(titel, autor string) {
id := len(l.Books) + 1
newBook := Book{ID: id, Titel: titel, Autor: autor, Read: false}
l.Books = append(l.Books, newBook)
}
// Markiert ein Buch als gelesen
func (l *Library) Read(id int) {
for i := range l.Books {
if l.Books[i].ID == id {
l.Books[i].Read = true
}
}
}
// Listet alle Bücher auf
func (l *Library) List() {
if len(l.Books) == 0 {
fmt.Println("Keine Bücher vorhanden.")
return
}
for _, b := range l.Books {
status := "ungelesen"
if b.Read {
status = "gelesen"
}
fmt.Printf("%d: %s (%s) %s\n", b.ID, b.Titel, b.Autor, status)
}
}
// Liste als JSON speichern
func (l *Library) Save(filename string) error {
data, err := json.MarshalIndent(l, "", " ")
if err != nil {
return err
}
err = os.WriteFile(filename, data, 0644)
if err != nil {
return err
}
return nil
}
// JSON aus Datei laden
func Load(filename string) (*Library, error) {
data, err := os.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return &Library{}, nil
}
return nil, err
}
var l Library
jsonErr := json.Unmarshal(data, &l)
return &l, jsonErr
}

View File

@ -0,0 +1,22 @@
{
"books": [
{
"id": 1,
"titel": "Testbuch",
"autor": "Testautor",
"read": false
},
{
"id": 2,
"titel": "Test2",
"autor": "222",
"read": false
},
{
"id": 3,
"titel": "Test3",
"autor": "EEEE",
"read": false
}
]
}

View File

@ -0,0 +1,3 @@
module kommandozeilenprogramm
go 1.24.5

View File

@ -0,0 +1,73 @@
package main
// fmt für ausgaben, os für Kommandozeilenargumente, strconv um strings in ints umzuwandeln
import (
"fmt"
"os"
"strconv"
)
// prüfen, ob mind. 1 Kommando übergeben wurde
func main() {
if len(os.Args) < 2 {
fmt.Println("Bitte einen Befehl eingeben")
return
}
filename := "books.json"
library, err := Load(filename)
if err != nil {
fmt.Println("Fehler beim Laden der JSON-Datai:", err.Error())
return
}
switch os.Args[1] {
case "add":
// Wenn Argumente kleiner 4, Hilfe ausgeben
if len(os.Args) < 4 {
fmt.Println("Benutze: add / Titel / Autor ")
return
}
// sonst titel = Argument 2 und autor = Argument 3
titel := os.Args[2]
autor := os.Args[3]
library.Add(titel, autor)
if err := library.Save(filename); err != nil {
fmt.Println("Fehler beim Speichern:", err.Error())
return
}
fmt.Println("Buch hinzugefügt.")
case "list":
library.List()
case "read":
if len(os.Args) < 3 {
fmt.Println("Benutze: read / ID")
return
}
id, convErr := strconv.Atoi(os.Args[2])
if convErr != nil {
fmt.Println("Die ID muss eine Zahl sein.")
return
}
library.Read(id)
if err := library.Save(filename); err != nil {
fmt.Println("Fehler beim Speichern:", err.Error())
return
}
fmt.Println("Buch als gelesen markiert.")
default:
fmt.Println("Kein bekanntes Kommando(add/list/red")
}
}

View File

@ -0,0 +1,82 @@
package main
import (
"fmt"
"net/http"
)
// Handler Funktion anlegen
// w = Antwort an Browser, r=Anfrage vom Client
func workshopHandler(w http.ResponseWriter, r *http.Request) {
// Prüfen auf POST, Fehler falls andere Methode
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// ließt den Body des Requests ein und befüllt FormFalue(), sofern keine Fehler auftauchen
err := r.ParseForm()
if err != nil {
http.Error(w, "Fehler beim Einlesen des Formulars", http.StatusBadRequest)
}
// Wenn Felder die als required markiert wurden leer sind: Fehlermeldung
vorname := r.FormValue("vorname")
if vorname == "" {
http.Error(w, "Vorname fehlt", http.StatusBadRequest)
return
}
nachname := r.FormValue("nachname")
if nachname == "" {
http.Error(w, "Nachname fehlt", http.StatusBadRequest)
return
}
email := r.FormValue("email")
if email == "" {
http.Error(w, "E-Mail fehlt", http.StatusBadRequest)
return
}
handy := r.FormValue("handy")
// Checkboxen auslesen
agb := r.FormValue("agb")
if agb == "" {
http.Error(w, "AGBs müssen akzeptiert werden", http.StatusBadRequest)
return
}
// newsletter und equipment beliben leer, wenn nicht angehakt
newsletter := r.FormValue("newsletter")
equipment := r.FormValue("equipment")
// Radio Buttons
format := r.FormValue("format")
if format == "" {
http.Error(w, "Wählen sie ein Kursformat", http.StatusBadRequest)
return
}
// Dropdown
session := r.FormValue("session")
if session == "" {
http.Error(w, "Wählen sie eine Zeit aus", http.StatusBadRequest)
return
}
fmt.Println("Vorname: ", vorname)
fmt.Println("Nachname: ", nachname)
fmt.Println("E-Mail: : ", email)
fmt.Println("Telefon: ", handy)
fmt.Fprintln(w, "POST Request funktioniert")
}
func main() {
// Anfrage an Pfad in HTML /registrierung, worshop Handler wird aufgerufen
http.HandleFunc("/registrierung", workshopHandler)
fmt.Println("Server läuft auf http://localhost:8080")
http.ListenAndServe(":8080", nil)
}

View File

@ -0,0 +1,110 @@
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("hello world")
fmt.Println("go" + "lang")
fmt.Println("1+1=", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
fmt.Println("True && False", true && false)
fmt.Println("True || False", true || false)
fmt.Println(!true)
/* hello world
golang
1+1= 2
7.0/3.0 = 2.3333333333333335
True && False false
True || False true
false */
var a = "initial"
var b, c int = 1, 2
var d = true
var e int
// shorthand für Deklaration + Initialisierung
f := "apple"
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
fmt.Println(f)
// initial , 1, 2, true, 0, apple
fmt.Println("---------------------------------")
constants()
fmt.Println("---------------------------------")
fors()
fmt.Println("---------------------------------")
}
const s string = "constant"
func constants() {
fmt.Println(s)
const n = 500000000
const d = 3e20 / n
fmt.Println(int64(d))
fmt.Println(math.Sin(n))
// constant,600000000000,-0.28470407323754404
}
func fors() {
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
//123
}
fmt.Println()
for j := 0; j < 3; j++ {
fmt.Println(j)
//0 1 2
}
fmt.Println()
for i := range 3 {
fmt.Println("range", i)
}
fmt.Println()
for {
fmt.Println("loop")
break
}
fmt.Println()
for n := range 6 {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}