frontend: add tableview to display added monsters

This commit is contained in:
Patryk Hegenberg 2023-12-05 12:47:57 +01:00
parent e556883972
commit b8b51f1870
10 changed files with 132 additions and 20 deletions

View file

@ -73,7 +73,11 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
},
},
}
mu.Lock()
defer mu.Unlock()
*Monsters = append(*Monsters, monster)
log.Printf("Monster hinzugefügt. Anzahl der Monster jetzt: %d\n", len(*Monsters))
http.Redirect(w, r, "/monsterTable", http.StatusFound)
}
}

View file

@ -1,27 +1,32 @@
package handlers
import (
"ddServer/model"
"embed"
"html/template"
"log"
"net/http"
)
func FormHandler(content embed.FS, filename string) http.HandlerFunc {
func FormHandler(content embed.FS, monsters *[]model.Monster, filename string) http.HandlerFunc {
log.Print("FormHandler called")
mu.Lock()
defer mu.Unlock()
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterForm.html")
tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterForm.html", "templates/monster.html", "templates/monsterTable.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(w, "base", map[string]interface{}{
"Title": "Dungeons & Dragons Monster Generator",
"Title": "Dungeons & Dragons Monster Generator",
"Monsters": *monsters,
})
if err != nil {
log.Printf("Template execution error: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Printf("Template mit %d Monstern gerendert\n", len(*monsters))
}
}

View file

@ -0,0 +1,29 @@
package handlers
import (
"ddServer/model"
"embed"
"html/template"
"log"
"net/http"
)
func MonsterTableHandler(content embed.FS, monsters *[]model.Monster) http.HandlerFunc {
log.Print("AboutHandler called")
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(content, "templates/base.html", "templates/header.html", "templates/main.html", "templates/footer.html", "templates/monsterTable.html", "templates/monster.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(w, "monsterTable", map[string]interface{}{
"Title": "Dungeons & Dragons Monster Generator",
"Monsters": *monsters,
})
if err != nil {
log.Printf("Template execution error: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}

View file

@ -46,6 +46,7 @@ func SubmitHandler(content embed.FS, chars *[]model.Character, Monsters *[]model
return
}
log.Printf("Monster hinzugefügt. Anzahl der Monster jetzt: %d\n", len(*Monsters))
// JSON-Daten in die Datei schreiben
err = model.WriteToFile(filename, charJSON)
if err != nil {