
    \i                     T    d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
  G d d      Zy)	zLeague repository    )OptionalList)Session)IntegrityError)LeagueCountryc                       e Zd ZdZdefdZdedee   fdZ	de
dee   fdZdee   fd	Zd
edefdZdde
de
dz  defdZy)LeagueRepositoryz Repository for league operationsdbc                     || _         y N)r   )selfr   s     2/app/app/core/db/repositories/league_repository.py__init__zLeagueRepository.__init__   s	        	league_idreturnc                     | j                   j                  t              j                  t        j                  |k(        j                         S )zGet league by ID)r   queryr   filteridfirst)r   r   s     r   	get_by_idzLeagueRepository.get_by_id   s2    ww}}V$++FII,BCIIKKr   	source_idc                     | j                   j                  t              j                  t        j                  |k(        j                         S )zGet league by source ID)r   r   r   r   r   r   )r   r   s     r   get_by_source_idz!LeagueRepository.get_by_source_id   s4    ww}}V$++F,<,<	,IJPPRRr   c                 \    | j                   j                  t              j                         S )zGet all leagues)r   r   r   all)r   s    r   get_allzLeagueRepository.get_all   s    ww}}V$((**r   leaguec                    | j                   j                  |       	 | j                   j                          | j                   j                  |       |S # t        $ r | j                   j	                          | j                   j                  t              j                  t        j                  |j                  k(  t        j                  |j                  k(        j                         cY S w xY w)zCreate a new league)r   addcommitr   rollbackr   r   r   
country_idnamer   refresh)r   r    s     r   createzLeagueRepository.create   s    F		GGNN 	  	GGf%))V->->>v{{@Z[	s   A BC-,C-Nr&   codec                 b   | j                   j                  t              j                  t        j                  |k(        j                         }|r|S t        ||      }| j                   j                  |       | j                   j                          | j                   j                  |       |S )z$Get or create country dictionary row)r&   r)   )	r   r   r   r   r&   r   r"   r#   r'   )r   r&   r)   countrys       r   get_or_create_countryz&LeagueRepository.get_or_create_country*   sx    ''--(//0DEKKMNt$/G r   r   )__name__
__module____qualname____doc__r   r   intr   r   r   strr   r   r   r(   r   r,    r   r   r
   r
      s    *7 L3 L8F+; LS# S(62B S+f +V   
# 
S4Z 
7 
r   r
   N)r0   typingr   r   sqlalchemy.ormr   sqlalchemy.excr   app.core.db.models.sportr   r   r
   r3   r   r   <module>r8      s     ! " ) 4, ,r   