30 lines
766 B
Ruby
30 lines
766 B
Ruby
require 'mysql2'
|
|
|
|
load 'config.rb'
|
|
|
|
db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB)
|
|
|
|
person = ARGV[0]
|
|
product = ARGV[1]
|
|
amount = ARGV[2]
|
|
log_dir = "./log/"
|
|
|
|
amount = 1.0 unless amount
|
|
|
|
valid_products = []
|
|
db.query("SELECT * FROM products").each do |product|
|
|
valid_products << product['id']
|
|
end
|
|
|
|
unless valid_products.include? product
|
|
raise "Product not found in table\nValid products are: #{valid_products}"
|
|
end
|
|
|
|
statement = db.prepare("INSERT INTO transactions (buyer, product, amount) VALUES (?, ?, ?)")
|
|
result = statement.execute(person,product,amount)
|
|
puts result
|
|
|
|
#output = File.open("#{log_dir}/matkant-#{Time.now.strftime('%Y%W')}.log","a+")
|
|
#output << "#{Time.now.to_i};#{person};#{product};#{amount}\n"
|
|
#output.close
|