Product creation
This commit is contained in:
parent
f3f1652890
commit
105bbd3e96
@ -1,4 +1,5 @@
|
|||||||
from flask import jsonify, abort, request
|
from flask import jsonify, abort, request
|
||||||
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||||
|
|
||||||
from app.api import bp_product
|
from app.api import bp_product
|
||||||
|
|
||||||
@ -29,7 +30,23 @@ def get_product_info(id: int):
|
|||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bp_product.route('/create', methods=['POST'])
|
@bp_product.route('/create', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
def create_product_listing():
|
def create_product_listing():
|
||||||
|
user_id = get_jwt_identity()
|
||||||
name = request.json.get('name')
|
name = request.json.get('name')
|
||||||
|
price = request.json.get('price')
|
||||||
|
|
||||||
|
if name is None or price is None:
|
||||||
|
return abort(401)
|
||||||
|
|
||||||
|
float_price = float(price)
|
||||||
|
|
||||||
|
if not isinstance(float_price, float):
|
||||||
|
return abort(401)
|
||||||
|
|
||||||
|
result, status_code = ProductService.create_listing(user_id, name, float_price)
|
||||||
|
|
||||||
|
return result, status_code
|
@ -3,6 +3,8 @@ import base64
|
|||||||
from flask import abort
|
from flask import abort
|
||||||
from mysql.connector import Error
|
from mysql.connector import Error
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from app.extensions import db_connection
|
from app.extensions import db_connection
|
||||||
|
|
||||||
class ProductService:
|
class ProductService:
|
||||||
@ -76,11 +78,23 @@ class ProductService:
|
|||||||
return {"Failed": f"Failed to fetch product info. Error: {e}"}, 500
|
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(seller_id: str, name: str, price: float):
|
||||||
try:
|
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))
|
with db_connection.cursor() as cursor:
|
||||||
db_connection.commit()
|
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()
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to create product. {e}"}, 400
|
return {"Failed": f"Failed to create product. {e}"}, 400
|
||||||
|
|
||||||
return {"Success", "Successfully created new product listing"}, 200
|
return {"Success": "Successfully created new product listing"}, 200
|
||||||
|
|
||||||
|
@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
|
Loading…
x
Reference in New Issue
Block a user