Added a way to see report/statements. Added more gems to the Gemfile.
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user