-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
135 lines (101 loc) · 3.14 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
require './env'
require 'bundler/setup'
require 'json'
require 'sinatra'
require 'sinatra/json'
require 'sinatra/activerecord'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
class SlidesApplication < Sinatra::Application
register Sinatra::ActiveRecordExtension
enable :sessions
configure :development, :test do
require 'pry'
end
#Serve the index document
get '/' do
File.read(File.join('public', 'index.html'))
end
get '/api/courses' do
json Course.all
end
get '/api/courses/:course' do
begin
json Course.find(params[:course])
rescue ActiveRecord::RecordNotFound
halt 404
end
end
get '/api/courses/:course/presentations' do
begin
json Course.find( params[:course] ).presentations.order( :position )
rescue ActiveRecord::RecordNotFound
halt 404
end
end
post '/api/courses/:course/presentations' do
body = JSON.parse request.body.read
presentation = Presentation.new
presentation_save( params[:course] , presentation , body )
end
get '/api/courses/:course/presentations/:presentation' do
begin
content_type :json
Presentation.find( params[:presentation] ).to_json :methods => :content
rescue ActiveRecord::RecordNotFound
halt 404
end
end
put '/api/courses/:course/presentations/:presentation' do
body = JSON.parse request.body.read
presentation = Presentation.find( params[:presentation] )
presentation_save( presentation.course_id , presentation , body )
end
delete '/api/courses/:course/presentations/:presentation' do
presentation = Presentation.find( params[:presentation] )
presentation.delete
end
def presentation_save( course_id , presentation , body )
ActiveRecord::Base.transaction do
#Save presentation
presentation.course_id = course_id
presentation.name = body['name']
presentation.position = 0 # TODO Set the position as the last item
presentation.save!
# Find the slides not available in the body
toDelete = Array.new(presentation.slides).reject! { | slide |
body['slides'].index { | slideBody | slideBody["id"] == slide.id }
}
toDelete.each { | slide | slide.destroy } if toDelete != nil
#Update or craete new slides
i = 0
presentation.slides = body['slides'].map do | slideBody |
if( slideBody['id'] )
slide = Slide.find( slideBody['id'] )
else
slide = Slide.new
end
puts i
slide.content = slideBody['content']
slide.valign = slideBody['valign'] || 'top'
slide.halign = slideBody['halign'] || 'left'
slide.position = i
slide.presentation_id = presentation.id
slide.save!
i += 1
slide
end
end
""
end
get '/api/courses/:course/presentations/:presentation/slides' do
begin
json Presentation.find( params[:presentation] ).slides.sort_by(&:position)
rescue ActiveRecord::RecordNotFound
halt 404
end
end
not_found do
"Not Found"
end
run! if __FILE__ == $0
end