forked from andreip/tv-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tv-planner.rb
148 lines (131 loc) · 3.13 KB
/
tv-planner.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
136
137
138
139
140
141
142
143
144
145
146
147
148
require "bundler/setup"
Bundler.require :default
Dir.glob("models/*").each { |f| require File.expand_path(f) }
class Tv_planner < Sinatra::Base
enable :static
enable :sessions
configure do
set :database, "sqlite://development.sql"
end
get "/" do
if session[:token].nil?
erb :login
else
redirect "/dashboard"
end
end
get "/register" do
erb :register
end
post "/register" do
if User.check_unique(params[:email])
user = User.new(:email => params[:email], :password => User.encrypt(params[:password]))
if !user.save
@error_message = "A fost o eroare cand am salvat utilizatorul. Incearca din nou"
redirect_to "/"
else
session[:token] = "#{user.email}"
redirect "/dashboard"
end
else
"User email is already in use"
end
end
post "/sessions/create" do
user = User.where(:email => params[:email]).first;
if user.nil?
erb :login
elsif user.has_password?(params[:password])
puts "user ok password"
session[:token]= "#{user.email}"
redirect "/dashboard"
else
puts "user wrong pass"
erb :login
end
end
get "/remove-subscribtion" do
if session[:token].nil?
erb :login
else
usr = User.where(:email => session[:token]).first;
rec = Series_users_link.where(:user_id => usr.id,
:serie_id => params[:id])
Series_users_link.delete(rec)
redirect "/dashboard"
end
end
get "/dashboard" do
if session[:token].nil?
erb :login
else
@user = User.where(:email => session[:token]).first;
@my_series = @user.get_subscribed_series()
puts @my_series.inspect
erb :index
end
end
get "/mark_seen" do
if session[:token].nil?
erb :login
else
message = Message.where(:id => params[:id]).first
message.read = true
message.save()
redirect "/reminders"
end
end
get "/reminders" do
if session[:token].nil?
erb :login
else
@user = User.where(:email => session[:token]).first;
@alerts = @user.get_current_alerts()
erb :reminders
end
end
get "/subscribe" do
if session[:token].nil?
erb :login
else
user = User.where(:email => session[:token]).first;
ret = Series_users_link.where(:user_id => user.id,
:serie_id => params[:id])
if ret.first.nil? === false
redirect "/all-series"
end
user.subscribe_to_serie_by_id( params[:id] )
user.save()
redirect "/all-series"
end
end
get "/all-series" do
if session[:token].nil?
erb :login
else
@user = User.where(:email => session[:token]).first;
@all_series = Serie.all()
erb :all_series
end
end
get "/user" do
if session[:token].nil?
erb :login
else
@user= User.where(:email => session[:token]).first
erb :user
end
end
not_found do
status 404
erb :not_found
end
get "/logout" do
if session[:token].nil?
erb :login
else
session[:token] = nil
redirect "/"
end
end
end