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/database | |
parent | 9b160dcc0315aaf54a8b4588be9d2a0c98084cf0 (diff) |
Feat: db methods and their usage
Diffstat (limited to 'internal/database')
-rw-r--r-- | internal/database/repos/action.go | 30 | ||||
-rw-r--r-- | internal/database/repos/userscore.go | 32 |
2 files changed, 62 insertions, 0 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 +} |