-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
93 lines (77 loc) · 1.25 KB
/
main.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
require 'sinatra'
require 'sinatra/reloader' if development?
require 'json'
require 'rubygems'
require './song'
helpers do
def format_date(date)
if date && date.respond_to?(:strftime)
date.strftime("%d/%m/%Y")
else
date
end
end
end
get '/' do
erb :home
end
get '/about' do
@title = "All About This Website"
erb :about
end
get '/contact' do
@title = "Contact"
erb :contact
end
get '/songs' do
@songs = Song.all
@title = "Songs"
erb :songs
end
get '/songs/new' do
@song = Song.new
erb :new
end
get '/songs/:id' do
@song = Song.get(params[:id])
if @song
erb :show_song
else
status 404
erb :not_found
end
end
get '/songs/:id/edit' do
@song = Song.get(params[:id])
if @song
erb :edit_song
else
status 404
erb :not_found
end
end
put '/songs/:id' do
song = Song.get(params[:id])
if song.update(params[:song])
redirect to("/songs/#{song.id}")
else
@song = song
erb :edit_song
end
end
post '/songs' do
song = Song.create(params[:song])
if song.save
redirect to("/songs/#{song.id}")
else
@song = song
erb :new
end
end
delete '/songs/:id' do
Song.get(params[:id]).destroy #motherfucker
redirect to("/songs")
end
not_found do
erb :not_found
end