Localization added, but not implemented yet.
This commit is contained in:
@@ -50,19 +50,23 @@ class Database
|
||||
@conn.exec(sql_wallet)
|
||||
@conn.exec(sql_ledger)
|
||||
@conn.exec(sql_index)
|
||||
|
||||
begin
|
||||
@conn.exec("ALTER TABLE wallets ADD COLUMN IF NOT EXISTS locale VARCHAR(5) DEFAULT 'en'")
|
||||
rescue PG::Error => e
|
||||
puts "Migration note: #{e.message}"
|
||||
end
|
||||
|
||||
puts "Database tables have been initialized."
|
||||
end
|
||||
|
||||
# We pass the user_id
|
||||
def get_currency(user_id)
|
||||
# 1. Run the query using parameters ($1) to prevent SQL injection
|
||||
# Run the query using parameters ($1) to prevent SQL injection
|
||||
result = @conn.exec_params("SELECT amount FROM wallets WHERE user_id = $1", [user_id])
|
||||
|
||||
# 2. Check if the user exists
|
||||
if result.num_tuples.zero?
|
||||
return 0 # User has no money/row yet
|
||||
else
|
||||
# 3. Return the value (don't print it)
|
||||
return result[0]['amount'].to_i
|
||||
end
|
||||
end
|
||||
@@ -106,4 +110,23 @@ class Database
|
||||
net: row['net_change'].to_i
|
||||
}
|
||||
end
|
||||
|
||||
def get_language(user_id)
|
||||
result = @conn.exec("SELECT locale FROM wallets WHERE user_id = $1", [user_id])
|
||||
|
||||
return nil if result.num_tuples.zero?
|
||||
|
||||
return result[0]['locale']
|
||||
end
|
||||
|
||||
def set_language(user_id, locale)
|
||||
sql = <<~SQL
|
||||
INSERT INTO wallets(user_id, amount, locale)
|
||||
VALUES ($1, 0, $2)
|
||||
ON CONFLICT (user_id)
|
||||
DO UPDATE SET locale = $2
|
||||
SQL
|
||||
|
||||
@conn.exec(sql, [user_id, locale])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user