Added a way to see report/statements. Added more gems to the Gemfile.

This commit is contained in:
2026-01-05 20:20:25 -03:00
parent d08af2f294
commit 689079ce21
4 changed files with 152 additions and 1 deletions

View File

@@ -85,4 +85,25 @@ class Database
@conn.exec_params(sql_update, [user_id, amount])
end
end
def fetch_report(user_id, interval_string)
sql = <<~SQL
SELECT
SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as income,
SUM(CASE WHEN amount < 0 THEN amount ELSE 0 END) as expenses,
SUM(amount) AS net_change
FROM transactions
WHERE user_id = $1
AND created_at >= NOW() - $2::INTERVAL
SQL
result = @conn.exec_params(sql, [user_id, interval_string])
row = result[0]
{
income: row['income'].to_i,
expenses: row['expenses'].to_i,
net: row['net_change'].to_i
}
end
end