Files
crmApi/apps/shop/services/product.py
T
2024-01-22 13:01:42 +03:30

56 lines
1.9 KiB
Python

from apps.shop.models import Product as ProductModel
from decimal import Decimal
from rest_framework import exceptions
def create_product(name: str, price: Decimal, category: int, acc_code: str = None, attr: str = None, workspace=None) -> ProductModel:
try:
product_obj = ProductModel()
product_obj.name = name
product_obj.price = price
product_obj.category_id = category
if acc_code is not None:
product_obj.acc_code = acc_code
if attr is not None:
product_obj.attr = attr
product_obj.sys_ws = workspace
product_obj.full_clean()
product_obj.save()
return product_obj
except Exception as exp:
message = f'Error Create Product: {exp}'
exceptions.ValidationError({'error': message})
def update_product(product_id: int, name: str, price: Decimal, category: int, acc_code: str = None, attr: str = None, workspace_uuid: str = None) -> ProductModel:
try:
product_obj = ProductModel.objects.get(id=product_id, sys_ws__uuid=workspace_uuid)
product_obj.name = name
product_obj.price = price
product_obj.category_id = category
if acc_code is not None:
product_obj.acc_code = acc_code
if attr is not None:
product_obj.attr = attr
product_obj.full_clean()
product_obj.save()
return product_obj
except Exception as exp:
message = f'Error Updating Product: {exp}'
raise exceptions.ValidationError({'error': message})
def delete_product(product_id: int, workspace_uuid: str) -> ProductModel:
try:
product_obj = ProductModel.objects.get(id=product_id, sys_ws__uuid=workspace_uuid).delete()
return product_obj
except Exception as exp:
message = f'Error Deleting Product: {exp}'
raise exceptions.ValidationError({'error': message})