-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
51 lines (42 loc) · 1.15 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/json'
set :database, { adapter: 'sqlite3', database: 'transactions.sqlite' }
class Transaction < ActiveRecord::Base
validates :amount, presence: true
end
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => 'Content-Type'
begin
params.merge! JSON.parse(request.env["rack.input"].read)
rescue JSON::ParserError
logger.error "Cannot parse request body."
end
end
get "/transactions" do
json Transaction.all
end
get "/transactions/:id" do
json Transaction.find(params[:id])
end
delete "/transactions/:id" do
transaction = Transaction.find(params[:id])
transaction.destroy!
json status: :ok
end
put "/transactions/:id" do
transaction = Transaction.find(params[:id])
transaction.update_attributes(params[:transaction])
json status: :ok
end
options '/transactions' do
200
end
post "/transactions" do
puts params.inspect
transaction = Transaction.create!(params[:transaction])
json status: :ok, transaction: transaction
end