06: Lösungen

main
Teena Steger 2026-04-28 13:06:56 +02:00
parent 6f4035aef8
commit 5c202be52a
4 changed files with 233 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package main
import (
"encoding/json"
"os"
)
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Read bool `json:"read"`
}
type Library struct {
Books []Book `json:"books"`
}
func (lib *Library) Add(title, author string) {
id := len(lib.Books) + 1
lib.Books = append(lib.Books, Book{ID: id, Title: title, Author: author, Read: false})
}
func (lib *Library) MarkRead(id int) {
for i := range lib.Books {
if lib.Books[i].ID == id {
lib.Books[i].Read = true
}
}
}
func (lib *Library) Save(filename string) error {
data, err := json.MarshalIndent(lib, "", " ")
if err != nil {
return err
}
return os.WriteFile(filename, data, 0644)
}
func LoadLibrary(filename string) (*Library, error) {
data, err := os.ReadFile(filename)
if err != nil {
return &Library{}, nil
}
var lib Library
err = json.Unmarshal(data, &lib)
return &lib, err
}

View File

@ -0,0 +1,16 @@
{
"books": [
{
"id": 1,
"title": "Der Steppenwolf",
"author": "Hermann Hesse",
"read": false
},
{
"id": 2,
"title": "Clean Code",
"author": "Robert C. Martin",
"read": true
}
]
}

View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: add <title> <author> | list | read <id>")
return
}
lib, _ := LoadLibrary("books.json")
switch os.Args[1] {
case "add":
if len(os.Args) < 4 {
fmt.Println("Usage: add <title> <author>")
return
}
lib.Add(os.Args[2], os.Args[3])
lib.Save("books.json")
fmt.Println("Buch hinzugefügt.")
case "list":
for _, b := range lib.Books {
status := " "
if b.Read {
status = "✓"
}
fmt.Printf("[%s] %d: \"%s\" by %s\n", status, b.ID, b.Title, b.Author)
}
case "read":
if len(os.Args) < 3 {
fmt.Println("Geben Sie eine Buch-ID ein.")
return
}
id, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Ungültige ID-Eingabe.")
return
}
lib.MarkRead(id)
lib.Save("books.json")
fmt.Println("Buch als gelesen markiert.")
}
}

View File

@ -0,0 +1,119 @@
{
"openapi": "3.0.0",
"info": {
"title": "Workshop API",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:8081"
}
],
"paths": {
"/registrierung": {
"post": {
"summary": "Sends all information needed for registering at a workshop",
"requestBody": {
"required": false,
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"$ref": "#/components/schemas/RegistrierungForm"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/RegistrierungForm"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"text/html": {
"schema": {
"type": "string"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"text/plain": {
"schema": {
"type": "string",
"example": "Kein Formular gesendet"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"RegistrierungForm": {
"type": "object",
"required": [
"vorname",
"nachname",
"agb",
"format"
],
"properties": {
"vorname": {
"type": "string",
"example": "Zoya"
},
"nachname": {
"type": "string",
"example": "Akhtar"
},
"email": {
"type": "string",
"example": "z.akhtar@test.de"
},
"telefon": {
"type": "string",
"example": "Zoya"
},
"sessions": {
"type": "array",
"items": {
"type": "string",
"example": "vormittag"
},
"example": [
"vormittag",
"nachmittag"
]
},
"agb": {
"type": "string",
"enum": ["ja"],
"example": "ja"
},
"newsletter": {
"type": "string",
"enum": ["ja"],
"example": "ja"
},
"equipment": {
"type": "string",
"enum": ["ja"],
"example": "ja"
},
"format": {
"type": "string",
"enum": ["online","praesenz"],
"example": "online"
}
}
}
}
}
}