from router import route
from utils import render_template, parse_query, NOT_FOUND_CODE
from database import db
from config import SERVER_HOST, SERVER_PORT
@route('/update', ['POST'])
def update_post(query, *args):
expressions, conditions = parse_query(query)
cursor = db.cursor()
cursor.execute('UPDATE `table_task1` SET {} WHERE {}'.format(
expressions, conditions
))
result = cursor.fetchall()
cursor.close()
return f'
UPDATE: {result}
'
@route('/delete', ['POST'])
def delete_post(query, *args):
return f'DELETE: {query}
'
@route('/add', ['POST'])
def add_post(query, *args):
return f'ADD: {query}
'
@route('/get', ['POST'])
def db_get(query, *args):
cursor = db.cursor()
if query['type'] == 'full':
cursor.execute('DESCRIBE table_task1;')
table_structure = cursor.fetchall()
cursor.execute('SELECT * FROM table_task1;')
content = cursor.fetchall()
cursor.close()
table_headers = [field[0] for field in table_structure]
json_content = []
for row in content:
new_row = []
for col in row:
if not isinstance(col, (float, bool, int, dict, list, tuple)):
col = str(col)
new_row.append(col)
json_content.append(new_row)
return {'headers': table_headers, 'content': json_content}
elif query['type'] == 'single_id':
cursor.execute(f'SELECT * FROM table_task1 WHERE service_id="{query["service_id"]}";')
content = cursor.fetchone()
cursor.close()
json_content = []
for col in content:
if not isinstance(col, (float, bool, int, dict, list, tuple)):
col = str(col)
json_content.append(col)
return json_content
return NOT_FOUND_CODE
@route('/')
def index_get(query, *args):
data = render_template('index.html')
return data
if __name__ == '__main__':
from server import start_server
start_server(SERVER_HOST, SERVER_PORT)