Skip to content

Commit

Permalink
Merge pull request sagaragarwal94#1 from ImaduddinAMajid/POSTMethod
Browse files Browse the repository at this point in the history
Add POST method for /employees route
  • Loading branch information
sagaragarwal94 authored Aug 11, 2017
2 parents f51c8a7 + 8c73aa9 commit 7986df1
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3
from flask import Flask, request
from flask_restful import Resource, Api
from sqlalchemy import create_engine
Expand All @@ -13,6 +14,30 @@ def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("select * from employees") # This line performs query and returns json result
return {'employees': [i[0] for i in query.cursor.fetchall()]} # Fetches first column that is Employee ID
def post(self):
conn = db_connect.connect()
print(request.json)
LastName = request.json['LastName']
FirstName = request.json['FirstName']
Title = request.json['Title']
ReportsTo = request.json['ReportsTo']
BirthDate = request.json['BirthDate']
HireDate = request.json['HireDate']
Address = request.json['Address']
City = request.json['City']
State = request.json['State']
Country = request.json['Country']
PostalCode = request.json['PostalCode']
Phone = request.json['Phone']
Fax = request.json['Fax']
Email = request.json['Email']
query = conn.execute("insert into employees values(null,'{0}','{1}','{2}','{3}', \
'{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}', \
'{13}')".format(LastName,FirstName,Title,
ReportsTo, BirthDate, HireDate, Address,
City, State, Country, PostalCode, Phone, Fax,
Email))
return {'status':'success'}

class Tracks(Resource):
def get(self):
Expand All @@ -27,12 +52,12 @@ def get(self, employee_id):
query = conn.execute("select * from employees where EmployeeId =%d " %int(employee_id))
result = {'data': [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]}
return jsonify(result)


api.add_resource(Employees, '/employees') # Route_1
api.add_resource(Tracks, '/tracks') # Route_2
api.add_resource(Employees_Name, '/employees/<employee_id>') # Route_3


if __name__ == '__main__':
app.run(port='5002')
app.run(port='5002')

0 comments on commit 7986df1

Please sign in to comment.