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...) tmpl, err := template.ParseFS(content, tmplFiles...)
if err != nil { if err != nil {
log.Printf("Template parsing error: %v\n", err) log.Printf("Template parsing error: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
@ -31,7 +31,7 @@ func AboutHandler(content embed.FS) http.HandlerFunc {
err = tmpl.ExecuteTemplate(w, "about", data) err = tmpl.ExecuteTemplate(w, "about", data)
if err != nil { if err != nil {
log.Printf("Template execution error: %v\n", err) 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,84 +24,12 @@ func AddMonster(Monsters *[]model.Monster) http.HandlerFunc {
err := r.ParseForm() err := r.ParseForm()
if err != nil { if err != nil {
log.Printf("Error parsing form data: %s", err.Error()) log.Printf("Error parsing form data: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusNoContent)
return return
} }
// Create a new monster with the form data // Create a new monster with the form data
monster := model.Monster{ monster := parseMonster(r)
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")},
},
},
}
// Lock the Monsters slice, append the monster, and unlock the slice // Lock the Monsters slice, append the monster, and unlock the slice
mu.Lock() mu.Lock()
@ -132,3 +60,84 @@ func parseInt(s string) int {
// Return the converted integer // Return the converted integer
return i 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")},
},
},
}
}

View file

@ -48,10 +48,10 @@ type HP struct {
type Speed struct { type Speed struct {
Walk int `json:"walk"` Walk int `json:"walk"`
burrow int `json:"burrow"` Burrow int `json:"burrow"`
climb int `json:"climb"` Climb int `json:"climb"`
fly int `json:"fly"` Fly int `json:"fly"`
swim int `json:"swim"` Swim int `json:"swim"`
} }
type Save struct { type Save struct {

View file

@ -1,83 +1,106 @@
{{ define "main" }} {{ define "main" }}
<div class="tile is-parent"> <div class="tile is-parent">
<div class="tile is-child card "> <div class="tile is-child card ">
<div class="card-content"> <div class="card-content">
<div class="media-content"> <div class="media-content">
<p class="title is-4">Monster Form</p> <p class="title is-4">Monster Form</p>
</div>
<div class="content">
<form action="/submit" method="post" class="">
<div class="field">
<td><label for="filename">Filename:</label></td>
<div class="control"><input type="text" name="filename" required placeholder="Dateiname"
class="input input-bordered w-full max-w-xs">
</div> </div>
</div> <div class="content">
<div class="field"> <form action="/submit" method="post" class="">
<div class="control"> <div class="field">
<button type="button" hx-post="/addMonster" hx-trigger="click" hx-target="#monster-table" <td><label for="filename">Filename:</label></td>
hx-swap="outerHTML" class="button is-info" hx-boost="true">Add <div class="control"><input type="text" name="filename" required placeholder="Dateiname"
Monster</button> class="input input-bordered w-full max-w-xs">
</div>
</div>
<div class="field">
<div class="control">
<button type="button" hx-post="/addMonster" hx-trigger="click" hx-target="#monster-table"
hx-swap="outerHTML" class="button is-info" hx-boost="true">Add
Monster</button>
</div>
</div>
{{ template "monsterform" . }}
<input type="hidden" name="filename" value="{{.Filename}}">
<input type="submit" value="Submit" class="button is-primary">
</form>
</div> </div>
</div> </div>
{{ template "monsterform" . }}
<input type="hidden" name="filename" value="{{.Filename}}">
<input type="submit" value="Submit" class="button is-primary">
</form>
</div>
</div> </div>
</div>
</div> </div>
<div class="tile is-parent"> <div class="tile is-parent">
<div class="tile is-child card"> <div class="tile is-child card">
<div class="card-content"> <div class="card-content">
<div class="media-content"> <div class="media-content">
<p class="title is-4 is-centered">Existing Monsters</p> <p class="title is-4 is-centered">Existing Monsters</p>
</div> </div>
<div class="table-container"> <div class="table-container">
<table class="table"> <table class="table">
<colgroup> <colgroup>
<col style="width: 100px;"> <col style="width: 100px;">
<col style="width: 100px;"> <col style="width: 100px;">
</colgroup> </colgroup>
<thead> <thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Source</th> <th>Source</th>
<th>Size</th> <th>Size</th>
<th>Type</th> <th>Type</th>
<th>Alignment</th> <th>Alignment</th>
<th>AC</th> <th>AC</th>
<th>AC Form</th> <th>AC Form</th>
<th>HP Average</th> <th>HP Average</th>
<th>HP Formula</th> <th>HP Formula</th>
<th>Speed</th> <th>Walk</th>
<th>Str</th> <th>Swim</th>
<th>Dex</th> <th>Burrow</th>
<th>Con</th> <th>Climb</th>
<th>Int</th> <th>Fly</th>
<th>Wis</th> <th>Str</th>
<th>Cha</th> <th>Dex</th>
<th>Save Dex</th> <th>Con</th>
<th>Save Con</th> <th>Int</th>
<th>Savve Wis</th> <th>Wis</th>
<th>Perception</th> <th>Cha</th>
<th>Stealth</th> <th>Save Dex</th>
<th>Damage Resistance</th> <th>Save Con</th>
<th>Senses</th> <th>Save Wis</th>
<th>Languages</th> <th>Save Str</th>
<th>CR</th> <th>Save Con</th>
<th>Trait Name</th> <th>Save Cha</th>
<th>Trait Entry</th> <th>Perception</th>
<th>Action Name</th> <th>Stealth</th>
<th>Action Entry</th> <th>Acrobatics</th>
</tr> <th>AnimalHandling</th>
</thead> <th>Arcana</th>
{{ template "monsterTable" }} <th>Athletics</th>
</table> <th>Deception</th>
</div> <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>
<th>CR</th>
<th>Trait Name</th>
<th>Trait Entry</th>
<th>Action Name</th>
<th>Action Entry</th>
</tr>
</thead>
{{ template "monsterTable" }}
</table>
</div>
</div>
</div> </div>
</div>
</div> </div>
</div> </div>
{{ end }} {{ end }}

View file

@ -10,6 +10,10 @@
<td>{{.HP.Average}}</td> <td>{{.HP.Average}}</td>
<td>{{.HP.Formula}}</td> <td>{{.HP.Formula}}</td>
<td>{{.Speed.Walk}}</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>{{.Str}}</td>
<td>{{.Dex}}</td> <td>{{.Dex}}</td>
<td>{{.Con}}</td> <td>{{.Con}}</td>

View file

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