Implement get goals (#25)

Closes #13

Reviewed-on: #25
This commit was merged in pull request #25.
This commit is contained in:
2025-05-08 11:51:16 +02:00
parent 5a19fc041d
commit aa26a8f338
4 changed files with 95 additions and 3 deletions

View File

@@ -57,3 +57,32 @@ func (db *Db) GetUser(id int) (*User, error) {
}
return user, nil
}
func (db *Db) UserExists(userId int) (bool, error) {
count := 0
err := db.db.Query("select count(*) from users where id = ?").
Bind(userId).ScanSingle(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
func (db *Db) GetUserGoals(userId int) ([]Goal, error) {
goals := make([]Goal, 0)
var err error
for row := range db.db.Query("select id, name, target, progress, weight from goals where user_id = ?").
Bind(userId).Range(&err) {
goal := Goal{}
err = row.Scan(&goal.ID, &goal.Name, &goal.Target, &goal.Progress, &goal.Weight)
if err != nil {
return nil, err
}
goals = append(goals, goal)
}
if err != nil {
return nil, err
}
return goals, nil
}