forked from s3lph/matemat
Reworked Product to allow a 0 argument constructor, because unittest apparently breaks stuff...
This commit is contained in:
parent
1439c3072c
commit
3794a570c8
2 changed files with 27 additions and 23 deletions
|
@ -254,7 +254,7 @@ class Database(object):
|
||||||
FROM products
|
FROM products
|
||||||
'''):
|
'''):
|
||||||
product_id, name, price_member, price_external = row
|
product_id, name, price_member, price_external = row
|
||||||
products.append(Product(product_id, name, price_member, price_external))
|
products.append(Product.from_database(product_id, name, price_member, price_external))
|
||||||
return products
|
return products
|
||||||
|
|
||||||
def create_product(self, name: str, price_member: int, price_non_member: int) -> Product:
|
def create_product(self, name: str, price_member: int, price_non_member: int) -> Product:
|
||||||
|
@ -273,9 +273,11 @@ class Database(object):
|
||||||
})
|
})
|
||||||
c.execute('SELECT last_insert_rowid()')
|
c.execute('SELECT last_insert_rowid()')
|
||||||
product_id = int(c.fetchone()[0])
|
product_id = int(c.fetchone()[0])
|
||||||
return Product(product_id, name, price_member, price_non_member)
|
return Product.from_database(product_id, name, price_member, price_non_member)
|
||||||
|
|
||||||
def change_product(self, product: Product):
|
def change_product(self, product: Product):
|
||||||
|
if product.id == -1:
|
||||||
|
raise ValueError('Invalid product ID')
|
||||||
with self.transaction() as c:
|
with self.transaction() as c:
|
||||||
c.execute('''
|
c.execute('''
|
||||||
UPDATE products
|
UPDATE products
|
||||||
|
@ -291,6 +293,8 @@ class Database(object):
|
||||||
})
|
})
|
||||||
|
|
||||||
def delete_product(self, product: Product):
|
def delete_product(self, product: Product):
|
||||||
|
if product.id == -1:
|
||||||
|
raise ValueError('Invalid product ID')
|
||||||
with self.transaction() as c:
|
with self.transaction() as c:
|
||||||
c.execute('''
|
c.execute('''
|
||||||
DELETE FROM products
|
DELETE FROM products
|
||||||
|
@ -298,6 +302,8 @@ class Database(object):
|
||||||
''', [product.id])
|
''', [product.id])
|
||||||
|
|
||||||
def increment_consumption(self, user: User, product: Product, count: int = 1):
|
def increment_consumption(self, user: User, product: Product, count: int = 1):
|
||||||
|
if product.id == -1:
|
||||||
|
raise ValueError('Invalid product ID')
|
||||||
with self.transaction() as c:
|
with self.transaction() as c:
|
||||||
c.execute('''
|
c.execute('''
|
||||||
SELECT count
|
SELECT count
|
||||||
|
@ -342,6 +348,8 @@ class Database(object):
|
||||||
})
|
})
|
||||||
|
|
||||||
def restock(self, product: Product, count: int):
|
def restock(self, product: Product, count: int):
|
||||||
|
if product.id == -1:
|
||||||
|
raise ValueError('Invalid product ID')
|
||||||
with self.transaction() as c:
|
with self.transaction() as c:
|
||||||
c.execute('''
|
c.execute('''
|
||||||
UPDATE products
|
UPDATE products
|
||||||
|
|
|
@ -1,28 +1,24 @@
|
||||||
|
|
||||||
from inspect import stack
|
|
||||||
|
|
||||||
|
|
||||||
class Product(object):
|
class Product(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self,
|
||||||
print(stack())
|
product_id: int = -1,
|
||||||
self._product_id: int = 0
|
name: str = '',
|
||||||
self._name: str = ''
|
price_member: int = 0,
|
||||||
self._price_member: int = 0
|
price_non_member: int = 0):
|
||||||
self._price_non_member: int = 0
|
self._product_id: int = product_id
|
||||||
raise NotImplementedError('This shoudt not be called!')
|
self._name: str = name
|
||||||
|
self._price_member: int = price_member
|
||||||
|
self._price_non_member: int = price_non_member
|
||||||
|
|
||||||
# def __init__(self,
|
@classmethod
|
||||||
# product_id: int,
|
def from_database(cls,
|
||||||
# name: str,
|
product_id: int,
|
||||||
# price_member: int,
|
name: str,
|
||||||
# price_non_member: int):
|
price_member: int,
|
||||||
# if product_id == -1:
|
price_non_member: int) -> 'Product':
|
||||||
# raise ValueError('Invalid product ID')
|
product = cls(product_id, name, price_member, price_non_member)
|
||||||
# self._product_id: int = product_id
|
return product
|
||||||
# self._name: str = name
|
|
||||||
# self._price_member: int = price_member
|
|
||||||
# self._price_non_member: int = price_non_member
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self) -> int:
|
def id(self) -> int:
|
||||||
|
|
Loading…
Reference in a new issue