54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
from flask import Flask, jsonify, request
|
|
import uuid
|
|
|
|
app = Flask(__name__)
|
|
|
|
teachers = {}
|
|
|
|
# вывод всех пользователей
|
|
@app.route('/teachers', methods=['GET'])
|
|
def get_teachers():
|
|
return jsonify(list(teachers.values()))
|
|
|
|
# получение пользователя по uuid
|
|
@app.route('/teachers/<uuid:teacher_uuid>', methods=['GET'])
|
|
def get_teacher(teacher_uuid):
|
|
teacher = teachers.get(str(teacher_uuid))
|
|
if teacher:
|
|
return jsonify(teacher)
|
|
return jsonify({'error': 'Not found'}), 404
|
|
|
|
# добавление нового пользователя
|
|
@app.route('/teachers', methods=['POST'])
|
|
def create_teacher():
|
|
data = request.get_json()
|
|
teacher_uuid = str(uuid.uuid4())
|
|
teacher = {
|
|
'uuid': teacher_uuid,
|
|
'name': data['name'],
|
|
'age': data['age']
|
|
}
|
|
teachers[teacher_uuid] = teacher
|
|
return jsonify(teacher), 201
|
|
|
|
# изменение пользователя по uuid
|
|
@app.route('/teachers/<uuid:teacher_uuid>', methods=['PUT'])
|
|
def update_teacher(teacher_uuid):
|
|
teacher = teachers.get(str(teacher_uuid))
|
|
if not teacher:
|
|
return jsonify({'error': 'Not found'}), 404
|
|
data = request.get_json()
|
|
teacher['name'] = data['name']
|
|
teacher['age'] = data['age']
|
|
return jsonify(teacher)
|
|
|
|
# удаление пользователя по uuid
|
|
@app.route('/teachers/<uuid:teacher_uuid>', methods=['DELETE'])
|
|
def delete_teacher(teacher_uuid):
|
|
if str(teacher_uuid) in teachers:
|
|
del teachers[str(teacher_uuid)]
|
|
return '', 204
|
|
return jsonify({'error': 'Not found'}), 404
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000) |