diff options
author | GrailFinder <wohilas@gmail.com> | 2024-04-14 09:11:15 +0300 |
---|---|---|
committer | GrailFinder <wohilas@gmail.com> | 2024-04-14 09:11:15 +0300 |
commit | f71a43e1a4cf2fba8a479842d0a61e24017cd87b (patch) | |
tree | bbf4fb67119e02f35c117be26f15a0d666b0401f /internal/handlers/main.go | |
parent | 9b160dcc0315aaf54a8b4588be9d2a0c98084cf0 (diff) |
Feat: db methods and their usage
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r-- | internal/handlers/main.go | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go index 1f2276c..cb872f0 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -39,8 +39,9 @@ func NewHandlers( // FIXME: global userscore for test var us = models.UserScore{ - Username: "test", - BurnTime: time.Now().Add(time.Duration(24) * time.Hour), + Username: "test", + BurnTime: time.Now().Add(time.Duration(24) * time.Hour), + CreatedAt: time.Now(), } func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { @@ -55,10 +56,23 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { 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", us) + tmpl.ExecuteTemplate(w, "main", userScore) } func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { @@ -96,16 +110,34 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { 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) } - // TODO: check that name + userid key is unique - us.Actions = append(us.Actions, &act) - // TODO: redirect to the main page instead http.Redirect(w, r, "/", 302) +} - // tmpl := template.Must(template.ParseGlob("components/*.html")) - // // tmpl := template.Must(template.ParseFiles("components/index.html")) - // // tmpl.Execute(w, us) - // tmpl.ExecuteTemplate(w, "main", us) +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) { @@ -113,16 +145,15 @@ func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { 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 - for _, act := range us.Actions { - if act.Name != actionName { - continue - } - us.UpdateScore(act) - h.log.Info("show action", "act", act, "us", us) - } tmpl := template.Must(template.ParseGlob("components/*.html")) - tmpl.ExecuteTemplate(w, "main", us) - // http.Redirect(w, r, "/", 302) + tmpl.ExecuteTemplate(w, "main", userScore) } |