diff options
-rw-r--r-- | components/index.html | 21 | ||||
-rw-r--r-- | internal/handlers/main.go | 43 | ||||
-rw-r--r-- | internal/models/models.go | 24 |
3 files changed, 72 insertions, 16 deletions
diff --git a/components/index.html b/components/index.html index 90e8050..7c4cb82 100644 --- a/components/index.html +++ b/components/index.html @@ -10,7 +10,22 @@ <div id="ancestor"> <hr /> <div> - HELLO {{.}} + HELLO {{.ID}} + <br /> + You have {{len .Actions}} actions. + <br /> + Your current score: {{.Score}} + <br /> + {{ range .Actions }} + Action Name: {{.Name}} + <br /> + Action Magnitude: {{.Magnitude}} + <br /> + Action Type: {{.Type}} + <br /> + Repeatable: {{.Repeatable}} + <br /> + {{end}} </div> <div> @@ -22,6 +37,10 @@ <input type="number" default="1" name="magnitude"><br /> <label>Repeatable:</label><br /> <input type="checkbox" name="repeatable"></input><br /> + <input type="radio" name="act_type" value="plus" checked> + <label for="plus">Plus</label> + <input type="radio" name="act_type" value="minus"> + <label for="minus">Minus</label><br> <input type="submit"> </form> </div> diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 74a66c7..1934250 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -2,10 +2,12 @@ package handlers import ( "apjournal/config" + "apjournal/internal/models" "html/template" "log/slog" "net/http" "os" + "strconv" ) // Handlers structure @@ -31,6 +33,9 @@ func NewHandlers( return h } +// FIXME: global userscore for test +var us models.UserScore + func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { h.log.Info("got ping request") w.Write([]byte("pong")) @@ -40,11 +45,45 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { h.log.Info("got mainpage request") tmpl := template.Must(template. ParseFiles("components/index.html")) - hardcodedName := "Guest" - tmpl.Execute(w, hardcodedName) + tmpl.Execute(w, us) } func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { r.ParseForm() h.log.Info("got postform request", "payload", r.PostForm) + magnitude := uint8(1) + mS := r.PostFormValue("magnitude") + if mS != "1" || mS != "" { + u64, err := strconv.ParseUint(mS, 10, 64) + magnitude = uint8(u64) + if err != nil { + // TODO: error handling + h.log.Warn("got an error", "error", err) + magnitude = uint8(1) + } + } + var at models.ActionType + switch r.PostFormValue("act_type") { + case "plus": + at = models.ActionTypePlus + case "minus": + at = models.ActionTypeMinus + default: + h.log.Warn("uknown actiontype", "type", r.PostFormValue("act_type")) + } + repeat := false + if r.PostFormValue("repeatable") == "on" { + repeat = true + } + // convert map to action object + act := models.Action{ + Name: r.PostFormValue("name"), + Magnitude: magnitude, + Type: at, + Repeatable: repeat, + } + us.Actions = append(us.Actions, act) + tmpl := template.Must(template. + ParseFiles("components/index.html")) + tmpl.Execute(w, us) } diff --git a/internal/models/models.go b/internal/models/models.go index eb90a0d..ee6ae5f 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -2,28 +2,26 @@ package models import "time" -type ( - ScoreChanger interface { - ChangeScore() error - } +type ActionType string + +const ( + ActionTypePlus ActionType = "ActionTypePlus" + ActionTypeMinus ActionType = "ActionTypeMinus" +) +type ( UserScore struct { ID string - Acts []ScoreChanger + Actions []Action BurnTime time.Time Score int8 } // plus 1 - PlusAction struct { + Action struct { Name string + Magnitude uint8 Repeatable bool - } - - // minus x - // always repeatable - MinusAction struct { - Name string - Magnitude uint8 + Type ActionType } ) |