temp images on products
This commit is contained in:
@@ -3,7 +3,9 @@ from sqlalchemy import select, func, Integer, update
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
import utils.barcodes
|
||||
from models.product import Product, ProductBarcode
|
||||
from backend import config
|
||||
from external.s3_uploader.uploader import S3Uploader
|
||||
from models.product import Product, ProductBarcode, ProductImage
|
||||
from schemas.base import PaginationSchema
|
||||
from services.base import BaseService
|
||||
from schemas.product import *
|
||||
@@ -146,6 +148,28 @@ class ProductService(BaseService):
|
||||
raise HTTPException(status_code=404, detail='Товар не найден')
|
||||
return ProductSchema.model_validate(product)
|
||||
|
||||
async def upload_image(self, product_id: int, file_bytes: bytes) -> ProductUploadImageResponse:
|
||||
try:
|
||||
product = await self.get_by_id(product_id)
|
||||
if not product:
|
||||
raise Exception("Неудалось найти товар с указанным ID")
|
||||
s3_uploader = S3Uploader(config.S3_API_KEY)
|
||||
response = await s3_uploader.upload(file_bytes)
|
||||
response_url = response.get('link')
|
||||
if not response_url:
|
||||
raise Exception("Неудалось загрузить изображение")
|
||||
product_image = ProductImage(
|
||||
product_id=product_id,
|
||||
image_url=response_url,
|
||||
)
|
||||
self.session.add(product_image)
|
||||
await self.session.commit()
|
||||
return ProductUploadImageResponse(ok=True,
|
||||
message='Изображение успешно загружено',
|
||||
image_url=response_url)
|
||||
except Exception as e:
|
||||
return ProductUploadImageResponse(ok=False, message=str(e))
|
||||
|
||||
# region Barcodes
|
||||
async def add_barcode(self, request: ProductAddBarcodeRequest):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user