#main2a.py    ===========================


 
 
#
import pymysql
#import PyMySQL

# THIS  https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04
#kill -9 $(lsof -i:5000 -t) 2> /dev/null

from app import app
from config import mysql
from flask import jsonify
from flask import flash, request
		
		
		
		
@app.route('/add', methods=['POST'])
def add_emp():
	try:
		_json = request.json
		_name = _json['name']
		_email = _json['email']
		_phone = _json['phone']
		_address = _json['address']
		if _name and _email and _phone and _address and request.method == 'POST':			
			sqlQuery = "INSERT INTO rest_emp(name, email, phone, address) VALUES(%s, %s, %s, %s, %s)"
			bindData = (_name, _email, _phone, _address)
			conn = mysql.connect()
			cursor = conn.cursor()
			cursor.execute(sqlQuery, bindData)
			conn.commit()
			respone = jsonify('Employee added successfully!')
			respone.status_code = 200
			return respone
		else:
			return not_found()
	except Exception as e:
		print(e)
	finally:
		cursor.close() 
		conn.close()
		
@app.route('/emp')
def emp():
	try:
		conn = mysql.connect()
		cursor = conn.cursor(pymysql.cursors.DictCursor)
		cursor.execute("SELECT id, name, email, phone, address FROM rest_emp")
		empRows = cursor.fetchall()
		respone = jsonify(empRows)
		respone.status_code = 200
		return respone
	except Exception as e:
		print(e)
	finally:
		cursor.close() 
		conn.close()
		
@app.route('/emp2/')
def emp2(id):
	try:
		conn = mysql.connect()
		cursor = conn.cursor(pymysql.cursors.DictCursor)
		cursor.execute("SELECT id, name, email, phone, address FROM rest_emp WHERE id =%s", id)
		empRow = cursor.fetchone()
		respone = jsonify(empRow)
		respone.status_code = 200
		return respone
	except Exception as e:
		print(e)
	finally:
		cursor.close() 
		conn.close()

#@app.route('/emp2a/')
@app.route('/emp2a')
def emp2a(id):
	if 'id' in request.args:
		v_id= request.args.get('id')
		print ("test", v_id)
		try:
			conn = mysql.connect()
			cursor = conn.cursor(pymysql.cursors.DictCursor)
			cursor.execute("SELECT id, name, email, phone, address FROM rest_emp WHERE id =%s", id)
			empRow = cursor.fetchone()
			respone2 = jsonify(empRow)
			respone2.status_code = 200
			return respone2
		except Exception as e:
			print(e)
		finally:
			cursor.close() 
			conn.close()
	else:
		return "Error: No id field provided. Please specify an id."
#@app.route('/delete/', methods=['DELETE'])  Removed methods worked 
@app.route('/delete/')
def delete_emp(id):
	try:
		conn = mysql.connect()
		cursor = conn.cursor()
		cursor.execute("DELETE FROM rest_emp WHERE id =%s", (id,))
		conn.commit()
		respone = jsonify('Employee deleted successfully!')
		respone.status_code = 200
		return respone
	except Exception as e:
		print(e)
	finally:
		cursor.close() 
		conn.close()
@app.route('/help/', methods=['GET', 'POST'])
def welcome():
    #return "Use /emp to show all records. Use /delete/ to delete a record."
    return '''

Help for API

Use /emp to show all records. Use /delete/ to delete a record.

''' @app.errorhandler(404) def not_found(error=None): message = { 'status': 404, 'message': 'Record not found try again: ' + request.url, } respone = jsonify(message) respone.status_code = 404 return respone if __name__ == "__main__": app.run() ==================== ==config.py from app import app from flaskext.mysql import MySQL mysql = MySQL() app.config['MYSQL_DATABASE_USER'] = ' ' app.config['MYSQL_DATABASE_PASSWORD'] = ' ' app.config['MYSQL_DATABASE_DB'] = 'blog_posts' app.config['MYSQL_DATABASE_HOST'] = 'localhost' mysql.init_app(app) ========== ==wsgi.py from main2a import app if __name__ == "__main__": app.run() =========== ===app.py from flask import Flask from flask_cors import CORS, cross_origin app = Flask(__name__) CORS(app)