-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
73 lines (62 loc) · 1.96 KB
/
server.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
require "sinatra"
require "sinatra/activerecord"
require "sinatra-websocket"
require "./lib/user"
require "./lib/board"
require "./lib/task"
class Server < Sinatra::Base
register Sinatra::ActiveRecordExtension
enable :sessions
set :sockets, Hash.new
get "/users" do
erb :users, :locals => { users: User.all }, :layout => :layout
end
get "/user/:id" do |id|
erb :user, locals: { user: User.find(id) }, :layout => :layout
end
get /\/|\/boards/ do
erb :boards, :locals => { boards: Board.all }, :layout => :layout
end
post "/board" do
Board.create(title: params["title"])
erb :boards, :locals => { boards: Board.all }, :layout => :layout
end
get "/board/:id/socket" do |board_id|
request.websocket do |ws|
ws.onopen do
if settings.sockets.key?(board_id)
settings.sockets[board_id] << ws
else
settings.sockets[board_id] = [ws]
end
end
ws.onmessage do |msg|
board_id, task_id, colname = msg.split("|")
board = Board.find(board_id)
task = Task.find(task_id)
previous_colname = task.colname
task.colname = colname
task.status = colname == "done" ? "done" : "pending"
task.save
settings.sockets[board_id].each { |ws| ws.send([task_id, previous_colname, colname].join("|")) }
end
ws.onclose do
settings.sockets[board_id].delete(ws)
end
end
end
get "/board/:id" do |id|
board = Board.find(id)
erb :board, :locals => { board: board, cols: board.as_cols, users: User.all }, :layout => :layout_board
end
post "/board/:id/task" do |id|
task = params.reject { |key, val| key == "id" }
Task.create(task)
board = Board.find(id)
erb :board, :locals => { board: board, cols: board.as_cols, users: User.all }, :layout => :layout_board
end
get "/board/:id/delete" do |id|
Board.delete(id)
erb :boards, :locals => { boards: Board.all }, :layout => :layout
end
end