o
    ȟ1iP	                     @   sV   d dl m Z mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 G dd dZdS )	    )datetimetimezone)get_db)GenericError)TimeSlotCourse)TimeSlotResponseSchemac                   @   s:   e Zd Zdd Zdd ZdefddZdd	 Zd
d ZdS )TimeSlotServicec                 C   s`   |  }t }tdi |}|| |  || |W  d    S 1 s)w   Y  d S )N )
model_dumpr   r   addcommitrefresh)selfrequestpayloaddbnew_slotr
   r
   /app/services/time_slot.pycreate_new_time_slot   s   

$z$TimeSlotService.create_new_time_slotc                 C   sT   t  }|ttjd  }dd |D W  d    S 1 s#w   Y  d S )Nc                 S   s   g | ]	}t | qS r
   )r   model_validater   ).0recordr
   r
   r   
<listcomp>   s    z7TimeSlotService.fetch_all_time_slot.<locals>.<listcomp>)r   queryr   where
deleted_atis_all)r   r   recordsr
   r
   r   fetch_all_time_slot   s   $z#TimeSlotService.fetch_all_time_slottime_slot_idc                 C   sl   t  )}|ttj|ktjd  }|stdddt	
| W  d    S 1 s/w   Y  d S )N  Time slot not foundstatus_codeexc)r   r   r   r   idr   r   firstr   r   r   r   )r   r!   r   r   r
   r
   r   fetch_time_slot_details   s   

$z'TimeSlotService.fetch_time_slot_detailsc                 C   s   |j dgd}t ,}|ttj|ktjd j|dd}|	  |dkr.t
dddW d    d S 1 s9w   Y  d S )	Nr'   )excludefetch)synchronize_sessionr     zCould not update time slotr$   )r   r   r   r   filterr'   r   r   updater   r   )r   r!   r   r   r   resultr
   r
   r   update_time_slot$   s   "z TimeSlotService.update_time_slotc                 C   s   t  F}|ttj|k dkrtddd|ttj|ktj	
d  }|s3tdddttj|_	|  ddiW  d    S 1 sLw   Y  d S )	Nr   r-   z3Time slot is associated with course. Cannot delete.r$   r"   r#   messagezTime slot deleted successfully)r   r   r   r   r!   countr   r   r'   r   r   r(   r   nowr   utcr   )r   r!   r   	time_slotr
   r
   r   delete_time_slot0   s   

$z TimeSlotService.delete_time_slotN)	__name__
__module____qualname__r   r    intr)   r1   r7   r
   r
   r
   r   r	   	   s    	r	   N)r   r   core.configr   core.security.exceptionsr   modelsr   r   schema.time_slotr   r	   r
   r
   r
   r   <module>   s    