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