-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpersistence.py
46 lines (35 loc) · 1.4 KB
/
persistence.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
from abc import ABC, abstractmethod
from database import DatabaseManager
class PersistenceLayer(ABC):
@abstractmethod
def create(self, data):
raise NotImplementedError(
'Persistence layers must implement a create method')
@abstractmethod
def list(self, order_by=None):
raise NotImplementedError(
'Persistence layers must implement a list method')
@abstractmethod
def delete(self, bookmark_id):
raise NotImplementedError(
'Persistence layers must implement a delete method')
class BookmarkDatabase(PersistenceLayer):
def __init__(self):
self.table_name = 'bookmarks'
self.db = DatabaseManager('bookmarks.db')
self.db.create_table(self.table_name, {
'id': 'integer primary key autoincrement',
'title': 'text not null',
'url': 'text not null',
'notes': 'text',
'date_added': 'text not null'
})
def create(self, bookmark_data):
self.db.add(self.table_name, bookmark_data)
def list(self, order_by=None):
return self.db.select(self.table_name, order_by=order_by).fetchall()
def edit(self, bookmark_data):
bookmark_id = bookmark_data.pop('id')
self.db.update(self.table_name, {'id': bookmark_id}, bookmark_data)
def delete(self, bookmark_id):
self.db.delete(self.table_name, {'id': bookmark_id})