-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
73 lines (58 loc) · 1.3 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
require 'sinatra'
require 'bcrypt'
require 'data_mapper'
enable :sessions, :logging
set :session_secret, 'ultra buttes'
set :root, File.dirname(__FILE__) << "/app/"
@env = ENV["RACK_ENV"] || "development"
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/#{@env}.db")
Dir["app/models/*.rb"].each {|file| require "#{Dir.pwd}/#{file}" }
DataMapper.finalize
get '/' do
@naps = Nap.all(:order => [ :id.desc ]) || {}
erb :welcome, layout: :main
end
get '/sloths' do
@sloths = User.all || {}
erb :sloths
end
get '/register' do
erb :register
end
post '/register' do
halt(400) unless params[:username] && params[:password]
unless User.name_taken?(params[:username])
User.create({
username: params[:username],
password_digest: params[:password]
})
else
halt(400)
end
redirect "/"
end
def logged_in?
session[:username]
end
post '/login' do
user = User.first(username: params[:username])
if user
password = BCrypt::Password.new(user.password_digest)
if password == params[:password]
session[:username] = params[:username]
end
end
redirect "/"
end
get '/logout' do
session[:username] = nil
redirect "/"
end
post '/nap' do
Nap.create({
body: params[:body],
author: session[:username],
posted: DateTime.now
})
redirect "/"
end