diff --git a/handlers/about_handler.go b/handlers/about_handler.go index fa7270c..5f8a1dd 100644 --- a/handlers/about_handler.go +++ b/handlers/about_handler.go @@ -20,7 +20,7 @@ func AboutHandler(content embed.FS) http.HandlerFunc { tmpl, err := template.ParseFS(content, tmplFiles...) if err != nil { log.Printf("Template parsing error: %v\n", err) - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -31,7 +31,7 @@ func AboutHandler(content embed.FS) http.HandlerFunc { err = tmpl.ExecuteTemplate(w, "about", data) if err != nil { log.Printf("Template execution error: %v\n", err) - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusBadRequest) } } } diff --git a/handlers/add_monster_handler.go b/handlers/add_monster_handler.go index ed44271..a2306aa 100644 --- a/handlers/add_monster_handler.go +++ b/handlers/add_monster_handler.go @@ -24,84 +24,12 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc { err := r.ParseForm() if err != nil { log.Printf("Error parsing form data: %s", err.Error()) - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusNoContent) return } // Create a new monster with the form data - monster := model.Monster{ - Name: r.FormValue("name"), - Source: r.FormValue("source"), - Size: []string{r.FormValue("size")}, - Type: r.FormValue("type"), - Alignment: []string{r.FormValue("alignment")}, - AC: []model.AC{ - { - AC: parseInt(r.FormValue("ac")), - From: []string{r.FormValue("acFrom")}, - }, - }, - HP: model.HP{ - Average: parseInt(r.FormValue("hpAverage")), - Formula: r.FormValue("hpFormula"), - }, - Speed: model.Speed{ - Walk: parseInt(r.FormValue("speed")), - }, - Str: parseInt(r.FormValue("str")), - Dex: parseInt(r.FormValue("dex")), - Con: parseInt(r.FormValue("con")), - Int: parseInt(r.FormValue("int")), - Wis: parseInt(r.FormValue("wis")), - Cha: parseInt(r.FormValue("cha")), - Save: model.Save{ - Dex: r.FormValue("saveDex"), - Con: r.FormValue("saveCon"), - Wis: r.FormValue("saveWis"), - Str: r.FormValue("saveStr"), - Cha: r.FormValue("saveCha"), - Int: r.FormValue("saveInt"), - }, - Skill: model.Skill{ - Perception: r.FormValue("perception"), - Stealth: r.FormValue("stealth"), - Acrobatics: r.FormValue("acrobatics"), - AnimalHandling: r.FormValue("animalHandling"), - Arcana: r.FormValue("arcana"), - Athletics: r.FormValue("athletics"), - Deception: r.FormValue("deception"), - History: r.FormValue("history"), - Insight: r.FormValue("insight"), - Intimidation: r.FormValue("intimidation"), - Investigation: r.FormValue("investigation"), - Medicine: r.FormValue("medicine"), - Nature: r.FormValue("nature"), - Performance: r.FormValue("performance"), - Persuasion: r.FormValue("persuasion"), - SleightOfHand: r.FormValue("sleightOfHand"), - Survival: r.FormValue("survival"), - Religion: r.FormValue("religion"), - }, - Resist: []string{r.FormValue("resist")}, - ConditionImmune: []string{r.FormValue("conditionImmune")}, - Immune: []string{r.FormValue("immune")}, - Vulnerable: []string{r.FormValue("vulnerable")}, - Senses: []string{r.FormValue("senses")}, - Languages: []string{r.FormValue("languages")}, - CR: r.FormValue("cr"), - Traits: []model.Trait{ - { - Name: r.FormValue("traitName"), - Entries: []string{r.FormValue("traitEntry")}, - }, - }, - Actions: []model.Action{ - { - Name: r.FormValue("actionName"), - Entries: []string{r.FormValue("actionEntry")}, - }, - }, - } + monster := parseMonster(r) // Lock the Monsters slice, append the monster, and unlock the slice mu.Lock() @@ -132,3 +60,84 @@ func parseInt(s string) int { // Return the converted integer return i } + +// parseMonster parses the Monster from monsterForm.html and return it. +func parseMonster(r *http.Request) model.Monster { + return model.Monster{ + Name: r.FormValue("name"), + Source: r.FormValue("source"), + Size: []string{r.FormValue("size")}, + Type: r.FormValue("type"), + Alignment: []string{r.FormValue("alignment")}, + AC: []model.AC{ + { + AC: parseInt(r.FormValue("ac")), + From: []string{r.FormValue("acFrom")}, + }, + }, + HP: model.HP{ + Average: parseInt(r.FormValue("hpAverage")), + Formula: r.FormValue("hpFormula"), + }, + Speed: model.Speed{ + Walk: parseInt(r.FormValue("walk")), + Burrow: parseInt(r.FormValue("burrow")), + Fly: parseInt(r.FormValue("fly")), + Swim: parseInt(r.FormValue("swim")), + Climb: parseInt(r.FormValue("climb")), + }, + Str: parseInt(r.FormValue("str")), + Dex: parseInt(r.FormValue("dex")), + Con: parseInt(r.FormValue("con")), + Int: parseInt(r.FormValue("int")), + Wis: parseInt(r.FormValue("wis")), + Cha: parseInt(r.FormValue("cha")), + Save: model.Save{ + Dex: r.FormValue("saveDex"), + Con: r.FormValue("saveCon"), + Wis: r.FormValue("saveWis"), + Str: r.FormValue("saveStr"), + Cha: r.FormValue("saveCha"), + Int: r.FormValue("saveInt"), + }, + Skill: model.Skill{ + Perception: r.FormValue("perception"), + Stealth: r.FormValue("stealth"), + Acrobatics: r.FormValue("acrobatics"), + AnimalHandling: r.FormValue("animalHandling"), + Arcana: r.FormValue("arcana"), + Athletics: r.FormValue("athletics"), + Deception: r.FormValue("deception"), + History: r.FormValue("history"), + Insight: r.FormValue("insight"), + Intimidation: r.FormValue("intimidation"), + Investigation: r.FormValue("investigation"), + Medicine: r.FormValue("medicine"), + Nature: r.FormValue("nature"), + Performance: r.FormValue("performance"), + Persuasion: r.FormValue("persuasion"), + SleightOfHand: r.FormValue("sleightOfHand"), + Survival: r.FormValue("survival"), + Religion: r.FormValue("religion"), + }, + Resist: []string{r.FormValue("resist")}, + ConditionImmune: []string{r.FormValue("conditionImmune")}, + Immune: []string{r.FormValue("immune")}, + Vulnerable: []string{r.FormValue("vulnerable")}, + Senses: []string{r.FormValue("senses")}, + Languages: []string{r.FormValue("languages")}, + CR: r.FormValue("cr"), + Traits: []model.Trait{ + { + Name: r.FormValue("traitName"), + Entries: []string{r.FormValue("traitEntry")}, + }, + }, + Actions: []model.Action{ + { + Name: r.FormValue("actionName"), + Entries: []string{r.FormValue("actionEntry")}, + }, + }, + } +} diff --git a/model/model.go b/model/model.go index 01bd301..0c8ec00 100644 --- a/model/model.go +++ b/model/model.go @@ -48,10 +48,10 @@ type HP struct { type Speed struct { Walk int `json:"walk"` - burrow int `json:"burrow"` - climb int `json:"climb"` - fly int `json:"fly"` - swim int `json:"swim"` + Burrow int `json:"burrow"` + Climb int `json:"climb"` + Fly int `json:"fly"` + Swim int `json:"swim"` } type Save struct { diff --git a/templates/main.html b/templates/main.html index eb95065..30ffc66 100644 --- a/templates/main.html +++ b/templates/main.html @@ -1,83 +1,106 @@ {{ define "main" }}
Monster Form
-Existing Monsters
-| Name | -Source | -Size | -Type | -Alignment | -AC | -AC Form | -HP Average | -HP Formula | -Speed | -Str | -Dex | -Con | -Int | -Wis | -Cha | -Save Dex | -Save Con | -Savve Wis | -Perception | -Stealth | -Damage Resistance | -Senses | -Languages | -CR | -Trait Name | -Trait Entry | -Action Name | -Action Entry | -
|---|
Existing Monsters
+| Name | +Source | +Size | +Type | +Alignment | +AC | +AC Form | +HP Average | +HP Formula | +Walk | +Swim | +Burrow | +Climb | +Fly | +Str | +Dex | +Con | +Int | +Wis | +Cha | +Save Dex | +Save Con | +Save Wis | +Save Str | +Save Con | +Save Cha | +Perception | +Stealth | +Acrobatics | +AnimalHandling | +Arcana | +Athletics | +Deception | +History | +Insight | +Intimidation | +Investigation | +Medicine | +Nature | +Performance | +Persuasion | +SleightOfHand | +Survival | +Religion | +Damage Resistance | +Senses | +Languages | +CR | +Trait Name | +Trait Entry | +Action Name | +Action Entry | +
|---|