Update for product fetching
This commit is contained in:
parent
caa8b76c88
commit
fb4f91a1d6
@ -20,10 +20,14 @@ def get_product_info(id: int):
|
|||||||
fields = ['name', 'price', 'image', 'image_name', 'seller']
|
fields = ['name', 'price', 'image', 'image_name', 'seller']
|
||||||
|
|
||||||
fields_param = request.args.get('fields')
|
fields_param = request.args.get('fields')
|
||||||
requested_fields = []
|
|
||||||
|
|
||||||
return abort(501)
|
fields_param_list = fields_param.split(',') if fields_param else fields
|
||||||
|
|
||||||
|
common_fields = list(set(fields) & set(fields_param_list))
|
||||||
|
|
||||||
|
result, status_code = ProductService.get_product_info(common_fields, id)
|
||||||
|
|
||||||
|
return result, status_code
|
||||||
|
|
||||||
@bp_product.route('/create', methods=['POST'])
|
@bp_product.route('/create', methods=['POST'])
|
||||||
def create_product_listing():
|
def create_product_listing():
|
||||||
|
@ -33,73 +33,47 @@ class ProductService:
|
|||||||
return result_obj, 200
|
return result_obj, 200
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to fetch products. Reason: {e}"}, 400
|
return {"Failed": f"Failed to fetch products. Error: {e}"}, 500
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_name(product_id: int):
|
def get_product_info(fields: list[str], product_id: int):
|
||||||
db_cursor.execute(f"select name from product where product.id = {product_id}")
|
try:
|
||||||
|
with db_connection.cursor(dictionary=True) as cursor:
|
||||||
if db_cursor.rowcount != 1:
|
fields_to_sql = {
|
||||||
return abort(404)
|
"name": "product.name",
|
||||||
|
"price": "product.price_pc as price",
|
||||||
result = db_cursor.fetchone()
|
"image": "product.image",
|
||||||
return result['name']
|
"image_name": "product.image_name",
|
||||||
|
"seller": "user.displayname as seller"
|
||||||
@staticmethod
|
|
||||||
def get_manufacturer(product_id: int):
|
|
||||||
db_cursor.execute(f"select user.displayname as seller from product inner join user on product.seller_id = user.id where product.id = {product_id}")
|
|
||||||
|
|
||||||
if db_cursor.rowcount != 1:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
result = db_cursor.fetchone()
|
|
||||||
return result['seller']
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_price(product_id: int):
|
|
||||||
db_cursor.execute(f"select price_pc from product where product.id = {product_id}")
|
|
||||||
|
|
||||||
if db_cursor.rowcount != 1:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
result = db_cursor.fetchone()
|
|
||||||
return result['price_pc']
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_image(product_id: int):
|
|
||||||
db_cursor.execute(f"select image from product where product.id = {product_id}")
|
|
||||||
|
|
||||||
if db_cursor.rowcount != 1:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
result = db_cursor.fetchone()
|
|
||||||
return base64.b64encode(result['image']).decode('utf-8')
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_image_name(product_id: int):
|
|
||||||
db_cursor.execute(f"select image_name from product where product.id = {product_id}")
|
|
||||||
|
|
||||||
if db_cursor.rowcount != 1:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
result = db_cursor.fetchone()
|
|
||||||
return result['image_name']
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_all_info(product_id: int):
|
|
||||||
db_cursor.execute(f"select name, user.displayname as seller, price_pc, image_name, image from product inner join user on product.seller_id = user.id where product.id = {product_id}")
|
|
||||||
|
|
||||||
if db_cursor.rowcount != 1:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
result = db_cursor.fetchone()
|
|
||||||
return {
|
|
||||||
"name": result['name'],
|
|
||||||
"seller": result['seller'],
|
|
||||||
"price": result['price_pc'],
|
|
||||||
"image_name": result['image_name'],
|
|
||||||
"image": base64.b64encode(result['image']).decode('utf-8')
|
|
||||||
}
|
}
|
||||||
|
field_sql = []
|
||||||
|
|
||||||
|
for field in fields:
|
||||||
|
field_sql.append(fields_to_sql[field])
|
||||||
|
|
||||||
|
select_params = ", ".join(field_sql)
|
||||||
|
|
||||||
|
if "seller" in fields:
|
||||||
|
cursor.execute(f"select {select_params} from product inner join user on user.id = product.seller_id where product.id = %s", (product_id,))
|
||||||
|
else:
|
||||||
|
cursor.execute(f"select {select_params} from product where id = %s", (product_id,))
|
||||||
|
|
||||||
|
result = cursor.fetchone()
|
||||||
|
|
||||||
|
if cursor.rowcount != 1:
|
||||||
|
return {"Failed": "Failed to fetch product. Product likely doesn't exist"}, 400
|
||||||
|
|
||||||
|
result_obj = {}
|
||||||
|
|
||||||
|
for field in fields:
|
||||||
|
if field == "image": # Encode image into base64
|
||||||
|
result_obj[field] = base64.b64encode(result[field]).decode('utf-8')
|
||||||
|
else:
|
||||||
|
result_obj[field] = result[field]
|
||||||
|
|
||||||
|
return result_obj, 200
|
||||||
|
except Error as e:
|
||||||
|
return {"Failed": f"Failed to fetch product info. Error: {e}"}, 500
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_listing(name: str, seller_id: str, price: float, image_name: str, image):
|
def create_listing(name: str, seller_id: str, price: float, image_name: str, image):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user