add tests forn forms, products, productors
All checks were successful
Deploy Amap / deploy (push) Successful in 3m45s
All checks were successful
Deploy Amap / deploy (push) Successful in 3m45s
This commit is contained in:
14
backend/src/products/exceptions.py
Normal file
14
backend/src/products/exceptions.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class ProductServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class ProductorNotFoundError(ProductServiceError):
|
||||
pass
|
||||
|
||||
class ProductNotFoundError(ProductServiceError):
|
||||
pass
|
||||
|
||||
class ProductCreateError(ProductServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -4,7 +4,9 @@ import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.products.service as service
|
||||
import src.products.exceptions as exceptions
|
||||
from src.auth.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix='/products')
|
||||
|
||||
@router.get('', response_model=list[models.ProductPublic], )
|
||||
@@ -40,7 +42,13 @@ def create_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, product)
|
||||
try:
|
||||
result = service.create_one(session, product)
|
||||
except exceptions.ProductCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.productinputinvalid)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductPublic)
|
||||
def update_product(
|
||||
@@ -48,9 +56,12 @@ def update_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, product)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.update_one(session, id, product)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productnotfound)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductPublic)
|
||||
@@ -59,7 +70,8 @@ def delete_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.products.exceptions as exceptions
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -15,7 +16,7 @@ def get_all(
|
||||
if len(names) > 0:
|
||||
statement = statement.where(models.Product.name.in_(names))
|
||||
if len(productors) > 0:
|
||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
||||
statement = statement.where(models.Productor.name.in_(productors))
|
||||
if len(types) > 0:
|
||||
statement = statement.where(models.Product.type.in_(types))
|
||||
return session.exec(statement.order_by(models.Product.name)).all()
|
||||
@@ -24,6 +25,10 @@ def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
return session.get(models.Product, product_id)
|
||||
|
||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
||||
if not product:
|
||||
raise exceptions.ProductCreateError('ProductCreate input cannot be None')
|
||||
if not session.get(models.Productor, product.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = models.Product(**product_create)
|
||||
session.add(new_product)
|
||||
@@ -36,7 +41,10 @@ def update_one(session: Session, id: int, product: models.ProductUpdate) -> mode
|
||||
result = session.exec(statement)
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
return None
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
if product.productor_id and not session.get(models.Productor, product.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
for key, value in product_updates.items():
|
||||
setattr(new_product, key, value)
|
||||
@@ -51,7 +59,7 @@ def delete_one(session: Session, id: int) -> models.ProductPublic:
|
||||
result = session.exec(statement)
|
||||
product = result.first()
|
||||
if not product:
|
||||
return None
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
result = models.ProductPublic.model_validate(product)
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
|
||||
Reference in New Issue
Block a user