-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
144 lines (117 loc) · 2.89 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
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/cross_origin'
require 'json'
require 'erb'
Dir[File.join('models', '**/*.rb')].each do |f|
require_relative f
end
set :port, 3000
configure do
enable :cross_origin
end
options "*" do
response.headers["Allow"] = "HEAD,GET,PUT,POST,DELETE,OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept"
200
end
get '/' do
'94% Server Admin Interface'
end
get '/entry' do
content_type :json
Entry.all.as_json.to_json
end
get '/level' do
content_type :json
Level.all.as_json(include: { questions: { only: [:id, :text] } }).to_json
end
get '/level/:id' do
content_type :json
level = Level.find_by_id(params[:id])
if level
level.as_json(include: { questions: { only: [:id,:text] } }).to_json
else
status 404
'Not found'
end
end
get '/question' do
content_type :json
Question.all.as_json(include: { answers: { only: [:id, :percent, :text] } }).to_json
end
get '/question/:id' do
content_type :json
question = Question.find_by_id(params[:id])
if question
question.as_json(include: { answers: { only: [:id, :percent, :text] } }).to_json
else
status 404
'Not found'
end
end
post '/createUser' do
content_type :json
payload = JSON.parse(request.body.read, symbolize_names: true)
age = payload[:age].to_i
gender = payload[:gender].to_i
education = payload[:education].to_i
employment_status = payload[:employment_status].to_i
newUser = User.create({ age: age, gender: gender, education: education, employment_status: employment_status })
newUser.as_json.to_json
end
post '/createEntry' do
content_type :json
payload = JSON.parse(request.body.read, symbolize_names: true)
question_id = payload[:question_id].to_i
user_id = payload[:user_id].to_i
text = payload[:text]
newEntry = Entry.create({ question_id: question_id, user_id: user_id, text: text })
newEntry.as_json.to_json
end
get '/user' do
content_type :json
User.all.as_json.to_json
end
get '/user/:id' do
content_type :json
user = User.find_by_id(params[:id])
if user
user.as_json.to_json
else
status 404
'Not found'
end
end
get '/admin' do
erb :admin
end
get '/admin/level' do
erb :level
end
post '/admin/level' do
number = params[:level_number]
Level.create({number: number})
redirect back
end
get '/admin/question' do
@levels = Level.all
erb :question
end
post '/admin/question' do
text = params[:text]
level_id = params[:level_id].to_i
Question.create({ text: text, level_id: level_id })
redirect back
end
get '/admin/answer' do
@questions = Question.all
erb :answer
end
post '/admin/answer' do
text = params[:text]
question_id = params[:question_id].to_i
percent = params[:percent].to_i
Answer.create({ text: text, question_id: question_id, percent: percent })
redirect back
end