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:
@@ -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, ×tamp)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user