Compare commits

..

3 Commits

3 changed files with 28 additions and 13 deletions

View File

@ -6,42 +6,42 @@ 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.product_id = {product_id}") cursor.execute(f"select name from product where product.id = {product_id}")
result = cursor.fetchone() result = 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 manufacturer from product where product.product_id = {product_id}") 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() result = cursor.fetchone()
return result['manufacturer'] 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.product_id = {product_id}") cursor.execute(f"select price_pc from product where product.id = {product_id}")
result = cursor['price_pc'] result = cursor['price_pc']
return result[0] return result[0]
@staticmethod @staticmethod
def get_image(product_id: int): def get_image(product_id: int):
cursor.execute(f"select image from product where product.product_id = {product_id}") cursor.execute(f"select image from product where product.id = {product_id}")
result = cursor['image'] result = cursor['image']
return base64.b64encode(result[0]).decode('utf-8') return base64.b64encode(result[0]).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.product_id = {product_id}") cursor.execute(f"select image_name from product where product.id = {product_id}")
result = cursor['image_name'] result = cursor['image_name']
return result[0] return result[0]
@staticmethod @staticmethod
def get_all_info(product_id: int): def get_all_info(product_id: int):
cursor.execute(f"select name,manufacturer,price_pc,image_name,image from product where product.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() result = cursor.fetchone()
return { return {
"name": result['name'], "name": result['name'],
"manufacturer": result['manufacturer'], "seller": result['seller'],
"price": result['price_pc'], "price": result['price_pc'],
"image_name": result['image_name'], "image_name": result['image_name'],
"image": base64.b64encode(result['image']).decode('utf-8') "image": base64.b64encode(result['image']).decode('utf-8')

View File

@ -232,7 +232,7 @@ class UserService:
:rtype: bool :rtype: bool
""" """
displayname_regex = r"^[a-zA-Z.-_]{1,64}$" displayname_regex = r"^[a-zA-Z.-_]{1,64}$"
return re.match(username_regex, displayname) return re.match(displayname_regex, displayname)
@staticmethod @staticmethod
def __verify_username(username: str) -> bool: def __verify_username(username: str) -> bool:

View File

@ -121,12 +121,27 @@ after delete
on cart_item for each row on cart_item for each row
begin begin
update cart update cart
set price_total = ( set price_total = ifnull(
select sum(price_subtotal) (select sum(price_subtotal)
from cart_item from cart_item
where cart_id = old.cart_id where cart_id = old.cart_id),
0.00
) )
where id = old.cart_id; where id = old.cart_id;
end; end;
// //
delimiter; delimiter;
delimiter //
create trigger calculate_price_subtotal
before insert on cart_item
for each row
begin
set new.price_subtotal = (
select new.count * p.price_pc
from product p
where p.id = new.product_id
);
END;
//
DELIMITER ;