-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
139 lines (99 loc) · 3.85 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##Author: GEETHA KARTHIKESAN
## DATA REPRESENTATION PROJECT 2020
# flask related
from flask import Flask, jsonify, request, abort
# For interaction with MySQL Table = job in Database = datarep
from zJobsDAO import jobsDAO
app = Flask(__name__, static_url_path='', static_folder='.')
job=[
{"JobId": 1, "JobTitle": "API manufacturer ", "Company": "UCB", "County": "Clare", "NoOfVacancy":100},
{"JobId": 2, "JobTitle": "Animal Health ", "Company": " Bimeda Ireland", "County":"Dublin", "NoOfVacancy": 150},
{"JobId": 3, "JobTitle": "Pharma manufacturer", "Company": "Temmler", "County": "Kerry", "NoOfVacancy": 60},
{"JobId": 4, "JobTitle": "Infants Products", "Company": "Wyeth", "County": "Limerick", "NoOfVacancy": 600},
{"JobId": 5, "JobTitle": "Food Supplements", "Company": "Bioshell", "County": "Mayo", "NoOfVacancy": 40},
]
jobId=6
# Action = Get all job from DB
# curl "http://127.0.0.1:5000/job"
@app.route('/job')
def getAll():
#return "in getAll"
# Connect to job table in datarep DB!
results = jobsDAO.getAll()
return jsonify(results)
#return results
# Action = Find job in DB by ID
# curl "http://127.0.0.1:5000/job/1"
@app.route('/job/<int:JobId>')
def findByID(JobId):
#return "in find by ID for id "+ str(id)
# Return job from DB by requested id
foundJob = jobsDAO.findByID(id)
# Check if id exists
if not foundJob:
return "That id does not exist in the database table"
abort(404)
return jsonify(foundJob)
# create()
# Action = Create a new job in the DB
# curl -i -H "Content-Type:application/json" -X POST -d "{\"JobTitle\": \"Newjob\", \"Company\":\"Poker\", \"County": \"Kildare\",\"NoOfVacancy\": 50 }" "http://127.0.0.1:5000/job"
@app.route('/job', methods=['POST'])
def create():
#return "in create"
if not request.json:
abort(400)
job = {
"JobTitle": request.json['JobTitle'],
"Company": request.json['Company'],
"County": request.json['County'],
"NoOfVacancy": request.json['NoOfVacancy']
}
# Make the tuple for DB
values = (job['JobTitle'], job['Company'], job['County'], job['NoOfVacancy'])
newJobId = jobsDAO.create(values)
job['JobId'] = newJobId
return jsonify(job)
# update(JobId)
#
# Action = Update job in DB by ID
# curl -i -H "Content-Type:application/json" -X PUT -d "{\"NoOfVacancy\":44}" "http://127.0.0.1:5000/job/3"
@app.route('/job/<int:JobId>', methods=['PUT'])
def update(JobId):
#return "in update by ID for JobId "+ str(JobId)
# Find the job in DB table
foundJob = jobsDAO.findByJobId(JobId)
if not foundJob:
return "That JobId does not exist in the database table"
abort(404)
if not request.json:
abort(400)
# Get what was passed up
reqJson = request.json
# Info to update
if 'JobTitle' in reqJson:
foundJob['JobTitle'] = reqJson['JobTitle']
if 'Company' in reqJson:
foundJob['Company'] = reqJson['Company']
if 'County' in reqJson:
foundJob['County'] = reqJson['County']
# Make the tuple for DB
values = (foundJob['JobTitle'], foundJob['Company'], foundJob['County'], foundJobId['JobId'],foundJob['NoOfVacancy'])
# Do the update on DB
jobDAO.update(values)
return jsonify(foundJob)
##################################################################
# Action = Delete job in DB by ID
# curl -X DELETE "http://127.0.0.1:5000/job/3"
@app.route('/job/<int:JobId>', methods=['DELETE'])
def delete(id):
#return "in delete by ID for id "+ str(id)
# Check if id exists in job table in DB
foundJob = jobDAO.findByJobId(JobId)
if not foundJob:
return "That id does not exist in the database table"
abort(404)
# Remove job from DB by id
jobsDAO.delete(id)
return jsonify({"done":True})
if __name__ == '__main__' :
app.run(debug= True)