-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
72 lines (59 loc) · 1.28 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
$LOAD_PATH.unshift(File.expand_path('.'))
require 'sinatra'
require 'sinatra/activerecord'
require './models/note'
require './models/user'
# This loads environment variables from the .env file
require 'dotenv'
Dotenv.load
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
get '/' do
erb :login
end
post '/users' do
User.create(username: params["username"], password: params["pwd"])
redirect '/notes'
end
get '/notes' do
@notes = Note.all
@title = 'All notes | Recall'
erb :index
end
post '/notes' do
n = Note.new
n.content = params[:content]
n.created_at = Time.now
n.updated_at = Time.now
n.save
redirect '/notes'
end
get '/:id' do
@note = Note.find(params[:id])
@title = 'Edit note ##{params[:id]}'
erb :edit
end
put '/:id' do
n = Note.find(params[:id])
n.content = params[:content]
n.complete = params[:complete] ? 1 : 0
n.updated_at = Time.now
n.save
redirect '/notes'
end
get '/:id/delete' do
@note = Note.find(params[:id])
@title = 'Delete note ##{params[:id]}'
erb :delete
end
delete '/:id' do
n = Note.find(params[:id])
n.destroy
redirect '/notes'
end
get '/:id/complete' do
n = Note.find(params[:id])
n.complete = n.complete ? 0 : 1
n.updated_at = Time.now
n.save
redirect '/notes'
end