Skip to content

Commit

Permalink
fix: purchase history
Browse files Browse the repository at this point in the history
  • Loading branch information
hopetambala committed Apr 10, 2020
1 parent 8a317fd commit c65e41b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def home():
api.add_resource(UserRegister, '/user/register')
api.add_resource(Inventory, '/product/<string:productId>')
api.add_resource(InventoryProductList, '/products')
api.add_resource(PurchaseHistory, '/history/<string:name>')
api.add_resource(PurchaseHistory, '/history/purchase/<string:user_id>')
api.add_resource(ScanHistory, '/history/scan/<string:user_id>')
api.add_resource(ScanHistoryRegister, '/history/scan')
api.add_resource(Shopping, '/shopping')
Expand Down
4 changes: 1 addition & 3 deletions db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
create_history_table ='{}{}{}{}{}{}'.format(
'CREATE TABLE IF NOT EXISTS',
' purchase_history(id INTEGER PRIMARY KEY,',
' product text, user_id INTEGER NOT NULL,',
' user_id INTEGER NOT NULL,',
' product_id INTEGER NOT NULL,',
' FOREIGN KEY (user_id) REFERENCES user(id),',
' FOREIGN KEY (product_id) REFERENCES inventory(id));'
Expand Down Expand Up @@ -79,8 +79,6 @@
query = "INSERT OR REPLACE INTO inventory VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(query, row)

cursor.execute('INSERT OR REPLACE INTO purchase_history VALUES(1, "tshirt", 1, 1);')

create_shipping_address_table = '{}{}{}{}{}'.format(
'CREATE TABLE IF NOT EXISTS',
' shipping_address(id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL,',
Expand Down
Binary file modified db/pineapplestore.db
Binary file not shown.
23 changes: 17 additions & 6 deletions endpoints/purchase_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@

class PurchaseHistory(Resource):

def get(self, name):
list_of_products = PurchaseHistoryModel.find_products_related_with_user_name(name)
def get(self, user_id):
# list_of_products = PurchaseHistoryModel.find_products_related_with_user_name(name)
list_of_products = PurchaseHistoryModel.find_history_product_by_userId(user_id)
if list_of_products:
return {
'product_history': [product.json() for product in list_of_products]
'purchase_history': [product.json() for product in list_of_products]
}, 200
else:
return {
'message': 'User and related prducts not found in database!'
'message': 'User and purchased products not found in database!'
}, 404

def post(self, name, product):
pass
def post(self, user_id):
parser = reqparse.RequestParser()
parser.add_argument('product_id',
type=str,
required=True,
help='This field is mandatory!')

data_payload = parser.parse_args()

PurchaseHistoryModel.add_purchase(user_id,
data_payload['product_id'])
return {'message': 'Purchase successfully added to database!'}, 201
29 changes: 18 additions & 11 deletions models/purchase_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ class PurchaseHistoryModel:

db_path = './db/pineapplestore.db'

def __init__(self, id, product, user_id, product_id):
def __init__(self, id, user_id, product_id):
self.id = id
self.product = product
self.user_id = user_id
self.product_id = product_id

@classmethod
def find_history_product_by_name(cls, name):
def find_history_product_by_userId(cls, userid):

history_products = list()

connection = sqlite3.connect(cls.db_path)
cursor = connection.cursor()
query = 'SELECT * FROM purchase_history WHERE product=?;'
resluts = cursor.execute(query, (name,))
query = 'SELECT * FROM purchase_history WHERE user_id=?;'
resluts = cursor.execute(query, (userid,))
rows = resluts.fetchall()
if rows:
for row in rows:
product = PurchaseHistoryModel(row[0], row[1], row[2], row[3])
product = PurchaseHistoryModel(row[0], row[1], row[2])
history_products.append(product)
connection.close()
return history_products
Expand All @@ -43,15 +42,23 @@ def find_products_related_with_user_name(cls, name):
rows = result.fetchall()
if rows:
for row in rows:
products = PurchaseHistoryModel(row[0], row[1], row[2], row[3])
products = PurchaseHistoryModel(row[0], row[1], row[2])
products_history_list.append(products)
connection.close()
return products_history_list

@classmethod
def add_purchase(self, user_id, product_id):
connection = sqlite3.connect('./db/pineapplestore.db')
cursor = connection.cursor()
query = 'INSERT INTO purchase_history VALUES(NULL, ?, ?);'
cursor.execute(query, (user_id, product_id,))
connection.commit()
connection.close()

def json(self):
return {
'user': self.user_id,
'product history id': self.id,
'product name': self.product,
'product store number': self.product_id
'purchase_id': self.id,
'user_id': self.user_id,
'product_id': self.product_id,
}

0 comments on commit c65e41b

Please sign in to comment.