Add endpoint to get allowance history (#37)

Closes #12

Reviewed-on: #37
This commit was merged in pull request #37.
This commit is contained in:
2025-05-14 15:27:29 +02:00
parent 2486bbf1ec
commit 4355e1b1b7
8 changed files with 106 additions and 39 deletions

View File

@@ -243,7 +243,7 @@ func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
}
defer tx.MustRollback()
err = tx.Query("insert into history (user_id, date, amount) values (?, ?, ?)").
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), allowance.Allowance).
Exec()
if err != nil {
@@ -251,3 +251,24 @@ func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error {
}
return tx.Commit()
}
func (db *Db) GetHistory(userId int) ([]Allowance, error) {
history := make([]Allowance, 0)
var err error
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) {
allowance := Allowance{}
var timestamp int64
err = row.Scan(&allowance.Allowance, &timestamp)
if err != nil {
return nil, err
}
allowance.Timestamp = time.Unix(timestamp, 0)
history = append(history, allowance)
}
if err != nil {
return nil, err
}
return history, nil
}