-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
135 lines (98 loc) · 2.48 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
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/flash'
require 'omniauth-github'
require_relative 'config/application'
Dir['app/**/*.rb'].each { |file| require_relative file }
helpers do
def current_user
user_id = session[:user_id]
@current_user ||= User.find(user_id) if user_id.present?
end
def signed_in?
current_user.present?
end
end
def set_current_user(user)
session[:user_id] = user.id
end
def authenticate!
unless signed_in?
flash[:notice] = 'You need to sign in if you want to do that!'
redirect '/'
end
end
get '/' do
@Meetups = Meetup.all.order(:name)
erb :index
end
post '/' do
authenticate!
@name = params['name'].capitalize
@description = params['description']
@location = params['location']
begin
m = Meetup.new(name: @name, description: @description,location: @location)
if m.save!
flash[:notice] = 'You have created a new meetup! In space!'
redirect '/'
end
rescue
flash[:notice] = 'That meetup already exists!'
end
redirect '/'
end
get '/meetup/:id' do
@id = Meetup.select("id")
@id = params[:id]
@meetup = Meetup.find(@id)
@attendees = @meetup.users
@comments = Comment.where(:meetup_id => @id)
erb :'meetup/id'
end
post '/meetup/:id' do
authenticate!
@id=params[:id]
@user = current_user
@meetup = Meetup.find(@id)
@attendees = @meetup.users
begin
Attendee.create(user_id: @user.id, meetup_id: @id)
flash[:notice] = 'You are signed up for the meetup! In space!'
redirect "/meetup/#{@id}"
rescue
flash[:notice] = 'You have already signed up for the meetup! In space!'
end
redirect "/meetup/#{@id}"
end
post '/leave/:id' do
authenticate!
@id=params[:id]
@user = current_user
Attendee.destroy_all(:user_id => @user.id, :meetup_id => @id)
flash[:notice] = 'You have left the meetup! In space!'
redirect "/meetup/#{@id}"
end
post '/comments/:id' do
@comment = params[:comment]
@id = params[:id]
@user = current_user
@user_id = @user.id
Comment.create(:user_id => @user_id, :meetup_id => @id, :comment => @comment)
redirect "/meetup/#{@id}"
end
get '/auth/github/callback' do
auth = env['omniauth.auth']
user = User.find_or_create_from_omniauth(auth)
set_current_user(user)
flash[:notice] = "You're now signed in as #{user.username}!"
redirect '/'
end
get '/sign_out' do
session[:user_id] = nil
flash[:notice] = "You have been signed out."
redirect '/'
end
get '/example_protected_page' do
authenticate!
end