/
app.rb
215 lines (176 loc) · 4.9 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
204
205
206
207
208
209
210
211
212
213
214
215
require 'rubygems'
require 'rack-flash'
require 'sinatra'
require 'sinatra/content_for'
require 'sinatra/redirect_with_flash'
require 'sinatra/reloader' if development?
require 'haml'
require 'sass'
require 'dm-core'
require 'dm-validations'
require 'dm-migrations'
require 'dm-timestamps'
require 'dm-serializer/to_json'
use Rack::Flash
enable :sessions
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/shdhers.sqlite3")
class User
include DataMapper::Resource
property :slug, String, :key => true
validates_uniqueness_of :slug, :message => "There's already a user with this slug"
property :first_name, String, :required => true
property :last_name, String, :required => true
property :email, String, :format => :email_address, :unique => true
property :twitter, String
property :github, String
property :personal, String
property :expertise, Object
property :interests, Object
property :location, String
property :created_at, DateTime
property :updated_at, DateTime
end
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true, :unique => true
end
DataMapper.auto_upgrade!
# === Errors ===============================
not_found do
haml :e404, :layout => false
end
# === Home ===============================
get '/' do
haml :index, :layout => false
end
# === Users ===============================
# Create
get '/new' do
haml :new
end
post '/new' do
@interests = params[:user][:interests] || []
@expertise = params[:user][:expertise] || []
@tags = @interests + @expertise
@tags.each do |t|
@tag = Tag.first_or_create(:name => t)
end
["all","new","create","edit","update","list","show","error","user","shdh","devhouse","superhappydevhouse","delete","tags","tag","interests","experience","location","locations"].each do |s|
if s == params[:user][:slug]
redirect "/new", :notice => 'You are trying to use a reserved word as slug'
end
end
@user = User.new(params[:user])
if @user.save
redirect "/all"
else
redirect "/new", :notice => 'Something went wrong'
end
end
#Show all
get '/all' do
@users = User.all(:order => [ :created_at.desc ])
unless params[:format] == 'json'
haml :list
else
content_type :json, 'charset' => 'utf-8'
@users.to_json
end
end
#Badge
get '/:slug/badge' do
@user = User.get(params[:slug])
haml :badge, :layout => false
end
#Update
get '/:slug/edit' do
@user = User.get(params[:slug])
haml :edit
end
post '/:slug/edit' do
@user = User.get(params[:slug])
if @user.update(params[:user])
redirect "/#{@user.username}/edit"
else
redirect "/#{@user.username}"
end
end
#Exists
get '/:slug/exists' do
@user = User.get(params[:slug])
if @user.nil?
{:slug => -1}.to_json
else
@user.to_json(:only => [:slug])
end
end
#Delete
post '/:slug/delete' do
@user = User.get(params[:slug])
if @user.destroy
redirect "/list"
else
redirect "/#{@user.username}"
end
end
#Show single
get '/:slug' do
if @user = User.get(params[:slug])
unless params[:format] == 'json'
haml :show
else
content_type :json, 'charset' => 'utf-8'
@user.to_json
end
else
halt 404
end
end
# === Tags ===============================
get '/tags/all' do
@tags = Tag.all
content_type :json
@tags.to_json(:only => [:name])
end
post '/tags/new' do
@tags = params[:tags]
@tags.each do |t|
@tag = Tag.first_or_create(:name => t)
end
redirect '/'
end
get '/tags/:t' do
@users = User.all(:interests => params[:t])
haml :tag
end
# === Locations ===========================
get '/locations/all' do
@locations = [{ :slug => 'mexico-city', :name => 'Mexico City'},
{ :slug => 'sv', :name => ' Silicon Valley (original)'},
{ :slug => 'guanajuato', :name => 'Guanajuato'},
{ :slug => 'guadalajara', :name => 'Guadalajara'},
{ :slug => 'villahermosa', :name => 'Villahermosa'},
{ :slug => 'dc', :name => 'Washington, DC'},
{ :slug => 'hermosillo', :name => 'Hermosillo'},
{ :slug => 'new-zealand', :name => 'New Zealand'},
{ :slug => 'vancouver', :name => 'Vancouver'},
{ :slug => 'austin', :name => 'Austin'},
{ :slug => 'merida', :name => 'Merida'}]
content_type :json
@locations.sort_by { |n| n[:name] }.to_json
end
get '/locations/:l' do
@users = User.all(:location => params[:l], :order => [ :created_at.desc ])
unless params[:format] == 'json'
haml :list
else
content_type :json, 'charset' => 'utf-8'
@users.to_json
end
end
# === Styles ===============================
get '/stylesheets/*' do
content_type 'text/css'
sass '../styles/'.concat(params[:splat].join.chomp('.css')).to_sym
end