sync: migrate secure-online-shop to Gitea (2026-08-10)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.exceptions import ForbiddenError
|
||||
from app.models.product import Product
|
||||
from app.models.user import RoleEnum, User
|
||||
from app.schemas.product import ProductCreate
|
||||
|
||||
audit_logger = logging.getLogger("app.audit")
|
||||
|
||||
|
||||
class ProductService:
|
||||
def __init__(self, db: Session) -> None:
|
||||
self.db = db
|
||||
|
||||
def list_available_products(self) -> list[Product]:
|
||||
statement = (
|
||||
select(Product)
|
||||
.where(Product.is_active.is_(True), Product.stock > 0)
|
||||
.order_by(Product.created_at.desc())
|
||||
)
|
||||
return list(self.db.scalars(statement).all())
|
||||
|
||||
def create_product(self, payload: ProductCreate, actor: User) -> Product:
|
||||
if actor.role != RoleEnum.shop:
|
||||
raise ForbiddenError("Shop privileges are required")
|
||||
|
||||
product = Product(
|
||||
name=payload.name,
|
||||
description=payload.description,
|
||||
price=payload.price,
|
||||
stock=payload.stock,
|
||||
is_active=payload.is_active,
|
||||
)
|
||||
self.db.add(product)
|
||||
|
||||
try:
|
||||
self.db.commit()
|
||||
self.db.refresh(product)
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
audit_logger.info("product_created product_id=%s shop_user_id=%s", product.id, actor.id)
|
||||
return product
|
||||
Reference in New Issue
Block a user