-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequel_persistence.rb
58 lines (45 loc) · 1.32 KB
/
sequel_persistence.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
require 'sequel'
DB = Sequel.connect('postgres://localhost/todos')
class SequelPersistence
def initialize(logger)
DB.logger = logger
end
def find_list(id)
all_lists.first(lists__id: id)
end
def all_lists
DB[:lists].left_join(:todos, list_id: :id).
select_all(:lists).
select_append do
[ count(todos__id).as(todos_count),
count(nullif(todos__completed, true)).as(todos_remaining_count) ]
end.
group(:lists__id).
order(:lists__name)
end
def create_new_list(list_name)
DB[:lists].insert(name: list_name)
end
def delete_list(id)
DB[:todos].where(list_id: id).delete
DB[:lists].where(id: id).delete
end
def update_list_name(id, new_name)
DB[:lists].where(id: id).update(name: new_name)
end
def create_new_todo(list_id, todo_name)
DB[:todos].insert(list_id: list_id, name: todo_name)
end
def delete_todo_from_list(list_id, todo_id)
DB[:todos].where(id: todo_id, list_id: list_id).delete
end
def update_todo_status(list_id, todo_id, new_status)
DB[:todos].where(list_id: list_id, id: todo_id).update(completed: new_status)
end
def mark_all_todos_as_completed(list_id)
DB[:todos].where(list_id: list_id).update(completed: true)
end
def find_todos_for_list(list_id)
DB[:todos].where(list_id: list_id)
end
end