-
Notifications
You must be signed in to change notification settings - Fork 3
/
webapp.rb
153 lines (134 loc) · 3.4 KB
/
webapp.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
145
146
147
148
149
150
151
152
153
require 'sinatra'
require 'sinatra/base'
require 'sinatra/activerecord'
require 'rack-flash'
enable :sessions
set :session_secret, 'a book is a dream that you hold in your hand'
use Rack::Flash
require_relative 'ext/failure_rate'
require_relative 'resources/author'
require_relative 'resources/book'
# index
get '/' do
@books = Book.all(:params => {:order => "lower(title)"})
@books_by_author = @books.group_by(&:author_id)
@authors = Author.all(:params => {:order => "lower(last_name)"})
@authors_by_id = @authors.index_by(&:id)
erb :index
end
# show author
get '/authors/:id' do
rescue_not_found do
@author = Author.find(params[:id])
@authors_by_id = [@author].index_by(&:id)
@books = Book.all(:params => {:author_id => @author.id, :order => "lower(title)"})
erb :author
end
end
# create author
post '/authors' do
author = Author.create(params)
if author.valid?
flash[:notice] = "Author \"#{author.name}\" created"
redirect to("/authors/#{author.id}")
else
flash[:error] = "Author create failed: #{author.errors.full_messages.join("; ")}"
redirect to("/")
end
end
# edit author form
get '/authors/:id/edit' do
rescue_not_found do
@author = Author.find(params[:id])
erb :edit_author
end
end
# edit author
post '/authors/:id/edit' do
rescue_not_found do
author = Author.find(params[:id])
if author.update_attributes(params.slice(*%w{first_name last_name}))
flash[:notice] = "Author \"#{author.name}\" updated"
else
flash[:error] = "Author update failed: #{author.errors.full_messages.join("; ")}"
end
redirect to("/authors/#{author.id}")
end
end
# delete author
post '/authors/:id/delete' do
rescue_not_found do
if Author.delete(params[:id])
flash[:notice] = "Author deleted"
else
flash[:error] = "Failed to delete author"
end
redirect to("/")
end
end
# show book
get '/books/:id' do
rescue_not_found do
@book = Book.find(params[:id])
@author = Author.find(@book.author_id)
erb :book
end
end
# create book
post '/books' do
book = Book.create(params)
if book.valid?
flash[:notice] = "Book \"#{book.title}\" created"
redirect to("/books/#{book.id}")
else
flash[:error] = "Book create failed: #{book.errors.full_messages.join("; ")}"
redirect to("/")
end
end
# edit book form
get '/books/:id/edit' do
rescue_not_found do
@book = Book.find(params[:id])
@authors = Author.all(:params => {:order => "lower(last_name)"})
erb :edit_book
end
end
# edit book
post '/books/:id/edit' do
rescue_not_found do
book = Book.find(params[:id])
if book.update_attributes(params.slice(*%w{title author_id pages}))
flash[:notice] = "Book \"#{book.title}\" updated"
else
flash[:error] = "Book update failed: #{book.errors.full_messages.join("; ")}"
end
redirect to("/books/#{book.id}")
end
end
# delete book
post '/books/:id/delete' do
rescue_not_found do
if Book.delete(params[:id])
flash[:notice] = "Book deleted"
else
flash[:error] = "Failed to delete book"
end
redirect to("/")
end
end
get '/reset' do
Author.find(:all, :params => {:last_name => "Cline"}).each do |author|
author.destroy
end
204
end
get '/ping' do
[200, {"Content-Type" => "text/plain"}, "pong"]
end
private
def rescue_not_found(&block)
yield
rescue ActiveResource::ResourceNotFound
flash[:error] = "Not found"
redirect to("/")
end