summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
diff options
context:
space:
mode:
authorGrailFinder <wohilas@gmail.com>2024-03-23 07:26:24 +0300
committerGrailFinder <wohilas@gmail.com>2024-03-23 07:26:24 +0300
commit87d71e65ed131b3e15510af968dd5ba0d30d851d (patch)
tree78249d3ece11bb6c3fc96fd1c23273ae87e02523 /internal/handlers/main.go
parent5f2eb541052f6f6f02d5bc4320f9c19e21c6f59f (diff)
Feat: parse input; show actions
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r--internal/handlers/main.go43
1 files changed, 41 insertions, 2 deletions
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)
}