-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtodo.py
47 lines (34 loc) · 1.44 KB
/
todo.py
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
from factory.validation import Validator
from factory.database import Database
class Todo(object):
def __init__(self):
self.validator = Validator()
self.db = Database()
self.collection_name = 'todos' # collection name
self.fields = {
"title": "string",
"body": "string",
"created": "datetime",
"updated": "datetime",
}
self.create_required_fields = ["title", "body"]
# Fields optional for CREATE
self.create_optional_fields = []
# Fields required for UPDATE
self.update_required_fields = ["title", "body"]
# Fields optional for UPDATE
self.update_optional_fields = []
def create(self, todo):
# Validator will throw error if invalid
self.validator.validate(todo, self.fields, self.create_required_fields, self.create_optional_fields)
res = self.db.insert(todo, self.collection_name)
return "Inserted Id " + res
def find(self, todo): # find all
return self.db.find(todo, self.collection_name)
def find_by_id(self, id):
return self.db.find_by_id(id, self.collection_name)
def update(self, id, todo):
self.validator.validate(todo, self.fields, self.update_required_fields, self.update_optional_fields)
return self.db.update(id, todo,self.collection_name)
def delete(self, id):
return self.db.delete(id, self.collection_name)