Skip to content

Commit

Permalink
fix: #2 & clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Abishevs committed Jan 12, 2024
1 parent a902f0d commit c5f146d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 53 deletions.
20 changes: 8 additions & 12 deletions src/tech_cache/commons/database_manager.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from typing import List
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import SQLAlchemyError
from tech_cache.models.item import Base, Item

# import sqlite3
from typing import List


class DatabaseManager:
"""Handles database connection and data persitance
Idea is that an instance of this class is passed to
Expand All @@ -21,19 +18,18 @@ def __init__(self, db_url="sqlite:///test.db"):
def get_session(self):
return self.Session()


def get_item(self, item_id:int):
with self.get_session() as session:
return session.query(Item).filter(Item.id == item_id).one_or_none()

def add_item(self, item: Item):
session = self.get_session()
try:
session.add(item)
session.commit()
except SQLAlchemyError as e:
session.rollback()
raise e
with self.get_session() as session:
try:
session.add(item)
session.commit()
except SQLAlchemyError as e:
session.rollback()
raise e

def update_item(self, item_id: int):

Expand Down
50 changes: 9 additions & 41 deletions src/tech_cache/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ class Item(Base):
created_at: Mapped[datetime] = mapped_column(DateTime, default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, default=func.now(), onupdate=func.now())

def __init__(self, name, category, quantity, specification):
def __init__(self,
name:str = "",
sku:str = "",
category:str = "",
quantity:int = 0,
specification:str = "",
):

self.name = name
self.sku = sku
self.category = category
self.quantity = quantity
self.specification = specification

# """Represents rows in tableview """
# def __init__(self, **kwargs):
# self.id:str = str(kwargs.get('id', uuid4())) # for in database data integrity
# self._name:str = kwargs.get('name', "")
# self.category:str = kwargs.get('category', "")
# self._quantity:int = kwargs.get('quantity', 0)
# self.sku:str = kwargs.get('sku', "") # TODO: Gen SKU
# self.specification:str = kwargs.get('specification', "")
# self.created_at:str = kwargs.get('created_at', datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# self.updated_at:str = kwargs.get('updated_at',datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

def __repr__(self) -> str:
return f"Item(name={self.name!r}, category={self.category!r}, quantity={self.quantity!r})"

Expand All @@ -59,32 +56,6 @@ def __getitem__(self, column_index):
else:
raise IndexError("Invalid column index")

# @property
# def name(self):
# """The name property."""
# return self._name
#
# @name.setter
# def name(self, value):
# if value == "":
# raise ValueError("Mandatory name field can't be empty")
# self._name = value
#
# @property
# def quantity(self):
# """The quantity property."""
# return self._quantity
#
# @quantity.setter
# def quantity(self, value:int):
# if not isinstance(value, int):
# raise ValueError("Quantity must be an integer")
#
# if value < 0:
# raise ValueError("Quantity cannot be negative")
#
# self._quantity = value

def get_fields(self) -> List[Any]:
"""Item fields to display"""
fields = []
Expand All @@ -94,6 +65,3 @@ def get_fields(self) -> List[Any]:
fields.append(self.quantity)
fields.append(self.specification)
return fields



0 comments on commit c5f146d

Please sign in to comment.