diff --git a/web/06/labor/06_loesungen/buecher/book.go b/web/06/labor/06_loesungen/buecher/book.go new file mode 100644 index 0000000..f72036b --- /dev/null +++ b/web/06/labor/06_loesungen/buecher/book.go @@ -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 +} diff --git a/web/06/labor/06_loesungen/buecher/books.json b/web/06/labor/06_loesungen/buecher/books.json new file mode 100644 index 0000000..882dda5 --- /dev/null +++ b/web/06/labor/06_loesungen/buecher/books.json @@ -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 + } + ] +} \ No newline at end of file diff --git a/web/06/labor/06_loesungen/buecher/main.go b/web/06/labor/06_loesungen/buecher/main.go new file mode 100644 index 0000000..b4ad205 --- /dev/null +++ b/web/06/labor/06_loesungen/buecher/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println("Usage: add <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.") + } +} diff --git a/web/06/labor/06_loesungen/buecher/workshop-api.json b/web/06/labor/06_loesungen/buecher/workshop-api.json new file mode 100644 index 0000000..f616880 --- /dev/null +++ b/web/06/labor/06_loesungen/buecher/workshop-api.json @@ -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" + } + } + } + } + } +} \ No newline at end of file