forked from shyam/openlibrary
-
Notifications
You must be signed in to change notification settings - Fork 11
/
openlibrary.rb
170 lines (140 loc) · 3.73 KB
/
openlibrary.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra/content_for'
require 'sinatra/flash'
Dir[File.dirname(__FILE__) + "/lib/*.rb"].each { |lib| require lib }
require './database'
set :environment, :production
set :logging, true
set :public_folder, File.dirname(__FILE__) + '/public'
set :views, File.dirname(__FILE__) + '/views'
enable :sessions
get '/' do
with_plain_layout :index
end
get '/issued-books' do
@reservations = Reservation.all({:state => :issued.to_s})
with_plain_layout :issued_books
end
get '/books' do
@books = Book.all
@reservations = Reservation.all(state: :issued.to_s).to_a
with_plain_layout :books
end
get '/books/:isbn' do
content_type :json
load_book
not_found and return if @book.nil?
@reservation = Reservation.last(:book => @book)
load_messages
without_layout :book_info
end
post '/books/:isbn' do
@book = Book.first(isbn: params[:isbn])
@book.update(params[:book])
without_layout(:donate_book)
end
get '/users/:employee_id' do
load_user
not_found and return unless @user
without_layout :user_info
end
get '/users' do
@users = User.all(:order => [:id.desc])
with_plain_layout :users
end
post '/user/create' do
user = User.create(params[:user].merge(employee_id: params[:user]["employee_id"].to_i))
if user.errors.empty?
flash[:success] = "Successfully created user !!!"
else
flash[:error] = user.errors.full_messages.join(", ")
end
redirect '/users'
end
get '/barcode/:employee_id/create' do
Process.detach(barcode_job)
redirect '/barcode/success'
end
get '/barcode/success' do
with_plain_layout :barcode_success
end
get '/donate' do
with_plain_layout :donate
end
post '/donate' do
@book_found = load_book
@book = Book.create_from_google_api(params[:isbn]) unless @book_found
without_layout(:donate_book)
end
post '/add_copies' do
@book_found = load_book
params[:copies_to_add].to_i.times{ @book.book_copies << BookCopy.create(book_id: @book.id) }
without_layout(:donate_book)
end
get '/users/:employee_id/reserve/:isbn' do
load_user_and_book
load_messages
not_found and return if @user.nil? || @book.nil?
criteria = {:user => @user, :book => @book, :state => :issued.to_s}
@reservation = get_reservation criteria
@reservation.save
send("send_#{@reservation.state}_msg")
without_layout :reservation
end
post '/send-reminder-mail' do
content_type :json
@reservations = Reservation.all({:state => :issued.to_s, :created_on.lt => Date.today - 30})
@reservations.each do |reservation|
send_reminder_mail(reservation)
end
"Success"
end
def with_plain_layout template, options={}
@menu_items = YAML::load(File.read(File.expand_path('config/menu.yml','.')))
erb template, options.merge(:layout => :'layout/plain')
end
def without_layout template
erb template
end
private
def barcode_job
fork do
load_user
exec "sh ./create_barcode.sh #{params[:employee_id]}"
email = Email.new(@user, nil)
email.send_barcode_image
end
end
def send_issued_msg
email = Email.new(@user, @book)
email.send_issued_msg
end
def send_returned_msg
email = Email.new(@user, @book)
email.send_returned_msg
end
def send_reminder_mail(reservation)
email = Email.new(reservation.user, reservation.book)
email.send_reminder_msg(reservation.created_on)
end
def load_user_and_book
load_user
load_book
end
def load_user
@user = User.first(:employee_id => params[:employee_id].to_i)
end
def load_book
@book = Book.first(:isbn => params[:isbn])
end
def load_messages
@messages = YAML::load(File.read(File.expand_path('config/en.yml','.')))
end
def get_reservation criteria
reservation = Reservation.first(criteria)
reservation.forward! unless reservation.nil?
reservation ||= Reservation.create(criteria)
return reservation
end