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 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'internal/database/repos/action.go') 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 } -- cgit v1.2.3