o
    G_Êi¹  ã                   @   s‚   d dl mZmZmZ d dlmZ d dlmZmZ eƒ Z	eeeddZ
e	 d¡dded	efd
d„ƒZe	 d¡dedefdd„ƒZdS )é    )Ú	APIRouterÚHTTPExceptionÚRequest)ÚProductDatabaseModel)Ú	MONGO_URIÚDB_NAMEÚproducts)Údb_uriÚdb_nameÚcollection_namez	/productsNÚrequestÚ
categoryIdc                 Ã   s  zni }|r
||d< t j |¡}g }|D ]U}| dg ¡}t|ƒdkr&|d nd}|}|r9| d¡s9| j› d|› }| dd¡| dd¡| dd¡ ¡ | dd¡t| d	d¡ƒ| d
d¡| dd¡|dœ}	| 	|	¡ qd|dœW S  t
yˆ }
 zdt|
ƒdœW  Y d }
~
S d }
~
ww )NÚcategoryNameÚimagesr   Ú Úhttpúuploads/upload_img/Ú	productIdÚtitleÚpriceÚbadgeÚ
badgeColorz#3b82f6)Úidr   r   r   r   r   r   ÚimageF©ÚerrorÚdataT©r   Úmessage)Údb_modelÚ
collectionÚfindÚgetÚlenÚ
startswithÚbase_urlÚlowerÚfloatÚappendÚ	ExceptionÚstr)r   r   ÚqueryZproducts_cursorZresult_listÚpr   Zimg_nameZ	image_urlÚitemÚe© r/   úCC:\quy\pod-logistic\pod-api\controller\public\product_controller.pyÚget_public_products   s<   €




ø
þ€ÿr1   z/products/{product_id}Ú
product_idc           	      Ã   s°  z°t j d|i¡}|stddd‚| dg ¡}g }|D ]}|r1| d¡s1| | j› d|› ¡ q| |¡ q|s<dg}| dd	¡| d
d	¡| dd	¡ ¡ | dd	¡t	| dd¡ƒ| dd¡| dd	¡| dd¡|| dg d¢¡| dddddœ¡dœ}| d¡rˆ| d¡|d< | d¡r”| d¡|d< | d¡r | d¡|d< | d¡r¬| d¡|d< d|d œW S  ty¿ } z|‚d }~w t
y× } zd!t|ƒd"œW  Y d }~S d }~ww )#Nr   i”  zProduct not found)Ústatus_codeÚdetailr   r   r   zhttps://via.placeholder.com/500r   r   r   r   r   Údescriptionz!Premium quality custom product...r   r   ÚyellowÚsizes)ÚSÚMÚLZXLZ2XLÚdesignTemplatei”  i  i,  )ÚwidthÚheightÚdpi)r   r   r   r   r   r5   r   r   r   r7   r;   ÚcolorsÚvariantsÚshippingRatesÚ	sizeGuideFr   Tr   )r   r    Úfind_oner   r"   r$   r(   r%   r&   r'   r)   r*   )	r   r2   r,   Z
raw_imagesZformatted_imagesÚimgr-   Úher.   r/   r/   r0   Úget_product_detail;   s^   €





ýõ



þ€€ÿrF   )N)Úfastapir   r   r   Úmodel.productr   ÚMongoDBConnectionr   r   Úrouterr   r"   r*   r1   rF   r/   r/   r/   r0   Ú<module>   s    ý*