-
Notifications
You must be signed in to change notification settings - Fork 0
/
plant_scheduler.rb
192 lines (149 loc) · 3.97 KB
/
plant_scheduler.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
require "sinatra"
require "sinatra/reloader" if development?
require "tilt/erubis"
require "yaml"
require "fileutils"
require "bcrypt"
require "securerandom"
require_relative "lib/plant_scheduler_helper_methods"
require_relative "lib/html_helper_methods"
configure do
enable :sessions
set :erb, escape_html: true
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }
end
before "/:user/:plant_id/*" do
@username = params[:user]
@plant_id = params[:plant_id].to_i
required_current_user(@username)
end
before "/:user/*" do
@username = params[:user]
required_current_user(@username)
end
get "/" do
redirect "/home"
end
# Renders the Home Page
get "/home" do
@users = load_user_credentials.keys
erb :home
end
# Renders the login page
get "/login" do
required_logged_out_user
erb :login
end
# Logs in a user
post "/signin" do
required_logged_out_user
username, password = params[:username], params[:password]
if valid_credentials?(username, password)
session[:username] = username
session[:message] = "Welcome back #{username}!"
redirect "/"
else
status 422
session[:message] = 'Invalid login info!'
erb :login
end
end
# Logs out a user
get "/logout" do
if !session[:username]
session[:message] = "You are not logged in!"
redirect "/"
end
session.delete(:username)
session[:message] = "You have successfully logged out"
redirect "/"
end
# Renders the signup page
get "/signup" do
required_logged_out_user
erb :signup
end
# Signs up a new user
post "/signup" do
required_logged_out_user
username, password = params[:username], params[:password]
credentials = load_user_credentials
if invalid_signup_message(credentials, username, password, params[:password_confirm])
status 422
erb :signup
else
credentials[username] = BCrypt::Password.create(password)
update_users(credentials)
session[:username] = username
session[:message] = "Welcome #{username}!"
redirect "/"
end
end
# Render a user page
get "/:user" do
@username = params[:user]
plants = load_plants_file[@username] || []
@plants = sort_by_unwatered(plants)
erb :user
end
# Render the new plant page
get "/:user/new" do
erb :new_plant
end
# Add a new plant to a user
post "/:user/new" do
format_plant_info(params)
if invalid_plant_input
status 422
erb :new_plant
else
add_new_plant(@username)
session[:message] = "Your plant family has sprouted a new friend!"
redirect "/#{@username}"
end
end
# Delete a current user's plant
post "/:user/:plant_id/delete" do
plants = load_plants_file[@username]
plant_does_not_exist(@username) unless plants[@plant_id]
plants.delete(@plant_id)
update_plant_file(plants)
session[:message] = "We're mourning the loss of your plant as well"
redirect "/#{@username}"
end
# Water a current user's plant
post "/:user/:plant_id/water" do
plants = load_plants_file
plant = plants[@username][@plant_id]
plant_does_not_exist(@username) unless plant
water_plant(plant)
update_plant_file(plants)
session[:message] = "You've watered your plant!"
redirect "/#{@username}"
end
# Render view edit page for plant
get "/:user/:plant_id/edit" do
plant_info = []
plant = load_plants_file[@username][@plant_id]
PLANT_INFO_TYPES.each_with_index do |info, idx|
plant_info[idx] = plant[info]
end
@type, @photo, @schedule, @last_water, @notes = plant_info
erb :edit_plant
end
# Updates a plant
post "/:user/:plant_id/edit" do
updated_info = params[:type], params[:photo], params[:schedule], [Date.today], params[:notes]
@type, @photo, @schedule, @last_water, @notes = updated_info
if invalid_plant_input
status 422
erb :edit_plant
else
plants = load_plants_file
plant = plants[@username][@plant_id]
update_plant_info(plant, updated_info, skip: 'last_water')
update_plant_file(plants)
session[:message] = "The info was successfully updated!"
redirect "/#{@username}"
end
end