forked from dantetam/retumblr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall-app.rb
64 lines (48 loc) · 1.21 KB
/
wall-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
require "sinatra" # Load the Sinatra web framework
require "data_mapper" # Load the DataMapper database library
require "./database_setup"
class Message
include DataMapper::Resource
property :id, Serial
property :title, Text
property :body, Text, required: true
property :created_at, DateTime, required: true
property :clicks, Integer, default: 0
property :bumps, Integer, default: 0
def body_length
self.body.length
end
end
DataMapper.finalize()
DataMapper.auto_upgrade!()
get("/") do
records = Message.all(order: :created_at.desc)
total = 0
click_count = Message.clicks
records.each do |wer|
total += wer.body_length
end
erb(:index, { locals: { messages: records, character_count: total, clicks: click_count}})
end
post("/delete-all") do
redirect("/")
end
post("/add_click") do
Message.clicks += 1
redirect("/")
end
post("/messages") do
message_title = params["title"]
message_body = params["body"]
message_time = DateTime.now
message = Message.create(
title: message_title,
body: message_body,
created_at: message_time
)
if message.saved?
redirect("/")
else
erb(:error)
end
end