Add history description (#98)
All checks were successful
Backend Deploy / build (push) Successful in 3m1s
Backend Build and Test / build (push) Successful in 3m5s

Closes #20

Reviewed-on: #98
This commit was merged in pull request #98.
This commit is contained in:
2025-05-27 10:55:13 +02:00
parent 56a19acd0f
commit db2f518cc2
7 changed files with 51 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"log"
"math"
"time"
@@ -206,8 +207,9 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
// Get the cost of the allowance
var cost int
err = tx.Query("select balance from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost)
var allowanceName string
err = tx.Query("select balance, name from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost, &allowanceName)
if err != nil {
return err
}
@@ -220,8 +222,8 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
}
// Add a history entry
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost, fmt.Sprintf("Allowance completed: %s", allowanceName)).
Exec()
if err != nil {
return err
@@ -420,7 +422,8 @@ func (db *Db) CompleteTask(taskId int) error {
defer tx.MustRollback()
var reward int
err = tx.Query("select reward from tasks where id = ?").Bind(taskId).ScanSingle(&reward)
var rewardName string
err = tx.Query("select reward, name from tasks where id = ?").Bind(taskId).ScanSingle(&reward, &rewardName)
if err != nil {
return err
}
@@ -434,8 +437,8 @@ func (db *Db) CompleteTask(taskId int) error {
}
// Add the history entry
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), reward).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), reward, fmt.Sprintf("Task completed: %s", rewardName)).
Exec()
if err != nil {
return err
@@ -499,8 +502,8 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
defer tx.MustRollback()
amount := int(math.Round(allowance.Allowance * 100.0))
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), amount).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), amount, allowance.Description).
Exec()
if err != nil {
return err
@@ -512,11 +515,11 @@ func (db *Db) GetHistory(userId int) ([]History, error) {
history := make([]History, 0)
var err error
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
for row := range db.db.Query("select amount, `timestamp`, description from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) {
allowance := History{}
var timestamp, amount int64
err = row.Scan(&amount, &timestamp)
err = row.Scan(&amount, &timestamp, &allowance.Description)
if err != nil {
return nil, err
}