-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovieDatabase.dart
118 lines (100 loc) · 2.46 KB
/
movieDatabase.dart
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import 'dart:io';
import 'package:database/database.dart';
import 'package:database/filter.dart';
main() async {
// test commands for the demo
var database = MemoryDatabaseAdapter().database();
var movies = database.collection('movies');
await movies.insert(data: {
'name': 'Tron',
'watched': false
});
await movies.insert(data: {
'name': 'Hackers',
'watched': true
});
await movies.insert(data: {
'name': 'The Matrix',
'watched': true
});
removeWatched(movies);
list(movies);
}
start(collection) async {
while(true) {
print('Commands:');
print('1: Add Movie');
print('2: Watch Movie');
print('3: List Movi9es');
print('4: Remove Watched');
print('5: End');
var command = stdin.readLineSync();
if (command == '5') {
break;
}
if (command == '1') {
await add(collection);
} else if (command == '2') {
await watch(collection);
} else if (command == '3') {
await list(collection);
} else if (command == '4') {
await removeWatched(collection);
}
}
}
add(collection) async {
print('Name of Movie?');
var name = stdin.readLineSync();
await collection.insert(data: {
'name': name,
'watched': false
});
}
watch(collection) async {
print('Name of Movie?');
var name = stdin.readLineSync();
var query = Query(
filter: MapFilter({
'name': ValueFilter(name)
})
);
var result = await collection.search(query: query);
var documents = result.snapshots;
for (var i = 0; i < documents.length; i++) {
var data = documents[i].data;
var document = documents[i].document;
await document.update(data: {
'name': data['name'],
'watched': true
});
}
}
list(collection) async {
var query = Query(
sorter: PropertySorter('name', isDescending: false)
);
var result = await collection.search(query: query);
var documents = result.snapshots;
for (var i = 0; i < documents.length; i++) {
var data = documents[i].data;
if (data['watched'] = true) {
print("[X] ${data['name']}");
} else {
print("[ ] ${data['name']}");
}
}
}
removeWatched(collection) async {
var query = Query(
filter: MapFilter({
'watched': ValueFilter(true)
})
);
var result = await collection.search(query: query);
var documents = result.snapshots;
for (var i = 0; i < documents.length; i++) {
var document = documents[i].document;
await document.delete();
}
}