package handlers import ( "apjournal/config" "apjournal/internal/database/repos" "apjournal/internal/models" "html/template" "log/slog" "net/http" "os" "strconv" "time" "github.com/jmoiron/sqlx" ) // Handlers structure type Handlers struct { cfg config.Config log *slog.Logger repo *repos.Provider } // NewHandlers constructor func NewHandlers( // cfg config.Config, s *service.Service, l *slog.Logger, cfg config.Config, l *slog.Logger, conn *sqlx.DB, ) *Handlers { if l == nil { l = slog.New(slog.NewJSONHandler(os.Stdout, nil)) } h := &Handlers{ cfg: cfg, log: l, repo: repos.NewProvider(conn), } return h } // FIXME: global userscore for test var us = models.UserScore{ Username: "test", BurnTime: time.Now().Add(time.Duration(24) * time.Hour), CreatedAt: time.Now(), } 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, err := template.ParseGlob("components/*.html") if err != nil { panic(err) } userScore, err := h.repo.DBUserScoreGet("test") if err != nil { h.log.Warn("got db err", "err", err) if err := h.repo.DBUserScoreCreate(&us); err != nil { panic(err) } tmpl.ExecuteTemplate(w, "main", us) return } userScore.Actions, err = h.repo.DBActionList("test") if err != nil { panic(err) } // tmpl.Execute(w, us) // us.Username = "test" // us.BurnTime = time.Now().Add(time.Duration(24) * time.Hour) tmpl.ExecuteTemplate(w, "main", userScore) } 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") h.log.Info("showing magnitude send", "mS", mS) if mS != "1" { 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) } } repeat := false var at models.ActionType switch r.PostFormValue("act_type") { case "plus": at = models.ActionTypePlus case "minus": at = models.ActionTypeMinus repeat = true default: h.log.Warn("uknown actiontype", "type", r.PostFormValue("act_type")) } // 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, CreatedAt: time.Now(), } // TODO: get username from ctx userScore, err := h.repo.DBUserScoreGet("test") if err != nil { panic(err) } act.Username = userScore.Username if err := h.repo.DBActionCreate(&act); err != nil { panic(err) } http.Redirect(w, r, "/", 302) } func (h *Handlers) UserScoreWithActionsByUsername( username string, ) (*models.UserScore, error) { userScore, err := h.repo.DBUserScoreGet("test") if err != nil { return nil, err } list, err := h.repo.DBActionList("test") if err != nil { return nil, err } userScore.Actions = list return userScore, nil } func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { r.ParseForm() h.log.Info("got done request", "payload", r.PostForm) actionName := r.PostFormValue("name") h.log.Info("got postform request", "name", actionName) if err := h.repo.DBActionDone(actionName); err != nil { panic(err) } userScore, err := h.UserScoreWithActionsByUsername("test") if err != nil { panic(err) } // change counter of user score // get action by name tmpl := template.Must(template.ParseGlob("components/*.html")) tmpl.ExecuteTemplate(w, "main", userScore) }