-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rb
46 lines (42 loc) · 1.08 KB
/
index.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
require 'sinatra'
require 'json'
require 'data_mapper'
get '/:article_id' do
@article_id = params['article_id']
star = Star.first( article_id: @article_id )
@count = star ? star.count : 0
erb @article_id.to_sym
end
get '/' do
@article_id = '20150618231030'
star = Star.first( article_id: @article_id )
@count = star ? star.count : 0
erb @article_id.to_sym
end
post '/', provides: :json do
article_id = params['article_id']
star = Star.first( article_id: article_id )
if session[article_id]
session[article_id] = session[article_id].to_i + 1
else
if star
session[article_id] = star.count + 1
else
star = Star.create( article_id: article_id, count: 1 )
session[article_id] = 1
end
end
star.update( count: session[article_id].to_i )
status 200
content_type :json
{ count: session[article_id].to_i }.to_json
end
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/star.db")
class Star
include DataMapper::Resource
property :id, Serial
property :article_id, String
property :count, Integer
end
DataMapper.finalize
Star.auto_upgrade!