swag-shop/app/services/product_service.py

127 lines
3.4 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
2024-03-10 22:47:42 +01:00
from decimal import Decimal
2024-03-08 14:05:22 +01:00
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):
2024-03-10 23:10:41 +01:00
"""
Fetches 10 products
:param page: Page, aka offset of fetching
:type page: int
"""
2024-03-10 17:11:26 +01:00
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:
2024-05-06 00:52:03 +02:00
return {"msg": "Failed to fetch products. You've probably selected too far with pages"}, 400
2024-03-10 17:11:26 +01:00
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:
2024-05-06 00:52:03 +02:00
return {"msg": f"Failed to fetch products. Error: {e}"}, 500
2024-03-10 17:11:26 +01:00
2024-03-05 16:01:26 +01:00
@staticmethod
2024-03-10 21:19:34 +01:00
def get_product_info(fields: list[str], product_id: int):
2024-03-10 23:10:41 +01:00
"""
Fetches specific product info
:param fields: array of fields that can be fetched
:type fields: list[str]
:param product_id: ID of product to be updated.
:type product_id: int
"""
2024-03-10 21:19:34 +01:00
try:
with db_connection.cursor(dictionary=True) as cursor:
fields_to_sql = {
"name": "product.name",
"price": "product.price_pc as price",
"image": "product.image",
"image_name": "product.image_name",
"seller": "user.displayname as seller"
}
field_sql = []
2024-03-05 16:01:26 +01:00
2024-03-10 21:19:34 +01:00
for field in fields:
field_sql.append(fields_to_sql[field])
2024-03-08 14:05:22 +01:00
2024-03-10 21:19:34 +01:00
select_params = ", ".join(field_sql)
2024-03-08 14:05:22 +01:00
2024-03-10 21:19:34 +01:00
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,))
2024-03-05 16:01:26 +01:00
2024-03-10 21:19:34 +01:00
result = cursor.fetchone()
2024-03-05 16:01:26 +01:00
2024-03-10 21:19:34 +01:00
if cursor.rowcount != 1:
2024-05-06 00:52:03 +02:00
return {"msg": "Failed to fetch product. Product likely doesn't exist"}, 400
2024-03-10 21:19:34 +01:00
result_obj = {}
2024-03-08 14:05:22 +01:00
2024-03-10 21:19:34 +01:00
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]
2024-03-05 16:01:26 +01:00
2024-03-10 21:19:34 +01:00
return result_obj, 200
except Error as e:
2024-05-06 00:52:03 +02:00
return {"msg": f"Failed to fetch product info. Error: {e}"}, 500
@staticmethod
2024-03-10 22:47:42 +01:00
def create_listing(seller_id: str, name: str, price: float):
2024-03-10 23:10:41 +01:00
"""
Creates a new product listing
:param seller_id: User ID
:type seller_id: str
:param name: New product's name
:type name: str
:param price: New product's price
:type price: float
"""
2024-03-08 14:05:22 +01:00
try:
2024-03-10 22:47:42 +01:00
with db_connection.cursor() as cursor:
rounded_price = round(price, 2)
cursor.execute("insert into product(seller_id, name, price_pc) values (%s, %s, %s)", (seller_id, name, Decimal(str(rounded_price))))
db_connection.commit()
2024-03-08 14:05:22 +01:00
except Error as e:
2024-05-06 00:52:03 +02:00
return {"msg": f"Failed to create product. {e}"}, 400
2024-03-08 14:05:22 +01:00
2024-05-06 00:52:03 +02:00
return {"msg": "Successfully created new product listing"}, 200
2024-03-10 22:47:42 +01:00
@staticmethod
def __is_base64_jpg(decoded_string):
try:
# Checking if the decoded data represents a valid JPEG image
image_type = imghdr.what(None, decoded_data)
return image_type == 'jpeg'
except Exception:
return False