64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
from .database import DatabaseController
|
|
from models.category import Category
|
|
|
|
|
|
class CategoryController(DatabaseController):
|
|
FIELDS = ['id', 'name']
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def create(self, category: Category):
|
|
params = [
|
|
category.name,
|
|
]
|
|
|
|
self._conn.execute(
|
|
"INSERT INTO Categories (name) VALUES (?)",
|
|
params
|
|
)
|
|
self._conn.commit()
|
|
|
|
def read(self, id: int = 0) -> Category | None:
|
|
params = [
|
|
id
|
|
]
|
|
|
|
cursor = self._conn.execute(
|
|
"SELECT * FROM Categories WHERE id = ?",
|
|
params
|
|
)
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
return None
|
|
|
|
params = dict(zip(self.FIELDS, row))
|
|
obj = self.new_instance(Category, params)
|
|
|
|
return obj
|
|
|
|
def read_all(self) -> list[Category] | None:
|
|
cursor = self._conn.execute(
|
|
"SELECT * FROM Categories",
|
|
)
|
|
rows = cursor.fetchall()
|
|
|
|
if rows is None:
|
|
return None
|
|
|
|
categories = list()
|
|
|
|
for category in rows:
|
|
params = dict(zip(self.FIELDS, category))
|
|
obj = self.new_instance(Category, params)
|
|
categories.append(obj)
|
|
|
|
return categories
|
|
|
|
def update(self):
|
|
print("Doing work")
|
|
|
|
def delete(self):
|
|
print("Doing work")
|