package handlers import ( "apjournal/config" "apjournal/internal/models" "html/template" "log/slog" "net/http" "os" "strconv" ) // Handlers structure type Handlers struct { cfg config.Config // s *service.Service log *slog.Logger } // NewHandlers constructor func NewHandlers( // cfg config.Config, s *service.Service, l *slog.Logger, cfg config.Config, l *slog.Logger, ) *Handlers { if l == nil { l = slog.New(slog.NewJSONHandler(os.Stdout, nil)) } h := &Handlers{ cfg: cfg, // s: s, log: l, } 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")) } func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { h.log.Info("got mainpage request") tmpl := template.Must(template. ParseFiles("components/index.html")) 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) }