-
Notifications
You must be signed in to change notification settings - Fork 0
/
recall.rb
175 lines (151 loc) · 3.25 KB
/
recall.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'sinatra'
require 'data_mapper'
require 'time'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'sqlite3'
require 'cgi'
require 'digest/md5'
SITE_TITLE = "Filter"
SITE_DESCRIPTION = "Twitt your Message"
enable :sessions
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/recall.db")
class User
include DataMapper::Resource
property :username, Text, :key =>true
property :password, Text
has n, :note
end
class Note
include DataMapper::Resource
belongs_to :user
property :id, Serial
property :content, Text, :required =>true
property :created_at, DateTime
end
DataMapper.auto_upgrade!
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
#
# Application
#
get '/' do
@title = 'All Notes'
erb :login
end
post '/' do
user = User.get params[:login]
user_pwd = params[:password]
user_pwden = Digest::MD5.hexdigest(Digest::MD5.hexdigest(user_pwd) + "salt")
if user.nil?
redirect '/regist'
end
if user.password.to_s() .to_s().eql?(user_pwden.to_s())
session[:id] = user.username
redirect '/login'
else
redirect '/regist'
end
end
get '/login' do
sess = session[:id]
getuser = User.get sess
puts getuser.username
@notes = Note.all(:user => {:username => getuser.username}, :order => [:created_at.desc])
puts @notes
@title = 'All Notes'
erb :notes
end
post '/login' do
n = Note.new
sess = session[:id]
getuser = User.get sess
n.user_username = getuser.username
n.attributes = {
:content => params[:content],
:created_at => Time.now
}
if n.save
redirect '/login', :notice => 'good'
else
n.errors.each do |e|
puts e
end
redirect '/login', :error => 'failed'
end
end
get '/regist' do
@title = 'REGIST'
erb:regist
end
post '/regist' do
n = User.new
user_pwden = Digest::MD5.hexdigest(Digest::MD5.hexdigest(params[:password]) + "salt")
n.attributes = {
:username => params[:login],
:password => user_pwden
}
if n.save
redirect '/'
else
redirect '/regist', :error => 'Failed to regist.'
end
end
get '/rss.xml' do
builder :rss
end
get '/filter/user/:username' do
getuser = User.get params[:username]
@notes = Note.all(:user => {:username => getuser.username}, :order => [:created_at.desc])
erb :note_all
end
put '/:userid' do
n = User.get params[:useid]
unless n
redirect '/', :error => "Can't find that note."
end
n.attributes = {
:content => params[:content],
:complete => params[:complete] ? 1 : 0,
:updated_at => Time.now
}
if n.save
redirect '/', :notice => 'Note updated successfully.'
else
redirect '/', :error => 'Error updating note.'
end
end
get '/:id/delete' do
@note = User.get params[:userid]
@title = "Confirm deletion of note ##{params[:id]}"
if @note
erb :delete
else
redirect '/', :error => "Can't find that note."
end
end
delete '/:id' do
n = User.get params[:id]
if n.destroy
redirect '/', :notice => 'Note deleted successfully.'
else
redirect '/', :error => 'Error deleting note.'
end
end
get '/:id/complete' do
n = User.get params[:id]
unless n
redirect '/', :error => "Can't find that note."
end
n.attributes = {
:complete => n.complete ? 0 : 1, # flip it
:updated_at => Time.now
}
if n.save
redirect '/', :notice => 'Note marked as complete.'
else
redirect '/', :error => 'Error marking note as complete.'
end
end