From ac70f43c3e52e35195353c73af5687b8286ff581 Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Mon, 15 Apr 2024 09:55:08 +0300 Subject: Feat: update method on burn_time --- internal/database/repos/action.go | 15 +++++++++++++++ internal/handlers/main.go | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/database/repos/action.go b/internal/database/repos/action.go index 31018b7..6024dd5 100644 --- a/internal/database/repos/action.go +++ b/internal/database/repos/action.go @@ -6,6 +6,7 @@ type ActionRepo interface { DBActionCreate(req *models.Action) error DBActionList(username string) ([]models.Action, error) DBActionDone(name string) error + DBActionsToReset() error } func (p *Provider) DBActionCreate(req *models.Action) error { @@ -26,5 +27,19 @@ func (p *Provider) DBActionList(username string) ([]models.Action, error) { func (p *Provider) DBActionDone(name string) error { // should reset at burn time + stmt := "UPDATE action SET done=true WHERE name=$1;" + _, err := p.db.Exec(stmt, name) + return err +} + +func (p *Provider) DBActionsToReset() error { + stmts := []string{`UPDATE action SET done=false WHERE username IN (SELECT username FROM user_score WHERE burn_time < NOW());`, + `UPDATE user_score SET burn_time=burn_time + INTERVAL '24 hours' WHERE burn_time < NOW();`} + for _, s := range stmts { + if _, err := p.db.Exec(s); err != nil { + // rollback + return err + } + } return nil } diff --git a/internal/handlers/main.go b/internal/handlers/main.go index cb872f0..d779f42 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -131,7 +131,6 @@ func (h *Handlers) UserScoreWithActionsByUsername( if err != nil { return nil, err } - list, err := h.repo.DBActionList("test") if err != nil { return nil, err -- cgit v1.2.3