Added product fetching and pages

This commit is contained in:
Thastertyn 2024-03-10 17:11:26 +01:00
parent 78aa03b2fb
commit 2694701337
2 changed files with 39 additions and 0 deletions

View File

@ -4,6 +4,17 @@ from app.api import bp_product
from app.services.product_service import ProductService
@bp_product.route('/get', methods=['GET'])
def get_products():
page = request.args.get('page', default=0, type=int)
if page < 0:
return abort(400)
result, status_code = ProductService.get_products(page)
return result, status_code
@bp_product.route('/<int:id>', methods=['GET'])
def get_product_info(id: int):
fields = ['name', 'price', 'image', 'image_name', 'seller']

View File

@ -7,6 +7,34 @@ from app.extensions import db_connection
class ProductService:
@staticmethod
def get_products(page: int):
try:
with db_connection.cursor(dictionary=True) as cursor:
offset = 10 * page
cursor.execute("select product.id, user.displayname as seller, product.name, product.price_pc from product inner join user on user.id = product.seller_id order by product.id limit 10 offset %s", (offset,))
results = cursor.fetchall()
if len(results) < 1:
return {"Failed": "Failed to fetch products. You've probably selected too far with pages"}, 400
result_obj = []
for row in results:
mid_result = {
"id": row['id'],
"seller": row['seller'],
"name": row['name'],
"price": row['price_pc']
}
result_obj.append(mid_result)
return result_obj, 200
except Error as e:
return {"Failed": f"Failed to load cart. Reason: {e}"}, 400
@staticmethod
def get_name(product_id: int):
db_cursor.execute(f"select name from product where product.id = {product_id}")