From f71a43e1a4cf2fba8a479842d0a61e24017cd87b Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sun, 14 Apr 2024 09:11:15 +0300 Subject: Feat: db methods and their usage --- internal/database/repos/userscore.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 internal/database/repos/userscore.go (limited to 'internal/database/repos/userscore.go') 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 +} -- cgit v1.2.3