-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.rb
144 lines (127 loc) · 3.34 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'bundler'
Bundler.require;
require 'sinatra/activerecord'
Dir.glob('./lib/*.rb') do |model|
require model
end
class Inspiration < Sinatra::Application
configure :development do
set :database, 'sqlite3:///database.db'
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
before do
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers' => 'Content-Type'
end
set :protection, false
# show everything
get '/' do
@users = User.all
@authors = Author.all
@quotes = Quote.all
File.read(File.join('public/app', 'index.html'))
end
# all quotes
get '/quotes' do
@quotes = Quote.all
@quotes.to_json(:only => [:id, :body],
:include => {
:author => {
:only => [:id, :firstName, :lastName]}})
end
# create new quote + author
post '/quotes' do
begin
params.merge! JSON.parse(request.env["rack.input"].read)
rescue JSON::ParserError
logger.error "Cannot parse request body."
end
@author = Author.find_or_create_by(params[:author])
@author.quotes.build(:body => params[:body])
@author.save!
redirect '/'
end
# show specific quote + author
get '/quotes/:id' do
@quote = Quote.find(params[:id])
@author = Author.find(@quote.author_id)
results = {}
results["quote"] = @quote
results["author"] = @author
results.to_json
end
# edit quote and author
put '/quotes/edit/:id' do
begin
params.merge! JSON.parse(request.env["rack.input"].read)
rescue JSON::ParserError
logger.error "Cannot parse request body."
end
@quote = Quote.find(params[:id])
@author = Author.find(@quote.author_id)
if @quote.update(:body => params[:body]) && @author.update(params[:author])
status 201
redirect '/'
else
status 400
reload
end
end
# delete
delete '/quotes/:id' do
Quote.find(params[:id]).destroy
redirect '/'
end
# all authors
get '/authors' do
@authors = Author.all
@authors.to_json
end
# show specific author + quotes
get '/authors/:id' do
@author = Author.find(params[:id])
puts @author.lastName
@quotes = Quote.where(:author_id => @author.id)
results = {}
results["author"] = @author
results["quotes"] = @quotes
results.to_json
end
# edit author
put '/authors/edit/:id' do
begin
params.merge! JSON.parse(request.env["rack.input"].read)
rescue JSON::ParserError
logger.error "Cannot parse request body."
end
@author = Author.find(params[:id])
if @author.update(params[:author])
status 201
redirect '/'
else
status 400
reload
end
end
# delete an author and all their quotes
delete '/authors/:id' do
@author = Author.find(params[:id])
@quotes = Quote.where(:author_id => @author.id).all
@quotes.each do |quote|
quote.destroy
end
@author.destroy
redirect '/'
end
end