frontend: backend: clean up code for better readability

This commit is contained in:
Patryk Hegenberg 2023-12-15 09:27:55 +01:00
parent f84ee2a466
commit 32a37eea32
4 changed files with 39 additions and 97 deletions

5
go.mod
View file

@ -2,7 +2,10 @@ module ddServer
go 1.21.4 go 1.21.4
require github.com/stretchr/testify v1.8.4 require (
github.com/stretchr/testify v1.8.4
golang.org/x/text v0.14.0
)
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect

2
go.sum
View file

@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View file

@ -2,10 +2,14 @@ package handlers
import ( import (
"ddServer/model" "ddServer/model"
"fmt"
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
) )
// AddMonster is a http.HandlerFunc that adds a new monster to the Monsters slice. // AddMonster is a http.HandlerFunc that adds a new monster to the Monsters slice.
@ -64,80 +68,6 @@ func parseInt(s string) int {
// parseMonster parses the Monster from monsterForm.html and return it. // parseMonster parses the Monster from monsterForm.html and return it.
func parseMonster(r *http.Request) model.Monster { func parseMonster(r *http.Request) model.Monster {
var (
acrobatics = ""
animalHandling = ""
arcana = ""
athletics = ""
deception = ""
history = ""
insight = ""
intimidation = ""
investigation = ""
medicine = ""
nature = ""
performance = ""
perception = ""
persuasion = ""
sleightOfHand = ""
religion = ""
stealth = ""
survival = ""
)
if r.FormValue("checkAcrobatics") == "on" {
acrobatics = r.FormValue("acrobatics")
}
if r.FormValue("checkAnimalHandling") == "on" {
animalHandling = r.FormValue("animalHandling")
}
if r.FormValue("checkArcana") == "on" {
arcana = r.FormValue("arcana")
}
if r.FormValue("checkAthletics") == "on" {
athletics = r.FormValue("athletics")
}
if r.FormValue("checkDeception") == "on" {
deception = r.FormValue("deception")
}
if r.FormValue("checkHistory") == "on" {
history = r.FormValue("history")
}
if r.FormValue("checkInsight") == "on" {
insight = r.FormValue("insight")
}
if r.FormValue("checkIntimidation") == "on" {
intimidation = r.FormValue("intimidation")
}
if r.FormValue("checkInvestigation") == "on" {
investigation = r.FormValue("investigation")
}
if r.FormValue("checkMedicine") == "on" {
medicine = r.FormValue("medicine")
}
if r.FormValue("checkNature") == "on" {
nature = r.FormValue("nature")
}
if r.FormValue("checkPerformance") == "on" {
performance = r.FormValue("performance")
}
if r.FormValue("checkPerception") == "on" {
perception = r.FormValue("perception")
}
if r.FormValue("checkPersuasion") == "on" {
persuasion = r.FormValue("persuasion")
}
if r.FormValue("checkSleightOfHand") == "on" {
sleightOfHand = r.FormValue("sleightOfHand")
}
if r.FormValue("checkStealth") == "on" {
stealth = r.FormValue("stealth")
}
if r.FormValue("checkSurvival") == "on" {
survival = r.FormValue("survival")
}
if r.FormValue("checkReligion") == "on" {
religion = r.FormValue("religion")
}
return model.Monster{ return model.Monster{
Name: r.FormValue("name"), Name: r.FormValue("name"),
Source: r.FormValue("source"), Source: r.FormValue("source"),
@ -176,24 +106,24 @@ func parseMonster(r *http.Request) model.Monster {
Int: r.FormValue("saveInt"), Int: r.FormValue("saveInt"),
}, },
Skill: model.Skill{ Skill: model.Skill{
Perception: perception, Perception: checkCheckbox("perception", r),
Stealth: stealth, Stealth: checkCheckbox("stealth", r),
Acrobatics: acrobatics, Acrobatics: checkCheckbox("acrobatics", r),
AnimalHandling: animalHandling, AnimalHandling: checkCheckbox("animalhandling", r),
Arcana: arcana, Arcana: checkCheckbox("arcana", r),
Athletics: athletics, Athletics: checkCheckbox("athletics", r),
Deception: deception, Deception: checkCheckbox("deception", r),
History: history, History: checkCheckbox("history", r),
Insight: insight, Insight: checkCheckbox("insight", r),
Intimidation: intimidation, Intimidation: checkCheckbox("intimidation", r),
Investigation: investigation, Investigation: checkCheckbox("investigation", r),
Medicine: medicine, Medicine: checkCheckbox("medicine", r),
Nature: nature, Nature: checkCheckbox("nature", r),
Performance: performance, Performance: checkCheckbox("performance", r),
Persuasion: persuasion, Persuasion: checkCheckbox("persuasion", r),
SleightOfHand: sleightOfHand, SleightOfHand: checkCheckbox("sleightofhand", r),
Survival: survival, Survival: checkCheckbox("survival", r),
Religion: religion, Religion: checkCheckbox("religion", r),
}, },
Resist: []string{r.FormValue("resist")}, Resist: []string{r.FormValue("resist")},
ConditionImmune: []string{r.FormValue("conditionImmune")}, ConditionImmune: []string{r.FormValue("conditionImmune")},
@ -216,3 +146,10 @@ func parseMonster(r *http.Request) model.Monster {
}, },
} }
} }
func checkCheckbox(field string, r *http.Request) string {
if r.FormValue(fmt.Sprintf("check%v", cases.Caser(cases.Title(language.Und)).String(field))) == "on" {
return r.FormValue(field)
}
return ""
}

View file

@ -85,9 +85,9 @@
</div> </div>
<div class="tile is-parent"> <div class="tile is-parent">
<div class="tile is-child field"> <div class="tile is-child field">
<label for="animalHandling" class="checkbox"><input type="checkbox" name="checkAnimalHandling">Animal Handling:</label> <label for="animalhandling" class="checkbox"><input type="checkbox" name="checkAnimalhandling">Animal Handling:</label>
<div class="control"> <div class="control">
<input type="text" name="animalHandling" required class="input input-bordered w-full max-w-xs" <input type="text" name="animalhandling" required class="input input-bordered w-full max-w-xs"
value="{{ .animalHandling }}" readonly> value="{{ .animalHandling }}" readonly>
</div> </div>
</div> </div>
@ -221,9 +221,9 @@
<div class="tile is-ancestor"> <div class="tile is-ancestor">
<div class="tile is-parent"> <div class="tile is-parent">
<div class="tile is-child field"> <div class="tile is-child field">
<label for="sleightOfHand" class="checkbox"><input type="checkbox" name="checkSleightOfHand">Sleight of Hand:</label> <label for="sleightofhand" class="checkbox"><input type="checkbox" name="checkSleightofhand">Sleight of Hand:</label>
<div class="control"> <div class="control">
<input type="text" name="sleightOfHand" required class="input input-bordered w-full max-w-xs" <input type="text" name="sleightofhand" required class="input input-bordered w-full max-w-xs"
value="{{.sleightOfHand }}" readonly> value="{{.sleightOfHand }}" readonly>
</div> </div>
</div> </div>