This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
89 lines (69 loc) · 1.43 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
require 'sinatra'
require 'sinatra/activerecord'
require './environments'
# Define the application with a name
class Blog < Sinatra::Application
end
class Article < ActiveRecord::Base
def number_of_characters
self.text.length
end
end
class Comment
def self.list
[
{comment: "Great site!", visitor: "Bob"},
{comment: "Wow. What a great blog", visitor: "Lisa"},
{comment: "Love the site :)", visitor: "Jeff"}
]
end
end
# Our first route
get '/' do
@articles = Article.order("created_at DESC")
erb :"articles/index"
end
get '/about' do
erb :"about/index"
end
get '/articles/new' do
@article = Article.new
erb :"articles/new"
end
post "/articles" do
@article = Article.new(params[:article])
if @article.save
redirect "/"
else
erb :"articles/new"
end
end
get "/articles/:id" do
@article = Article.find(params[:id])
erb :"articles/show"
end
get "/articles/:id/edit" do
@article = Article.find(params[:id])
erb :"articles/edit"
end
put "/articles/:id" do
@article = Article.find(params[:id])
if @article.update_attributes(params[:article])
redirect "/"
else
erb :"articles/edit"
end
end
delete "/articles/:id" do
@article = Article.find(params[:id]).destroy
redirect "/"
end
get '/guestbook' do
@comments = Comment.list
erb :"guestbook/index"
end
get '/api' do
content_type :json
@articles = Article.order("created_at DESC")
@articles.to_json
end