backend: frontend: fix errors in model struct and add new fields to result table view

This commit is contained in:
Patryk Hegenberg 2023-12-07 15:55:47 +01:00
parent 0fd2775aa3
commit 1a4ac687f4
6 changed files with 189 additions and 153 deletions

View file

@ -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)
}
}
}

View file

@ -24,12 +24,46 @@ 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{
monster := parseMonster(r)
// Lock the Monsters slice, append the monster, and unlock the slice
mu.Lock()
defer mu.Unlock()
*Monsters = append(*Monsters, monster)
// Log the number of monsters and redirect to the monster table
log.Printf("Monster added. Number of monsters now: %d\n", len(*Monsters))
http.Redirect(w, r, "/monsterTable", http.StatusFound)
}
}
// parseInt converts a string to an integer and returns 0 if the conversion fails
func parseInt(s string) int {
// Add logging statement to print the input string
log.Println("Input string:", s)
// Atoi is used to convert the string to an integer
i, err := strconv.Atoi(s)
// If there is an error in the conversion, return 0 and log the error
if err != nil {
log.Println("Conversion error:", err)
return 0
}
// Log the converted integer
log.Println("Converted integer:", i)
// 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")},
@ -46,7 +80,11 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
Formula: r.FormValue("hpFormula"),
},
Speed: model.Speed{
Walk: parseInt(r.FormValue("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")),
@ -102,33 +140,4 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
},
},
}
// Lock the Monsters slice, append the monster, and unlock the slice
mu.Lock()
defer mu.Unlock()
*Monsters = append(*Monsters, monster)
// Log the number of monsters and redirect to the monster table
log.Printf("Monster added. Number of monsters now: %d\n", len(*Monsters))
http.Redirect(w, r, "/monsterTable", http.StatusFound)
}
}
// parseInt converts a string to an integer and returns 0 if the conversion fails
func parseInt(s string) int {
// Add logging statement to print the input string
log.Println("Input string:", s)
// Atoi is used to convert the string to an integer
i, err := strconv.Atoi(s)
// If there is an error in the conversion, return 0 and log the error
if err != nil {
log.Println("Conversion error:", err)
return 0
}
// Log the converted integer
log.Println("Converted integer:", i)
// Return the converted integer
return i
}

View file

@ -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 {

View file

@ -51,7 +51,11 @@
<th>AC Form</th>
<th>HP Average</th>
<th>HP Formula</th>
<th>Speed</th>
<th>Walk</th>
<th>Swim</th>
<th>Burrow</th>
<th>Climb</th>
<th>Fly</th>
<th>Str</th>
<th>Dex</th>
<th>Con</th>
@ -60,9 +64,28 @@
<th>Cha</th>
<th>Save Dex</th>
<th>Save Con</th>
<th>Savve Wis</th>
<th>Save Wis</th>
<th>Save Str</th>
<th>Save Con</th>
<th>Save Cha</th>
<th>Perception</th>
<th>Stealth</th>
<th>Acrobatics</th>
<th>AnimalHandling</th>
<th>Arcana</th>
<th>Athletics</th>
<th>Deception</th>
<th>History</th>
<th>Insight</th>
<th>Intimidation</th>
<th>Investigation</th>
<th>Medicine</th>
<th>Nature</th>
<th>Performance</th>
<th>Persuasion</th>
<th>SleightOfHand</th>
<th>Survival</th>
<th>Religion</th>
<th>Damage Resistance</th>
<th>Senses</th>
<th>Languages</th>

View file

@ -10,6 +10,10 @@
<td>{{.HP.Average}}</td>
<td>{{.HP.Formula}}</td>
<td>{{.Speed.Walk}}</td>
<td>{{.Speed.Swim}}</td>
<td>{{.Speed.Burrow}}</td>
<td>{{.Speed.Climb}}</td>
<td>{{.Speed.Fly}}</td>
<td>{{.Str}}</td>
<td>{{.Dex}}</td>
<td>{{.Con}}</td>

View file

@ -22,7 +22,7 @@
<label for="size">Size:</label>
<div class="control">
<div class="select">
<select>
<select name="size">
<option>H</option>
<option>T</option>
<option>S</option>