-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
66 lines (52 loc) · 2.43 KB
/
api.py
File metadata and controls
66 lines (52 loc) · 2.43 KB
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
import json
from flask import Flask, request, jsonify
app = Flask(__name__)
def employee_is_valid(employee):
"""Check if the employee data contains required fields."""
required_fields = ['name', 'position', 'department']
return all(field in employee and employee[field] for field in required_fields)
employees = [ {'id': 1, 'name': "Lorenzo", 'age': 25, 'department': "Communications"},
{'id': 2 , 'name': "Giovanni", 'age': 30, 'department': "Marketing"},
{'id': 3, 'name': "Francesca", 'age': 28, 'department': "Accounting"},
{'id': 4, 'name': "Alessandro", 'age': 35, 'department': "IT"},
{'id': 5, 'name': "Luca", 'age': 29, 'department': "HR"}]
nextEmployeeID = len(employees) + 1
@app.route('/employees', methods=['GET'])
def get_employees():
"""Get the list of employees."""
return jsonify(employees)
@app.route('/employees', methods=['POST'])
def add_employee():
"""Add a new employee."""
global nextEmployeeID
employee = json.loads(request.data)
if not employee_is_valid(employee):
return jsonify({"error": "Invalid employee data"}), 400
employee['id'] = len(employees) + 1
employees.append(employee)
return '', jsonify({"message": "Employee added successfully", "employee": employee}), 201
def get_employee(employee_id):
"""Retrieve an employee by ID."""
return next((emp for emp in employees if emp['id'] == employee_id), None)
@app.route('/employees/<int:employee_id>', methods=['PUT'])
def update_employee(employee_id: int):
"""Update an existing employee."""
employee = get_employee(employee_id)
if employee is None:
return jsonify({'error': 'employee not found'}), 404
updated_data = json.loads(request.data)
if not employee_is_valid(updated_data):
return jsonify({'error': 'Invalid employee data'}), 400
employee.update(updated_data)
return jsonify({'message': 'Employee updated successfully', 'employee': employee})
@app.route('/employees/<int:employee_id>', methods=['DELETE'])
def delete_employee(employee_id: int):
"""Delete an employee."""
global employees
employee = get_employee(id)
if employee is None:
return jsonify({'error': 'employee not found'}), 404
employees = [emp for emp in employees if emp['id'] != employee_id]
return jsonify({'message': 'Employee deleted successfully'}), 200
if __name__ == '__main__':
app.run(port=5050, debug=True)