-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.rb
107 lines (88 loc) · 2.39 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
require 'sinatra/base'
require 'sinatra/flash'
require 'uri'
require_relative './lib/bookmark'
require_relative './lib/comment'
require_relative './lib/tag'
require_relative './lib/bookmark_tag'
require_relative './lib/user'
require_relative './database_connection_setup'
class BookmarkManager < Sinatra::Base
enable :sessions, :method_override
register Sinatra::Flash
get '/' do
'Bookmark Manager'
end
get '/bookmarks' do
@user = User.find(id: session[:user_id])
@bookmarks = Bookmark.all
erb :'bookmarks/index'
end
get '/bookmarks/new' do
erb :'bookmarks/new'
end
post '/bookmarks' do
flash[:notice] = "Please submit a valid URL" unless Bookmark.create(url: params[:url], title: params[:title])
redirect '/bookmarks'
end
delete '/bookmarks/:id' do
Bookmark.delete(id: params[:id])
redirect '/bookmarks'
end
get '/bookmarks/:id/edit' do
@bookmark = Bookmark.find(id: params[:id])
erb :"bookmarks/edit"
end
patch '/bookmarks/:id' do
Bookmark.update(id: params[:id], title: params[:title], url: params[:url])
redirect('/bookmarks')
end
get '/bookmarks/:id/comments/new' do
@bookmark_id = params[:id]
erb :'comments/new'
end
post '/bookmarks/:id/comments' do
Comment.create(bookmark_id: params[:id], text: params[:comment])
redirect '/bookmarks'
end
get '/bookmarks/:id/tags/new' do
@bookmark_id = params[:id]
erb :'/tags/new'
end
post '/bookmarks/:id/tags' do
tag = Tag.create(content: params[:tag])
BookmarkTag.create(bookmark_id: params[:id], tag_id: tag.id)
redirect '/bookmarks'
end
get '/tags/:id/bookmarks' do
@tag = Tag.find(id: params['id'])
erb :'tags/index'
end
get '/users/new' do
erb :"users/new"
end
post '/users' do
user = User.create(email: params['email'], password: params['password'])
session[:user_id] = user.id
redirect '/bookmarks'
end
get '/sessions/new' do
erb :"sessions/new"
end
post '/sessions' do
user = User.authenticate(email: params[:email], password: params[:password])
if user
session[:user_id] = user.id
redirect('/bookmarks')
else
flash[:notice] = 'Please check your email or password.'
redirect('/sessions/new')
end
end
post '/sessions/destroy' do
session.clear
flash[:notice] = 'You have signed out.'
redirect('/bookmarks')
end
run! if app_file == $0
end