
    2YhF                         d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ dded	ed
efdZdedefdZdededefdZdedede	fdZdedefdZy)    )Session)HTTPExceptionstatus)WineAppellation)WineAppellationCreateSchemaWineAppellationUpdateSchemaWineAppellationOutSchema)QueryPaginator)Region	SubRegion)Countrydbpageper_pagec                     |dz
  |z  }| j                  t              }t        |t        d||d      }|j	                         S )N   z/wine-appellationsT)queryschemaurloffsetlimituse_orm)r   r   r
   r	   paginate)r   r   r   r   r   	paginators         f/var/www/html/wine-match-dev/backend/winematch-backend/src/apps/wine_settings/services/appellations.pyget_all_wine_appellationsr      sK    Qh("FHH_%E' I     appellation_idc                     | j                  t              j                  t        j                  |k(        j	                         }|st        dd      |S )N  zWine Appellation not foundstatus_codedetail)r   r   filteridfirstr   r   r   objs      r   get_wine_appellation_by_idr)      sE    
((?
#
*
*?+=+=+O
P
V
V
XC4PQQJr   payloadreturnc                    | j                  t              j                  t        j                  |j                  k(        j                         }|st        dd      | j                  t              j                  t        j                  |j                  k(        j                         }|st        dd      |j                  r| j                  t              j                  t        j                  |j                  k(        j                         }|st        dd      |j                  |j                  k7  r(t        dd|j                   d|j                   d	      | j                  t              j                  t        j                  |j                  k(  t        j                  |j                  k(  t        j                  |j                  k(        j                         }|rt        dd
      t        di |j                         }| j                  |       | j                          | j!                  |       |S )Nr    zCountry not foundr!   zRegion not foundzSub-region not foundi  zSub-region (ID: z!) does not belong to Region (ID: )zWine Appellation already exists )r   r   r$   r%   
country_idr&   r   r   	region_idsub_region_idr   r   titledictaddcommitrefresh)r   r*   countryregion
sub_regionexistingappellations          r   create_wine_appellationr<   &   s   hhw&&wzzW5G5G'GHNNPG4GHH XXf$$VYY'2C2C%CDJJLF4FGG XXi(//	@U@U0UV\\^
C8NOO7#4#44)'*?*?)@@abibsbsattuv  xx(//.!!W%6%66%%)>)>> eg	 
 4UVV "3GLLN3KFF;IIKJJ{r   c                     t        | |      }|j                  d      j                         D ]  \  }}t        |||        | j	                          | j                  |       |S )NT)exclude_unset)r)   r3   itemssetattrr5   r6   )r   r   r*   r(   fieldvalues         r   update_wine_appellationrC   N   s\    
$R
8C48>>@ #uUE"# IIKJJsOJr   c                 ^    t        | |      }| j                  |       | j                          y )N)r)   deleter5   r'   s      r   delete_wine_appellationrF   X   s"    
$R
8CIIcNIIKr   N)r   
   )sqlalchemy.ormr   fastapir   r   *src.apps.wine_settings.models.appellationsr   +src.apps.wine_settings.schemas.appellationsr   r   r	   src.utils.paginationr
   src.apps.base.models.regionsr   r   src.apps.base.models.countryr   intr   r)   r<   rC   rF   r.   r   r   <module>rP      s    " ) F 
 0 : 0 '    C  7 C % %2M %Ra %P  Gb   r   