-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
127 lines (106 loc) · 2.79 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
require 'sinatra'
require 'sinatra/activerecord'
require './models'
require 'bundler/setup'
require 'sinatra/base'
require 'rack-flash'
require 'sinatra/asset_pipeline'
require './config/environments'
class MyApp < Sinatra::Base
register Sinatra::AssetPipeline
end
enable :sessions
use Rack::Flash, :sweep => true
set :sessions => true
configure(:development){set :database, "sqlite3:nomdder_app.sqlite3"}
helpers do
def current_user
session[:user_id].nil? ? nil : User.find(session[:user_id])
end
end
get '/' do
@title = "Welcome to Nomadder"
erb :sign_in, :layout => false
end
post '/signup' do
@user = User.create(params[:user])
flash[:notice] = 'New account created'
session[:user_id] = @user.id
redirect '/logged_in'
end
post '/signin' do
@user = User.where(params[:user]).first
if !@user
flash[:alert] = "Sorry, that user doesn't exist. Feel free to sign up."
redirect '/'
else
flash[:notice] = "You've successfully signed in."
session[:user_id] = @user.id
redirect '/logged_in'
end
end
get '/sign_out' do
session[:user_id] = nil
redirect '/'
end
get '/logged_in' do
@title = "Welcome to Nomadder"
@user = User.find(session[:user_id]) if session[:user_id]
@postsall = Post.all.order(date: :desc).limit(10)
erb :logged_in
end
get '/follow/:id' do
@relationships = Relationship.new(follower_id: current_user.id, followed_id: params[:id])
if @relationships.save
flash[:notice] = "You've successfully followed #{User.find(params[:id]).fname}."
else
flash[:alert] = "There was an error following that user."
end
redirect back
end
get '/profile' do
@title = "Profile Page"
@user = User.find(session[:user_id]) if session[:user_id]
@posts = current_user.posts.order(date: :desc)
erb :profile
end
post '/post' do
puts params.inspect
@post = Post.new(params[:post])
@post.date = DateTime.now
@post.user_id = current_user.id
@post.save
#@user = User.find(session[:user_id])
#session[:user_id] = @user.id
redirect '/logged_in'
end
post '/post_profile' do
puts params.inspect
@post = Post.new(params[:post])
@post.date = DateTime.now
@post.user_id = current_user.id
@post.save
redirect '/profile'
end
get '/account_setting' do
@title = "Account Settings"
@user = User.find(session[:user_id]) if session[:user_id]
erb :account_settings
end
get '/users/:id' do
@user = User.find(params[:id])
@posts = @user.posts
erb :profile
end
post '/user/update' do
@user = User.find(session[:user_id]) if session[:user_id]
@user.update(params[:user])
flash[:notice] = 'Account updated'
redirect '/account_setting'
end
post '/user/photo' do
@user = User.find(session[:user_id]) if session[:user_id]
@user.update(params[:user])
flash[:notice] = 'Account updated'
redirect '/account_setting'
end