-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
203 lines (165 loc) · 3.85 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'sinatra'
require 'sinatra/reloader'
require 'csv'
require 'date'
enable :sessions
configure :development, :test do
require 'pry'
end
Dir[File.join(File.dirname(__FILE__), 'lib', '**', '*.rb')].each do |file|
require file
also_reload file
end
def valid_sign_in?(input)
return false if input.values.include?("")
username = input[:username]
password = input[:password]
user_info = retrieve_user_info(username)
user_info[:username] == username && user_info[:password] == password
end
def valid_username?(input)
return false if input.values.include?("")
username = input[:username]
users = []
CSV.foreach('users.csv', headers: true, header_converters: :symbol) do |row|
users << row[1]
end
!users.include?(username)
end
def valid_password?(input)
return false if input.values.include?("")
password = input[:password]
confirm_password = input[:confirm_password]
password == confirm_password
end
def add_user
user = params[:user]
username = params[:username]
password = params[:password]
CSV.open('users.csv', 'a+') do |file|
file << [user, username, password]
end
end
def retrieve_user_info(username)
users = {}
CSV.foreach("users.csv", headers: true, header_converters: :symbol) do |row|
user_hash = row.to_hash
users = user_hash if user_hash[:username] == username
end
users
end
def retrieve_homes_for(user)
homes = []
CSV.foreach("homes.csv", headers: true, header_converters: :symbol) do |row|
homes_hash = row.to_hash
homes << homes_hash[:home] if homes_hash[:user] == user
end
homes
end
def delete(home)
table = CSV.table('homes.csv')
table.delete_if do |row|
row[:home] == home
end
File.open('homes.csv', 'w') do |f|
f.write(table.to_csv)
end
end
def add_home(user, info)
CSV.open("homes.csv", "a") do |csv|
csv << [user, info[:home]]
end
end
def get_reviews(home)
homes = []
CSV.foreach("reviews.csv", headers: true, header_converters: :symbol) do |row|
reviews_hash = row.to_hash
homes << reviews_hash if reviews_hash[:home] == home
end
homes.reverse
end
def store_review(review)
date = Time.now.to_s
date = DateTime.parse(date).strftime("%d/%m/%Y %H:%M")
CSV.open("reviews.csv", "a") do |csv|
csv << [review[:home], review[:reviewer], date, review[:rating], review[:explanation]]
end
end
def go_up_directory(path)
path = path.split("/")
path.pop
path.join("/")
end
get '/' do
redirect('/sign_in')
end
get '/sign_in' do
@input = Hash.new("")
erb :sign_in
end
post '/sign_in' do
if valid_sign_in?(params)
session[:user] = params[:username]
redirect('/foster_homes')
else
@input = params
erb :sign_in
end
end
get '/register' do
@input = Hash.new("")
erb :register
end
post '/register' do
if valid_username?(params) && valid_password?(params)
session[:user] = params[:username]
add_user
redirect('/')
else
@input = params
erb :register
end
end
get '/foster_homes' do
@homes = retrieve_homes_for(session[:user])
erb :foster_homes
end
post '/foster_homes' do
if params[:add]
add_home(session[:user], params)
else
delete(params["home"])
end
redirect('/foster_homes')
end
get '/new_home' do
@input = Hash.new("")
erb :new_home
end
post '/new_home' do
add_home(session[:user], params)
redirect('/foster_homes')
end
get '/foster_homes/reviews/:home' do |home|
@reviews = get_reviews(home)
erb :home_reviews
end
get '/foster_homes/new_review/:home' do |home|
@home = home
erb :review_options
end
get '/foster_homes/new_review/:home/:person' do |home, person|
@home = home
if person == "parents"
erb :parent_review
elsif person == "children"
erb :children_review
else
erb :worker_review
end
end
post '/foster_homes/new_review/:home/:person' do |home, person|
store_review(params)
path = go_up_directory(request.path)
redirect(path)
end