Compare commits

..

2 Commits

Author SHA1 Message Date
Oliver Stolle 1c9ae7c8c8 Added answer for exercise 1
Co-authored-by: Copilot <copilot@github.com>
2026-04-27 22:06:27 +00:00
Oliver Stolle cd66b8022b Implemented the book exercise
Co-authored-by: Copilot <copilot@github.com>
2026-04-27 22:05:37 +00:00
3 changed files with 323 additions and 0 deletions

View File

@ -0,0 +1,131 @@
{
"openapi": "3.0.0",
"info": {
"title": "Workshop Anmeldungs API",
"version": "1.0.0"
},
"servers": [
{
"url": "https://web4-637691723779.europe-west1.run.app"
}
],
"paths": {
"/registrierung": {
"post": {
"summary": "Returns a response from the wokshop server",
"requestBody":{
"required": true,
"content": {
"application/x-www-form-urlencoded":{
"schema":{
"type": "object",
"properties": {
"Vorname":{
"type":"string",
"example":"Alice"
},
"Nachname":{
"type":"string",
"example":"Ecila"
},
"Email":{
"type":"string",
"example":"Alice@mail.de"
},
"Telefon":{
"type":"string",
"example":"0123/456789"
},
"Session":{
"type":"string",
"enum": ["vormittag","nachmittag","abendsession","wochenende"],
"example":"vormittag"
},
"Agb":{
"type":"boolean",
"example": true
},
"Newsletter":{
"type":"boolean",
"example": false
},
"Equipment":{
"type":"boolean",
"example": false
},
"Format":{
"type":"string",
"enum": ["praesenz","online"],
"example":"online"
}
},
"required": ["Vorname", "Nachname","Agb","Format"]
}
},
"application/json":{
"schema":{
"type": "object",
"properties": {
"Vorname":{
"type":"string",
"example":"Alice"
},
"Nachname":{
"type":"string",
"example":"Ecila"
},
"Email":{
"type":"string",
"example":"Alice@mail.de"
},
"Telefon":{
"type":"string",
"example":"0123/456789"
},
"Session":{
"type":"string",
"enum": ["vormittag","nachmittag","abendsession","wochenende"],
"example":"vormittag"
},
"Agb":{
"type":"boolean",
"example": true
},
"Newsletter":{
"type":"boolean",
"example": false
},
"Equipment":{
"type":"boolean",
"example": false
},
"Format":{
"type":"string",
"enum": ["praesenz","online"],
"example":"online"
}
},
"required": ["Vorname", "Nachname","Agb","Format"]
}
}
}
},
"responses": {
"200": {
"description": "Erfolgreiche Anmeldungs",
"content": {
"text/plain": {
"schema": {
"type": "string"
},
"example": "Anmeldung erfolgreich!"
}
}
}
}
}
}
}
}

View File

@ -0,0 +1,137 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
type Book struct {
Title string `json:"title"`
Author string `json:"author"`
Read bool `json:"read"`
ID int `json:"id"`
}
func addBook(title string, author string) error {
fmt.Println("add...")
books, err := readJson()
if err != nil {
return err
}
var newID int
if len(books) == 0 {
newID = 1
} else {
last := books[len(books)-1]
newID = last.ID + 1
}
newBook := Book{Title: title, Author: author, Read: false, ID: newID}
books = append(books, newBook)
erro := writeJson(books)
if erro != nil {
return erro
}
return nil
}
func listBooks() error {
fmt.Println("list...")
books, err := readJson()
if err != nil {
return err
}
fmt.Println("Hier ist Ihre aktuelle Buchübersicht:\n")
for _, book := range books {
fmt.Println("Titel:", book.Title)
fmt.Println("Author:", book.Author)
fmt.Println("Gelesen:", book.Read)
fmt.Println("ID:", book.ID)
fmt.Println("============================================")
}
return nil
}
func markBook(id int) error {
books, err := readJson()
if err != nil {
return err
}
for i := 0; i < len(books); i++ {
if books[i].ID == id {
books[i].Read = true
erro := writeJson(books)
if erro != nil {
return erro
}
return nil
}
}
return fmt.Errorf("ID %d nicht gefunden!", id)
}
func readJson() ([]Book, error) {
fmt.Println("read json...")
data, err := os.ReadFile("books.json")
if err != nil {
return nil, err
}
var books []Book
err = json.Unmarshal(data, &books)
if err != nil {
fmt.Println(err)
return nil, err
}
return books, nil
}
func writeJson(books []Book) error {
data, err := json.MarshalIndent(books, "", " ")
if err != nil {
return err
}
return os.WriteFile("books.json", data, 0644)
}
func ensureFileExists() error {
_, err := os.Stat("books.json")
if os.IsNotExist(err) {
// Initialer Inhalt: leeres JSON-Array
initialData := []byte("[]")
return os.WriteFile("books.json", initialData, 0644)
}
return err
}

View File

@ -0,0 +1,55 @@
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
if len(os.Args) == 1 {
fmt.Println("Kein Befehl angegeben!")
return
}
if err := ensureFileExists(); err != nil {
fmt.Println("Fehler beim prüfen der Datei:", err)
return
}
switch args := os.Args; args[1] {
case "add", "Add":
if len(args) < 4 {
fmt.Println("Nutzung: add <title> <author>")
return
}
if err := addBook(args[2], args[3]); err != nil {
fmt.Println("Fehler beim Hinzufügen:", err)
}
case "list", "List":
if err := listBooks(); err != nil {
fmt.Println("Fehler beim Auflisten:", err)
}
case "read", "Read":
if len(args) < 3 {
fmt.Println("Nutzung: read <id>")
return
}
convId, err := strconv.Atoi(args[2])
if err != nil || convId <= 0 {
fmt.Println("ID ist keine positive Ganzzahl!")
return
}
if err := markBook(convId); err != nil {
fmt.Println("Fehler beim Markieren:", err)
}
default:
fmt.Println("Unknown command!")
fmt.Println("Nutzung: add|list|read")
}
}