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 | |
parent | 9b160dcc0315aaf54a8b4588be9d2a0c98084cf0 (diff) |
Feat: db methods and their usage
Diffstat (limited to 'internal')
-rw-r--r-- | internal/database/repos/action.go | 30 | ||||
-rw-r--r-- | internal/database/repos/userscore.go | 32 | ||||
-rw-r--r-- | internal/handlers/main.go | 69 | ||||
-rw-r--r-- | internal/models/models.go | 25 |
4 files changed, 126 insertions, 30 deletions
diff --git a/internal/database/repos/action.go b/internal/database/repos/action.go new file mode 100644 index 0000000..31018b7 --- /dev/null +++ b/internal/database/repos/action.go @@ -0,0 +1,30 @@ +package repos + +import "apjournal/internal/models" + +type ActionRepo interface { + DBActionCreate(req *models.Action) error + DBActionList(username string) ([]models.Action, error) + DBActionDone(name string) error +} + +func (p *Provider) DBActionCreate(req *models.Action) error { + _, err := p.db.NamedExec(` + INSERT INTO action(username, name, magnitude, repeatable, type, done) + VALUES (:username, :name, :magnitude, :repeatable, :type, :done);`, req) + return err +} + +func (p *Provider) DBActionList(username string) ([]models.Action, error) { + resp := []models.Action{} + query := "SELECT * FROM action WHERE username=$1;" + if err := p.db.Select(&resp, query, username); err != nil { + return nil, err + } + return resp, nil +} + +func (p *Provider) DBActionDone(name string) error { + // should reset at burn time + return nil +} diff --git a/internal/database/repos/userscore.go b/internal/database/repos/userscore.go new file mode 100644 index 0000000..5c09004 --- /dev/null +++ b/internal/database/repos/userscore.go @@ -0,0 +1,32 @@ +package repos + +import "apjournal/internal/models" + +type UserScoreRepo interface { + DBUserScoreCreate(req *models.UserScore) error + DBUserScoreGet(username string) (*models.UserScore, error) + DBUserScoreUpdate(req *models.UserScore) error +} + +func (p *Provider) DBUserScoreCreate(req *models.UserScore) error { + _, err := p.db.NamedExec(` + INSERT INTO user_score(username, burn_time, score) + VALUES (:username, :burn_time, :score);`, req) + return err +} + +func (p *Provider) DBUserScoreGet(username string) (*models.UserScore, error) { + resp := models.UserScore{} + query := "SELECT * FROM user_score WHERE username=$1;" + if err := p.db.Get(&resp, query, username); err != nil { + return nil, err + } + return &resp, nil +} + +func (p *Provider) DBUserScoreUpdate(req *models.UserScore) error { + stmt := `UPDATE user_score SET burn_time=$1, score=$2 + WHERE username=$3;` + _, err := p.db.Exec(stmt, req.BurnTime, req.Score, req.Username) + return err +} 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) } diff --git a/internal/models/models.go b/internal/models/models.go index a2b3628..bd38eaf 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -11,19 +11,22 @@ const ( type ( UserScore struct { - Username string - Actions []*Action - BurnTime time.Time - Score int8 + ID uint32 `db:"id"` + Username string `db:"username"` + Actions []Action + BurnTime time.Time `db:"burn_time"` + Score int8 `db:"score"` + CreatedAt time.Time `db:"created_at"` } Action struct { - ID uint32 - Name string - Magnitude uint8 - Repeatable bool - Type ActionType - Done bool - Username string + ID uint32 `db:"id"` + Name string `db:"name"` + Magnitude uint8 `db:"magnitude"` + Repeatable bool `db:"repeatable"` + Type ActionType `db:"type"` + Done bool `db:"done"` + Username string `db:"username"` + CreatedAt time.Time `db:"created_at"` } ) |