swag-shop/app/services/product_service.py

112 lines
3.2 KiB
Python
Raw Normal View History

2024-03-05 16:01:26 +01:00
import base64
2024-03-08 14:05:22 +01:00
from flask import abort
from mysql.connector import Error
from app.extensions import db_connection
2024-03-05 16:01:26 +01:00
class ProductService:
2024-03-10 17:11:26 +01:00
@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
2024-03-05 16:01:26 +01:00
@staticmethod
def get_name(product_id: int):
2024-03-08 14:05:22 +01:00
db_cursor.execute(f"select name from product where product.id = {product_id}")
if db_cursor.rowcount != 1:
return abort(404)
result = db_cursor.fetchone()
return result['name']
2024-03-05 16:01:26 +01:00
@staticmethod
def get_manufacturer(product_id: int):
2024-03-08 14:05:22 +01:00
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']
2024-03-05 16:01:26 +01:00
@staticmethod
def get_price(product_id: int):
2024-03-08 14:05:22 +01:00
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']
2024-03-05 16:01:26 +01:00
@staticmethod
def get_image(product_id: int):
2024-03-08 14:05:22 +01:00
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')
2024-03-05 16:01:26 +01:00
@staticmethod
def get_image_name(product_id: int):
2024-03-08 14:05:22 +01:00
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']
2024-03-05 16:01:26 +01:00
@staticmethod
def get_all_info(product_id: int):
2024-03-08 14:05:22 +01:00
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)
2024-03-05 16:01:26 +01:00
2024-03-08 14:05:22 +01:00
result = db_cursor.fetchone()
2024-03-05 16:01:26 +01:00
return {
"name": result['name'],
"seller": result['seller'],
"price": result['price_pc'],
"image_name": result['image_name'],
"image": base64.b64encode(result['image']).decode('utf-8')
}
@staticmethod
2024-03-08 14:05:22 +01:00
def create_listing(name: str, seller_id: str, price: float, image_name: str, image):
try:
db_cursor.execute("insert into product(seller_id, name, price_pc, image, image_name) values (%s, %s, %s, %s, %s)", (seller_id, name, price, image, image_name))
db_connection.commit()
except Error as e:
return {"Failed": f"Failed to create product. {e}"}, 400
return {"Success", "Successfully created new product listing"}, 200