summaryrefslogtreecommitdiff
path: root/internal/handlers/main.go
diff options
context:
space:
mode:
authorGrailFinder <wohilas@gmail.com>2024-05-18 13:27:28 +0300
committerGrailFinder <wohilas@gmail.com>2024-05-18 13:27:28 +0300
commit2e9b18944eac3dcaf8a006594cb338d94c07a447 (patch)
tree7715140a6951407d13781e0a5bbf83a6e8f9dbe9 /internal/handlers/main.go
parentff86222fc9ab85fb4c5c5e8a063083595b323761 (diff)
Feat: auth; login; signup; migrate to sqlite
Diffstat (limited to 'internal/handlers/main.go')
-rw-r--r--internal/handlers/main.go46
1 files changed, 25 insertions, 21 deletions
diff --git a/internal/handlers/main.go b/internal/handlers/main.go
index aa9db4f..e87c74f 100644
--- a/internal/handlers/main.go
+++ b/internal/handlers/main.go
@@ -39,13 +39,6 @@ func NewHandlers(
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"))
@@ -70,10 +63,7 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) {
userScore, err := h.repo.DBUserScoreGet(username)
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)
+ tmpl.ExecuteTemplate(w, "main", nil)
return
}
userScore.Actions, err = h.repo.DBActionList(username)
@@ -120,8 +110,10 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) {
Repeatable: repeat,
CreatedAt: time.Now(),
}
- // TODO: get username from ctx
- userScore, err := h.repo.DBUserScoreGet("test")
+ // get username from ctx
+ username := r.Context().Value("username").(string)
+ h.log.Info("got username from ctx", "username", username)
+ userScore, err := h.repo.DBUserScoreGet(username)
if err != nil {
panic(err)
}
@@ -135,11 +127,11 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) {
func (h *Handlers) UserScoreWithActionsByUsername(
username string,
) (*models.UserScore, error) {
- userScore, err := h.repo.DBUserScoreGet("test")
+ userScore, err := h.repo.DBUserScoreGet(username)
if err != nil {
return nil, err
}
- list, err := h.repo.DBActionList("test")
+ list, err := h.repo.DBActionList(username)
if err != nil {
return nil, err
}
@@ -149,18 +141,30 @@ func (h *Handlers) UserScoreWithActionsByUsername(
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)
+ username := r.Context().Value("username").(string)
+ h.log.Info("got postform request", "name", actionName,
+ "username", username)
+ userScore, err := h.UserScoreWithActionsByUsername(username)
+ if err != nil {
+ panic(err)
+ }
+ // get action by name
+ action, err := h.repo.DBActionGetByName(actionName)
+ magnitude := int8(action.Magnitude)
+ if action.Type == models.ActionTypeMinus {
+ magnitude *= -1
+ }
+ // change counter of user score
+ userScore.Score += magnitude
+ // disable action if repetable
if err := h.repo.DBActionDone(actionName); err != nil {
panic(err)
}
- userScore, err := h.UserScoreWithActionsByUsername("test")
- if err != nil {
+ // update score in db
+ if err := h.repo.DBUserScoreUpdate(userScore); 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)
}