This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
126 lines (110 loc) · 2.7 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
require 'sinatra'
require 'sinatra/flash'
require 'data_mapper'
require 'haml'
require 'redcarpet'
require 'builder'
SITE_TITLE = "Recall"
SITE_DESCRIPTION = "Because you always forget to remember"
configure do
enable :sessions
set :session_secret, "xXtv6ReGcMRmFSgXGiHXqnrpOscpi8WaYXcer6oFYaK2PbMPBvriC7Lu0YrFsJA"
DataMapper.setup(:default, ENV["DATABASE_URL"] || "sqlite3:///#{Dir.pwd}/development.sqlite3")
end
class Note
include DataMapper::Resource
property :id, Serial
property :content, Text, required: true
property :complete, Boolean, required: true, default: false
property :created_at, DateTime
property :updated_at, DateTime
end
DataMapper.finalize.auto_upgrade!
helpers do
def h(input)
Rack::Utils.escape_html(input)
end
end
set(:method) do |method|
method = method.to_s.upcase
condition { request.request_method == method }
end
before method: :post do
if Note.all.count >= 5
redirect "/", flash[:error] = "You have 5 notes already. Delete one first."
end
end
get "/" do
@notes = Note.all order: :id.desc
@title = "All Notes"
if @notes.empty?
flash[:error] = "No notes found. Add your first one below."
end
haml :home
end
get "/rss.xml" do
@notes = Note.all order: :id.desc
builder :rss
end
post "/" do
note = Note.new
note.content = markdown params[:content]
note.created_at = Time.now
note.updated_at = Time.now
if note.save
redirect "/", flash[:notice] = "Note created successfully"
else
redirect "/", flash[:error] = "Failed to save note"
end
end
get "/:id" do
@note = Note.get params[:id]
@title = "Edit note ##{params[:id]}"
@checked_value = @note.complete ? true : false
if @note
haml :edit
else
redirect "/", flash[:error] = "Can't find that note"
end
end
put "/:id" do
note = Note.get params[:id]
note.content = params[:content]
note.complete = params[:complete] ? 1 : 0
note.updated_at = Time.now
if note.save
redirect "/", flash[:notice] = "Note updated successfully"
else
redirect "/", flash[:error] = "Error updating note"
end
end
get "/:id/delete" do
@note = Note.get params[:id]
@title = "Confirm deletion of note ##{params[:id]}"
if @note
haml :delete
else
redirect "/", flash[:error] = "Can't find that note"
end
end
delete "/:id" do
note = Note.get params[:id]
if note.destroy
redirect "/", flash[:notice] = "Note deleted successfully"
else
redirect "/", flash[:error] = "Error deleting note"
end
end
get "/:id/complete" do
note = Note.get params[:id]
unless note
redirect "/", flash[:error] = "Can't find that note"
end
note.complete = params[:complete] ? 0 : 1
note.updated_at = Time.now
if note.save
redirect "/", flash[:notice] = "Note marked as complete"
else
redirect "/", flash[:error] = "Error completing note"
end
end