Inital work on better product fetching
This commit is contained in:
parent
a7a1ea72f1
commit
a45efa6433
@ -5,58 +5,14 @@ from app.api import bp_product
|
|||||||
from app.services.product_service import ProductService
|
from app.services.product_service import ProductService
|
||||||
|
|
||||||
@bp_product.route('/<int:id>', methods=['GET'])
|
@bp_product.route('/<int:id>', methods=['GET'])
|
||||||
def all_product_info(id: int):
|
def get_product_info(id: int):
|
||||||
result = ProductService.get_all_info(id)
|
fields = ['name', 'price', 'image', 'image_name', 'seller']
|
||||||
|
|
||||||
if result is not None:
|
fields_param = request.args.get('fields')
|
||||||
return jsonify(result)
|
requested_fields = []
|
||||||
else:
|
|
||||||
abort(404)
|
return abort(501)
|
||||||
|
|
||||||
@bp_product.route('/<int:id>/name', methods=['GET'])
|
|
||||||
def get_name(id: int):
|
|
||||||
result = ProductService.get_name(id)
|
|
||||||
|
|
||||||
if result is not None:
|
|
||||||
return jsonify({"name": result})
|
|
||||||
else:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
@bp_product.route('/<int:id>/manufacturer', methods=['GET'])
|
|
||||||
def get_manufacturer(id: int):
|
|
||||||
result = ProductService.get_manufacturer(id)
|
|
||||||
|
|
||||||
if result is not None:
|
|
||||||
return jsonify({"name": result})
|
|
||||||
else:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
@bp_product.route('/<int:id>/price', methods=['GET'])
|
|
||||||
def get_price(id: int):
|
|
||||||
result = ProductService.get_price(id)
|
|
||||||
|
|
||||||
if result is not None:
|
|
||||||
return jsonify({"price": result})
|
|
||||||
else:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
@bp_product.route('/<int:id>/image', methods=['GET'])
|
|
||||||
def get_image(id: int):
|
|
||||||
result = ProductService.get_image(id)
|
|
||||||
|
|
||||||
if result is not None:
|
|
||||||
return jsonify({"image": result})
|
|
||||||
else:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
@bp_product.route('/<int:id>/image_name', methods=['GET'])
|
|
||||||
def get_image_name(id: int):
|
|
||||||
result = ProductService.get_image_name(id)
|
|
||||||
|
|
||||||
if result is not None:
|
|
||||||
return jsonify({"image_name": result})
|
|
||||||
else:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
@bp_product.route('/create', methods=['POST'])
|
@bp_product.route('/create', methods=['POST'])
|
||||||
def create_product_listing():
|
def create_product_listing():
|
||||||
|
@ -1,44 +1,70 @@
|
|||||||
import base64
|
import base64
|
||||||
|
|
||||||
from ..extensions import db_cursor as cursor
|
from flask import abort
|
||||||
|
from mysql.connector import Error
|
||||||
|
|
||||||
|
from app.extensions import db_connection
|
||||||
|
|
||||||
class ProductService:
|
class ProductService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_name(product_id: int):
|
def get_name(product_id: int):
|
||||||
cursor.execute(f"select name from product where product.id = {product_id}")
|
db_cursor.execute(f"select name from product where product.id = {product_id}")
|
||||||
result = cursor.fetchone()
|
|
||||||
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
return result['name']
|
return result['name']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_manufacturer(product_id: int):
|
def get_manufacturer(product_id: int):
|
||||||
cursor.execute(f"select user.displayname as seller from product inner join user on product.seller_id = user.id where product.id = {product_id}")
|
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}")
|
||||||
result = cursor.fetchone()
|
|
||||||
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
return result['seller']
|
return result['seller']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_price(product_id: int):
|
def get_price(product_id: int):
|
||||||
cursor.execute(f"select price_pc from product where product.id = {product_id}")
|
db_cursor.execute(f"select price_pc from product where product.id = {product_id}")
|
||||||
result = cursor['price_pc']
|
|
||||||
return result[0]
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
|
return result['price_pc']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_image(product_id: int):
|
def get_image(product_id: int):
|
||||||
cursor.execute(f"select image from product where product.id = {product_id}")
|
db_cursor.execute(f"select image from product where product.id = {product_id}")
|
||||||
result = cursor['image']
|
|
||||||
return base64.b64encode(result[0]).decode('utf-8')
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
|
return base64.b64encode(result['image']).decode('utf-8')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_image_name(product_id: int):
|
def get_image_name(product_id: int):
|
||||||
cursor.execute(f"select image_name from product where product.id = {product_id}")
|
db_cursor.execute(f"select image_name from product where product.id = {product_id}")
|
||||||
result = cursor['image_name']
|
|
||||||
return result[0]
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
|
return result['image_name']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_all_info(product_id: int):
|
def get_all_info(product_id: int):
|
||||||
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}")
|
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}")
|
||||||
result = cursor.fetchone()
|
|
||||||
|
|
||||||
|
if db_cursor.rowcount != 1:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
result = db_cursor.fetchone()
|
||||||
return {
|
return {
|
||||||
"name": result['name'],
|
"name": result['name'],
|
||||||
"seller": result['seller'],
|
"seller": result['seller'],
|
||||||
@ -48,5 +74,11 @@ class ProductService:
|
|||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_listing():
|
def create_listing(name: str, seller_id: str, price: float, image_name: str, image):
|
||||||
print("asd")
|
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
|
Loading…
x
Reference in New Issue
Block a user