o
    h7                  
   @   s  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
Z
d dlZeddgdZejed	gd
d	gd	gd ddddddZeded  ded  ded  ded  d	d
dZeddd Zedd efd!d"Zed#d$efd%d&Zed'd efd(d)ZdS )*    )	APIRouterHTTPException)StaticFiles)CORSMiddleware)create_enginetextNz	/questionquestion)prefixtags*T)allow_originsallow_credentialsallow_methodsallow_headersz3.112.250.101user123zPassWord12!!add_questionsutf8mb4)hostuserpassworddatabasecharsetzmysql+pymysql://r   :r   @r   /r   z?charset=utf8mb4)echoz/get_questionsc               
      s   z*t  } | td}dd | D }|W  d    W S 1 s$w   Y  W d S  ty? } ztdt|dd }~ww )Nz+SELECT id, question FROM `list` ORDER BY idc                 S   s   g | ]}|d  |d dqS )r      )idr    .0rowr   r   +/home/air/segue/gemini/back/add_question.py
<listcomp>*   s    z!get_questions.<locals>.<listcomp>  status_codedetail)engineconnectexecuter   fetchall	Exceptionr   str)
connectionresult	questionser   r   r"   get_questions%   s   
(r2   z/get_answerr   c              
      s   z@t  1}|tdd| i}| }|r*|d |d pddW  d    W S dddW  d    W S 1 s:w   Y  W d S  tyU } ztdt|dd }~ww )	Nz2SELECT question, answer FROM `list` WHERE id = :idr   r   r    )r   answerr$   r%   )r(   r)   r*   r   fetchoner,   r   r-   )r   r.   r/   r!   r1   r   r   r"   
get_answer0   s   
(r6   z/save_questiondatac              
      s   |  d}|  d}|r)|r)|dr)|dd   r)t|dd  tddvr/tdddz1t "}td	| d
}|	|d|i |
  dddW  d    W S 1 sYw   Y  W d S  tyt } ztdt|dd }~ww )Ntabler   Qr        u-   無効なテーブル名または質問ですr%   zINSERT INTO `z` (Q) VALUES (:question)Tu!   データが保存されました)successmessager$   )get
startswithisdigitintranger   r(   r)   r   r*   commitr,   r-   )r7   r8   r   r.   sqlr1   r   r   r"   save_question=   s    

<
(rE   z/questions/{id}c              
      s   | t ddvrtdddd|  }z.t }|td| d}d	d
 | D }|W  d    W S 1 s:w   Y  W d S  tyU } ztdt|dd }~ww )Nr   r:   r;   u   無効なIDですr%   r9   zSELECT Q FROM `z` ORDER BY Qc                 S   s   g | ]}d |d iqS )r9   r   r   r   r   r   r"   r#   W   s    z"get_table_data.<locals>.<listcomp>r$   )	rB   r   r(   r)   r*   r   r+   r,   r-   )r   
table_namer.   r/   r0   r1   r   r   r"   get_table_dataN   s   

(rG   )fastapir   r   fastapi.staticfilesr   fastapi.middleware.corsr   
sqlalchemyr   r   pymysqljsonrouteradd_middleware	DB_CONFIGr(   r>   r2   rA   r6   postdictrE   rG   r   r   r   r"   <module>   s@    
,

