-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
77 lines (64 loc) · 1.67 KB
/
app.rb
File metadata and controls
77 lines (64 loc) · 1.67 KB
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
#this is sinatra and sequel demo
#require 'sinatra/reloader' not to do this, why?
#require 'sinatra/json'
require 'sinatra/reloader'
require './models/article.rb'
#class App < Sinatra::Base
#set :views, File.dirname(__FILE__) + '/../views'
#set :views,File.dirname(__FILE__) + '/views'
#this is first create table,
#DB.create_table :articles do
#primary_key :id
#String :title
#String :content
# Time :date
#end
# module Validations
# def valid_id?(id)
# id && id.to_s =~ /^\d+$/
# end
# end
#articles = DB[:articles]
# enable :sessions
before '/posts' do
redirect '/posts/new' unless Article.count != 0
end
get '/posts/new', :provides => 'html' do
erb :new
end
post '/posts' do
time = Time.new
Article.insert(:title => params[:title],:content => params[:content],
:date => time.strftime("%Y-%m-%d %H:%M:%S")
)
status 201
end
get '/posts' do
@articles = Article.all
erb :index
end
get '/posts/update' do
@article = Article[:id => params[:id].to_i]
erb :update
end
put '/posts/:id' do
Article.where('id = ?',params[:id]).update(:title => params[:title],:content => params[:content])
status 200
end
delete '/posts/:id' do
Article.where('id = ?', params[:id]).delete
status 204
end
get '/api/:id' do
content_type :json
@article = Article[:id => params[:id].to_i]
tmp = {:id => @article.id,:title => @article.title,:content => @article.content,:date => @article.date}
json :article => tmp.to_json
end
not_found do
'this is nothing for you'
end
error 400..510 do
'some error for you'
end
#end