-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·119 lines (93 loc) · 2.45 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
require 'sinatra'
require 'sinatra/activerecord'
require 'rack-flash'
# require 'bundler/setup'
# require 'sinatra/base'
use Rack::Flash, :sweep => true
set :sessions => true
configure(:development){set :database, "sqlite3:test.sqlite3"}
require './models'
get '/' do
flash[:notice] = 'Welcome to the homepage'
erb :home
end
get '/user_area' do
erb :user_area
end
get '/posts' do
erb :posts
end
get '/new' do
@posts = Post.all
erb :new
end
post '/sign-up' do
@user = User.create(params[:user])
erb :user_area
end
#The following code *should* take the info entered in the form on home.erb and create a user record in the database that can be used to sign in/out (create a session).
#The following code should lookup user/password combos and redirect to posts.erb if successful.
post '/sign-in' do
#check that the user's password is correct
# params
# {user: {username: "zachfeldmna", password: "pass"}}
@user = User.where(username: params[:user][:username]).first
if @user.password == params[:user][:password]
session[:user_id] = @user.id
flash[:notice] = "You were logged in successfully."
redirect "/user_area"
erb :user_area
else
flash[:alert] = "There was a problem logging you in"
redirect "/error"
erb :home
end
end
#if so, sign them in
def current_user
if session[:user_id]
@current_user = User.find(session[:user_id])
else
flash[:alert] = "There was a problem logging you in"
redirect "/error"
erb :home
end
end
helpers do
def current_user
session[:user_id].nil? ? nil : User.find(session[:user_id])
end
end
get '/signout' do
session.clear
flash[:notice] = "You were logged out successfully."
end
post "/posts/new" do
@post = Post.new(params[:post])
if @post.save
flash[:notice] = "Your post was created."
redirect "/new"
erb :new
else
flash[:notice] = "There was an error in creating your post. Try again."
erb :user_area
end
end
# get '/new' do
# # get the latest 10 posts
# @posts = Post.all? { |e| }
# end
# post '/posts/follow' do
# @post = Post.new(title: params[:title], text: params[:text])
# post '/signout' do
# @current_user = User_id.destroy(params[:user_id])
# flash[:notice] = "You were logged out successfully."
# redirect "/home"
# end
post '/posts' do
@post = Post.new(params[:post])
erb :posts
end
# post '/posts/follow' do
# @post = Post.new(title: params[:title], text: params[:text])
# end