3
Uh                 @   s   d dl mZmZmZ d dlmZmZmZmZ d dl	m
Z
 d dlZdd Zejddgd	d
d Zejddd Zejddd Zejddgd	dd Zejddgd	dd ZdS )    )Flaskjsonifyrequest)appdbdb_cache	FromCache)AutoNc             C   s   | j   d S )N)close)session r   )/booking_service/app/routes/autos_urls.pyr
      s    r
   z
/autos/newPOST)methodsc              C   s   g } t jjddj }|s4| jd td| idfS tjjt	j
t	jj|t	jd kj }|rptddgidfS t	|j d}tjj| y$tjj  tt tdd	gid
fS  tk
 r } z*tjj  tt tdgt|ddfS d }~X nX d S )Nauto zAuto name is required.messagesi  messagez'That auto already exists in the system.)r   zSuccessfully added an auto.   z#There was an error adding the auto.)r   error)r   jsongetstripappendr   r   r   queryr	   filterr   ilikedeletion_markerfirsttitleaddcommitr
   	Exceptionrollbackstr)r   Z	auto_nameexistingr   er   r   r   add_new_auto
   s(    



r'   z/autos/viewc              C   s   g } g }t jjtjtjd kjttj	tj
j j }|sX| jd td| idfS x|D ]}| j|j  q^W td| idfS d S )Nz:There are no vehicles in the system at this point in time.r   r   data)r   r   r   r	   r   r   optionsr   r   order_byr   ascallr   r   return_json)outputid_arrayZ	all_autossingler   r   r   get_all_autos(   s    


r1   z/autos/view/<public_id>c             C   sP   t jjtjtj| ktjd kj }|s:tddgidfS td|j	 gidfS )Nr   z'That auto is not present in the system.i  r(   r   )
r   r   r   r	   r   	public_idr   r   r   r-   )r2   r   r   r   r   view_single_auto?   s
    r3   z/autos/editPATCHc              C   s   t j} | jd}| jddj }| s,| r>tddgidfS tjjtj	tj
|ktjd kj }|sxtddgid	fS |j |_y$tjj  tt tdd
gidfS  tk
r } z*tjj  tt tdgt|ddfS d }~X nX d S )Nr2   r   r   r   z%public_id and auto name are required.i  r   zThat auto does not exist.i  zSuccessfully updated auto.r   zFailed to update auto.)r   r   )r   r   r   r   r   r   r   r   r	   r   r2   r   r   r   r   r!   r
   r"   r#   r$   )r(   r2   new_namer   r&   r   r   r   	edit_autoK   s&    



r6   z/autos/deletec              C   s   t jjddj } | s(tddgidfS tjjtj	tj
| ktjd kj }|sbtddgidfS d	|_y$tjj  tt tdd
gidfS  tk
r } z*tjj  tt tdgt|ddfS d }~X nX d S )Nr2   r   r   zpublic_id is required.i  r   zThat auto does not exist.i     zDeletion successful.r   zError deleting auto.)r   r   )r   r   r   r   r   r   r   r   r	   r   r2   r   r   r!   r
   r"   r#   r$   )r2   r   r&   r   r   r   delete_autoh   s"    

r8   )flaskr   r   r   routesr   r   r   r   Zdatabase.autor	   uuidr
   router'   r1   r3   r6   r8   r   r   r   r   <module>   s   