-
Notifications
You must be signed in to change notification settings - Fork 9
/
todo.pl
executable file
·72 lines (60 loc) · 1.93 KB
/
todo.pl
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
#!/usr/bin/env swipl --quiet -O -t todo -f
% Usage: Run `examples/todo.pl` from the project root.
% Setup prolongo load paths and load the library.
:- [load].
:- use_module(mongo(mongo), []). % Empty import forces use of namespace.
todo :-
print_welcome,
setup_call_cleanup(
mongo:new_connection(Connection),
todo_run(Connection),
mongo:free_connection(Connection)).
print_welcome :-
format('--- Simple Todo ---~n'),
format('Terminate input with a period.~n~n').
todo_run(Connection) :-
mongo:get_database(Connection, 'prolongo_example_todo', Database),
mongo:get_collection(Database, 'items', Collection),
action(list, Collection).
action(list, Collection) :- !,
list_items(Collection),
new_action(Collection).
action(add, Collection) :- !,
add_item(Collection),
new_action(Collection).
action(delete, Collection) :- !,
delete_item(Collection),
new_action(Collection).
action(quit, _Collection) :- !,
format('Bye!~n').
action(_Unknown, Collection) :-
format('Unknown alternative.~n'),
new_action(Collection).
new_action(Collection) :-
format('~nEnter list/add/delete/quit: '),
read(Action),
action(Action, Collection).
list_items(Collection) :-
mongo:find_all(Collection, [], [], Docs),
print_items(Docs).
print_items(Docs) :-
format('Id~26|Label~45|Priority~n'),
print_items_aux(Docs).
print_items_aux([]).
print_items_aux([Doc|Docs]) :-
bson:doc_get(Doc, '_id', object_id(Id)),
bson:doc_get(Doc, label, Label),
bson:doc_get(Doc, priority, Priority),
format('~w~26|~w~45|~w~n', [Id,Label,Priority]),
print_items_aux(Docs).
add_item(Collection) :-
format('Label: '),
read(Label),
format('Priority: '),
read(Priority),
Doc = [label-Label,priority-Priority],
mongo:insert(Collection, Doc).
delete_item(Collection) :-
format('Id: '),
read(Id),
mongo:delete(Collection, ['_id'-object_id(Id)]).