-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
178 lines (133 loc) · 2.84 KB
/
main.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
require 'sinatra'
require 'sinatra/reloader' if development?
require 'pry' if development?
require_relative 'models/user.rb'
require_relative 'models/card.rb'
enable :sessions
get '/' do
erb :landing
end
get '/user/new' do
erb :'/user/user_new'
end
post '/user' do
create_user(params[:email], params[:pw])
session[:user_id] = get_user_by_email(params[:email])["id"]
redirect '/index'
end
get '/user/login' do
# unless coming from home page
# redirect '/'
# end
erb :'/user/user_login'
end
post '/user/login' do
user = get_user_by_email(params[:email])
if user && BCrypt::Password.new(user["pw_digest"]) == params[:pw]
session[:user_id] = user["id"]
redirect '/index'
else
redirect '/user/login'
end
end
delete '/session' do
session[:user_id] = nil
redirect '/'
end
get '/index' do
user = get_user_by_id(session[:user_id])
if user == nil
redirect '/'
else
cards = all_cards_for_user(user["id"])
erb :'cards/index', locals: {
user: user,
cards: cards
}
end
end
get '/show/:id' do
card = get_card_by_id(params[:id])
user = get_user_by_id(session[:user_id])
# the card as a number as source
# we want a name
if card["source"] == nil
card["source"] = "No-one"
else
knows_card = get_card_by_id(card["source"])
card["source"] = knows_card["name"]
end
# show the targets
# find all the cards that have source is this card's id
targets = get_targets(params[:id])
erb :'/cards/show', locals: {
card: card,
user: user,
source_card: knows_card,
targets: targets
}
end
get '/card/:id/edit' do
card = get_card_by_id(params[:id])
user = get_user_by_id(session[:user_id])
erb :'/cards/edit', locals: {
card: card,
user: user
}
end
patch '/card' do
edit_card(params[:id],
params[:name],
params[:image],
params[:workplace],
params[:job],
params[:met],
params[:source],
params[:skills],
params[:quality],
params[:notes],
)
redirect "/index"
end
get '/card/new' do
user = get_user_by_id(session[:user_id])
erb :'cards/new', locals: {
user: user
}
end
post'/card' do
create_card(
params[:name],
params[:image],
params[:workplace],
params[:job],
params[:met],
params[:source],
params[:skills],
params[:quality],
params[:notes],
session[:user_id]
)
redirect '/index'
end
get '/card/:id/delete' do
card = get_card_by_id(params[:id])
user = get_user_by_id(session[:user_id])
erb :'cards/delete', locals: {
card: card,
user: user
}
end
delete '/card' do
delete_card(params[:id])
redirect "/index"
end
get '/search' do
results = search(params[:search])
user = get_user_by_id(session[:user_id])
erb :'/cards/search_results', locals: {
results: results,
user: user,
search_term: params[:search]
}
end