Compare commits

..

No commits in common. "a45efa643389e9e0841109ae67917c8028eb2c76" and "1aca8888d2a4f082e4fa4127ef69872b69f17322" have entirely different histories.

10 changed files with 90 additions and 111 deletions

View File

@ -5,12 +5,11 @@ app = Flask(__name__)
jwt_manager = JWTManager(app)
def create_app():
from app.api import bp, bp_errors, bp_product, bp_user, bp_cart
from app.api import bp, bp_errors, bp_product, bp_user
app.register_blueprint(bp)
app.register_blueprint(bp_errors)
app.register_blueprint(bp_product)
app.register_blueprint(bp_user)
app.register_blueprint(bp_cart)
from app.config import FlaskTesting, FlaskProduction
app.config.from_object(FlaskTesting)

View File

@ -4,6 +4,5 @@ bp_errors = Blueprint('errors', __name__)
bp = Blueprint('api', __name__)
bp_product = Blueprint('product', __name__, url_prefix="/product")
bp_user = Blueprint('user', __name__, url_prefix="/user")
bp_cart = Blueprint('cart', __name__, url_prefix="/cart")
from . import routes

View File

@ -1 +1 @@
from app.api.routes import main_routes, error_routes, product_routes, user_routes, cart_routes
from . import main_routes,error_routes, product_routes, user_routes

View File

@ -1,16 +0,0 @@
from flask import jsonify, abort, request
from flask_jwt_extended import jwt_required, get_jwt_identity
from app.api import bp_cart
from app.services.cart_service import CartService
@bp_cart.route('add/<int:product_id>', methods=['PUT'])
@jwt_required()
def add_to_cart(product_id: int):
user_id = get_jwt_identity()
count = request.args.get('count', default=1, type=int)
result, status_code = CartService.add_to_cart(user_id, product_id, count)
return result, status_code

View File

@ -1,4 +1,4 @@
from app.api import bp_errors
from .. import bp_errors
@bp_errors.app_errorhandler(400)
def bad_request(e):

View File

@ -1,6 +1,6 @@
from flask import jsonify, abort
from app.api import bp
from .. import bp
@bp.route('/')
def hello():

View File

@ -5,14 +5,58 @@ from app.api import bp_product
from app.services.product_service import ProductService
@bp_product.route('/<int:id>', methods=['GET'])
def get_product_info(id: int):
fields = ['name', 'price', 'image', 'image_name', 'seller']
def all_product_info(id: int):
result = ProductService.get_all_info(id)
fields_param = request.args.get('fields')
requested_fields = []
if result is not None:
return jsonify(result)
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'])
def create_product_listing():

View File

@ -1,30 +0,0 @@
from mysql.connector import Error
from typing import Tuple, Union
from app.extensions import db_cursor, db_connection
class CartService:
@staticmethod
def add_to_cart(user_id: str, product_id: int, count: int) -> Tuple[Union[dict, str], int]:
try:
with db_connection.cursor() as cursor:
db_connection.begin()
db_cursor.execute("select * from cart_item where cart_id = %s and product_id = %s", (user_id, product_id))
if db_cursor.rowcount != 0:
db_cursor.execute("update cart_item set count = count + %s where cart_id = %s and product_id = %s", (count, user_id, product_id))
else:
db_cursor.execute("insert into cart_item(cart_id, product_id, count) values (%s, %s, %s)", (user_id, product_id, count))
db_connection.commit()
return {"Success": "Successfully added to cart"}, 200
except Error as e:
return {"Failed": f"Failed to add item to cart. {e}"}
@staticmethod
def show_cart(user_id: str):
return None

View File

@ -1,70 +1,44 @@
import base64
from flask import abort
from mysql.connector import Error
from app.extensions import db_connection
from ..extensions import db_cursor as cursor
class ProductService:
@staticmethod
def get_name(product_id: int):
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()
cursor.execute(f"select name from product where product.id = {product_id}")
result = cursor.fetchone()
return result['name']
@staticmethod
def get_manufacturer(product_id: int):
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()
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()
return result['seller']
@staticmethod
def get_price(product_id: int):
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']
cursor.execute(f"select price_pc from product where product.id = {product_id}")
result = cursor['price_pc']
return result[0]
@staticmethod
def get_image(product_id: int):
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')
cursor.execute(f"select image from product where product.id = {product_id}")
result = cursor['image']
return base64.b64encode(result[0]).decode('utf-8')
@staticmethod
def get_image_name(product_id: int):
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']
cursor.execute(f"select image_name from product where product.id = {product_id}")
result = cursor['image_name']
return result[0]
@staticmethod
def get_all_info(product_id: int):
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}")
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 {
"name": result['name'],
"seller": result['seller'],
@ -74,11 +48,5 @@ class ProductService:
}
@staticmethod
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
def create_listing():
print("asd")

View File

@ -18,8 +18,8 @@ create table user_role(
role varchar(32) not null unique
);
insert into user_role(id, role) values (1, 'normal');
insert into user_role(id, role) values (2, 'admin');
insert into user_role(role) values ('normal')
insert into user_role(role) values ('admin')
create table user_statistics(
id int primary key,
@ -77,6 +77,7 @@ create table purchase_item(
foreign key (product_id) references product(id)
);
delimiter //
create trigger after_user_insert
after insert
on user for each row
@ -84,7 +85,10 @@ begin
insert into user_statistics(id) values (new.id);
insert into cart(id) values (new.id);
end;
//
delimiter;
delimiter //
create trigger after_user_delete
after delete
on user for each row
@ -92,7 +96,10 @@ begin
delete from user_statistics where id = old.id;
delete from cart where id = old.id;
end;
//
delimiter;
delimiter //
create trigger after_cart_insert
after insert
on cart_item for each row
@ -105,7 +112,10 @@ begin
)
where id = new.cart_id;
end;
//
delimiter;
delimiter //
create trigger after_cart_delete
after delete
on cart_item for each row
@ -119,7 +129,10 @@ begin
)
where id = old.cart_id;
end;
//
delimiter;
delimiter //
create trigger calculate_price_subtotal
before insert on cart_item
for each row
@ -130,3 +143,5 @@ begin
where p.id = new.product_id
);
end;
//
delimiter;