-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsong.rb
64 lines (54 loc) · 1.31 KB
/
song.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
require 'dm-core'
require 'dm-migrations'
class Song
include DataMapper::Resource
property :id, Serial
property :title, String
property :lyrics, Text
property :length, Integer
property :released_on, Date
def released_on=date
super Date.strptime(date, '%m/%d/%Y')
end
end
DataMapper.finalize
get '/songs' do
halt(401,'Not Authorized') unless session[:admin]
@songs = Song.all
slim :songs
# erb :songs
end
post '/songs' do
halt(401,'Not Authorized') unless session[:admin]
song = Song.create(params[:song])
redirect to("/songs/#{song.id}")
end
get '/songs/new' do
halt(401,'Not Authorized') unless session[:admin]
@song = Song.new
slim :new_song
# erb :new_song
end
get '/songs/:id' do
halt(401,'Not Authorized') unless session[:admin]
@song = Song.get(params[:id])
slim :show_song
# erb :show_song
end
get '/songs/:id/edit' do
halt(401,'Not Authorized') unless session[:admin]
@song = Song.get(params[:id])
slim :edit_song
# erb :edit_song
end
put '/songs/:id' do
halt(401,'Not Authorized') unless session[:admin]
song = Song.get(params[:id])
song.update(params[:song])
redirect to("/songs/#{song.id}")
end
delete '/songs/:id' do
halt(401,'Not Authorized') unless session[:admin]
Song.get(params[:id]).destroy
redirect to('/songs')
end