-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e77d001
commit 53d200e
Showing
10 changed files
with
158 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
from typing import Optional | ||
|
||
from flask import Blueprint, render_template | ||
from flask import Blueprint, jsonify, render_template | ||
|
||
from app.models.category import Category | ||
from app.models.product import Product | ||
from app.models.supplier import Supplier | ||
from app.services.category import CategoryService | ||
from app.services.products import ProductService | ||
from app.services.product import ProductService | ||
from app.services.supplier import SupplierService | ||
|
||
product_bp = Blueprint("product", __name__) | ||
product_service = ProductService() | ||
category_service = CategoryService() | ||
supplier_service = SupplierService() | ||
|
||
|
||
@product_bp.route("/<int:product_id>") | ||
@product_bp.route("/<int:product_id>", methods=["GET"]) | ||
def index(product_id: int): | ||
product: Product = ProductService().get_product_by_id(product_id) | ||
category: Optional[Category] = CategoryService().get_category_by_id(product.CategoryID) | ||
supplier: Optional[Supplier] = SupplierService().get_supplier_by_id(product.SupplierID) | ||
product: Optional[Product] = product_service.get_product_by_id(product_id) | ||
if not product: | ||
raise ValueError(f"Error rendering product page: product not found with id {product_id}") | ||
|
||
category: Optional[Category] = category_service.get_category_by_id(product.CategoryID) | ||
supplier: Optional[Supplier] = supplier_service.get_supplier_by_id(product.SupplierID) | ||
return render_template( | ||
"product/index.html", product=product, category=category, supplier=supplier | ||
) | ||
|
||
|
||
@product_bp.route("/<int:product_id>", methods=["POST"]) | ||
def add_to_cart(product_id): | ||
product: Optional[Product] = product_service.get_product_by_id(product_id) | ||
if not product: | ||
raise ValueError(f"Error adding product to cart: product not found with id {product_id}") | ||
|
||
# quantity = int(request.form.get("quantity", 1)) | ||
|
||
# if quantity > product.UnitsInStock: | ||
# return jsonify({"message": "Requested quantity exceeds available stock"}), 400 | ||
|
||
# cart = session.get("cart", {}) | ||
# cart[product_id] = cart.get(product_id, 0) + quantity | ||
# session["cart"] = cart | ||
|
||
return jsonify({"message": f"Added {product.ProductName} to cart"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from flask import session | ||
|
||
from app.models.product import Cart | ||
|
||
|
||
def get_cart() -> Cart: | ||
return Cart.model_validate(session.get("cart", {})) | ||
|
||
|
||
def save_cart(cart: Cart): | ||
session["cart"] = cart.model_dump() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Optional | ||
|
||
from app.models.product import Product | ||
from app.utils.database import get_db | ||
# from app.utils.logger import setup_logger | ||
|
||
# log = setup_logger("ProductService") | ||
|
||
|
||
class ProductService: | ||
|
||
def __init__(self): | ||
self.cursor = None | ||
self.columns = list(Product.model_fields.keys()) | ||
|
||
|
||
def _ensure_cursor(self): | ||
if self.cursor is None: | ||
self.cursor = get_db().cursor() | ||
return self.cursor | ||
|
||
def get_all_products(self) -> list[Product]: | ||
cursor = self._ensure_cursor() | ||
cursor.execute("SELECT * FROM Products") | ||
products: list[Product] = [ | ||
Product.model_validate(dict(zip(self.columns, row))) for row in cursor.fetchall() | ||
] | ||
return products | ||
|
||
def get_product_by_id(self, product_id: Optional[int]) -> Optional[Product]: | ||
if not product_id: | ||
return None | ||
cursor = self._ensure_cursor() | ||
cursor.execute(f"SELECT * FROM Products WHERE ProductID = {product_id}") | ||
rows: list = cursor.fetchall() | ||
match len(rows): | ||
case 0: | ||
log.info(f"No product found with id {product_id}") | ||
return None | ||
case 1: | ||
product: Product = Product.model_validate(dict(zip(self.columns, rows[0]))) | ||
return product | ||
case _: | ||
log.error( | ||
f"Invalid number of rows returned for product with id {product_id}: {len(rows)}" | ||
) | ||
raise ValueError("Multiple rows returned") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# import logging | ||
|
||
# def setup_logger(name: str) -> logging.Logger: | ||
# """Setup a logger with basic formatting.""" | ||
# logger = logging.getLogger(name) | ||
# logger.setLevel(logging.INFO) | ||
# handler = logging.StreamHandler() | ||
# formatter = logging.Formatter("%(levelname)-8s [%(name)s] %(message)s") | ||
# handler.setFormatter(formatter) | ||
# logger.addHandler(handler) | ||
# logger.propagate = False | ||
# return logger |