sevastyan_basharin_lab_3 is ready
This commit is contained in:
parent
22b163ed29
commit
5e85f5a315
@ -21,22 +21,22 @@ def show_orders():
|
|||||||
|
|
||||||
|
|
||||||
# CREATE
|
# CREATE
|
||||||
@app.route('/add_order/<int:user_id>_<string:product>_<string:action>', methods=['POST'])
|
@app.route('/add_order/<int:user_id>_<string:product>', methods=['POST'])
|
||||||
def create_order(user_id, product, action):
|
def create_order(user_id, product):
|
||||||
order = {'id': len(orders) + 1,
|
order = {'id': len(orders) + 1,
|
||||||
'user_id': user_id,
|
'user_id': user_id,
|
||||||
'product': product,
|
'product': product,
|
||||||
'action': action}
|
'action': 'create'}
|
||||||
orders.append(order)
|
orders.append(order)
|
||||||
# Отправка сообщения о создании заказа
|
# Отправка сообщения о создании заказа
|
||||||
requests.post("http://localhost/order-service/event", json=order)
|
requests.post("http://user-service:5001/event", json=order)
|
||||||
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'),properties=pika.BasicProperties(delivery_mode=2, # make message persistent))
|
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'),properties=pika.BasicProperties(delivery_mode=2, # make message persistent))
|
||||||
|
|
||||||
return jsonify(order)
|
return jsonify(order)
|
||||||
|
|
||||||
|
|
||||||
# READ
|
# READ
|
||||||
@app.route('/order/<int:order_id>', methods=['GET'])
|
@app.route('/order/get_<int:order_id>', methods=['GET'])
|
||||||
def get_order(order_id):
|
def get_order(order_id):
|
||||||
order = next((order for order in orders if order['id'] == order_id), None)
|
order = next((order for order in orders if order['id'] == order_id), None)
|
||||||
if order:
|
if order:
|
||||||
@ -49,15 +49,14 @@ def get_order(order_id):
|
|||||||
|
|
||||||
|
|
||||||
# UPDATE
|
# UPDATE
|
||||||
@app.route('/order/<int:order_id>/<int:user_id>_<string:product>_<string:action>', methods=['PUT'])
|
@app.route('/order/<int:order_id>/<string:product>', methods=['PUT'])
|
||||||
def update_order(order_id, user_id, product, action):
|
def update_order(order_id, product):
|
||||||
order = next((order for order in orders if order['id'] == order_id), None)
|
order = next((order for order in orders if order['id'] == order_id), None)
|
||||||
if order:
|
if order:
|
||||||
order['user_id'] = user_id
|
|
||||||
order['product'] = product
|
order['product'] = product
|
||||||
order['action'] = action
|
order['action'] = 'update'
|
||||||
# Отправка сообщения об обновлении заказа
|
# Отправка сообщения об обновлении заказа
|
||||||
requests.post("http://localhost/order-service/event", json=order)
|
requests.post("http://user-service:5001/event", json=order)
|
||||||
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'), properties=pika.BasicProperties(delivery_mode=2, # make message persistent))
|
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'), properties=pika.BasicProperties(delivery_mode=2, # make message persistent))
|
||||||
return jsonify(order)
|
return jsonify(order)
|
||||||
else:
|
else:
|
||||||
@ -65,14 +64,15 @@ def update_order(order_id, user_id, product, action):
|
|||||||
|
|
||||||
|
|
||||||
# DELETE
|
# DELETE
|
||||||
@app.route('/orders/<int:order_id>', methods=['DELETE'])
|
@app.route('/order/del_<int:order_id>', methods=['DELETE'])
|
||||||
def delete_order(order_id):
|
def delete_order(order_id):
|
||||||
global orders
|
global orders
|
||||||
order = next((order for order in orders if order['id'] == order_id), None)
|
del_order = next((order for order in orders if order['id'] == order_id), None)
|
||||||
orders = [order for order in orders if order['id'] != order]
|
orders = [order for order in orders if order['id'] != order_id]
|
||||||
|
del_order['action'] = 'delete'
|
||||||
|
|
||||||
# Отправка сообщения об удалении заказа
|
# Отправка сообщения об удалении заказа
|
||||||
requests.post("http://localhost/order-service/event", json=order)
|
requests.post("http://user-service:5001/event", json=del_order)
|
||||||
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'), properties=pika.BasicProperties(delivery_mode=2, # make message persistent ))
|
#channel.basic_publish(exchange='', routing_key='order_service', body=json.dumps(order).encode('utf-8'), properties=pika.BasicProperties(delivery_mode=2, # make message persistent ))
|
||||||
|
|
||||||
return jsonify({'result': True})
|
return jsonify({'result': True})
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
Flask==3.0.0
|
Flask==3.0.0
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
|
gunicorn==21.2.0
|
@ -1,2 +1,3 @@
|
|||||||
Flask==3.0.0
|
Flask==3.0.0
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
|
gunicorn==21.2.0
|
@ -73,8 +73,9 @@ users = []
|
|||||||
|
|
||||||
# CRUD операции для пользователей
|
# CRUD операции для пользователей
|
||||||
# READ ALL
|
# READ ALL
|
||||||
@app.route('/event')
|
@app.route('/event', methods=['POST'])
|
||||||
def event():
|
def event():
|
||||||
|
print('получено сообщение')
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
print(data)
|
print(data)
|
||||||
user_id = data.get('user_id')
|
user_id = data.get('user_id')
|
||||||
@ -94,7 +95,7 @@ def event():
|
|||||||
# Обновление заказа у пользователя
|
# Обновление заказа у пользователя
|
||||||
user = next((user for user in users if user['id'] == user_id), None)
|
user = next((user for user in users if user['id'] == user_id), None)
|
||||||
if user:
|
if user:
|
||||||
order_id = data.get('order_id')
|
order_id = data.get('id')
|
||||||
order = next((order for order in user['orders'] if order['id'] == order_id), None)
|
order = next((order for order in user['orders'] if order['id'] == order_id), None)
|
||||||
if order:
|
if order:
|
||||||
order['product'] = data['product']
|
order['product'] = data['product']
|
||||||
@ -108,11 +109,12 @@ def event():
|
|||||||
# Удаление заказа у пользователя
|
# Удаление заказа у пользователя
|
||||||
user = next((user for user in users if user['id'] == user_id), None)
|
user = next((user for user in users if user['id'] == user_id), None)
|
||||||
if user:
|
if user:
|
||||||
order_id = data.get('order_id')
|
order_id = user.get('id')
|
||||||
user['orders'] = [order for order in user.get('orders', []) if order['id'] != order_id]
|
user['orders'] = [order for order in user.get('orders', []) if order['id'] != order_id]
|
||||||
print(f"Order deleted for user {user_id}: {order_id}")
|
print(f"Order deleted for user {user_id}: {order_id}")
|
||||||
else:
|
else:
|
||||||
print(f"User not found for order deletion: {user_id}")
|
print(f"User not found for order deletion: {user_id}")
|
||||||
|
return jsonify({'result': True})
|
||||||
|
|
||||||
@app.route('/users', methods=['GET'])
|
@app.route('/users', methods=['GET'])
|
||||||
def show_users():
|
def show_users():
|
||||||
@ -123,13 +125,17 @@ def show_users():
|
|||||||
@app.route('/add_user/<string:name>', methods=['POST'])
|
@app.route('/add_user/<string:name>', methods=['POST'])
|
||||||
def create_user(name):
|
def create_user(name):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
user = {'id': len(users) + 1, 'name': name}
|
if len(users) == 0:
|
||||||
|
id = 1
|
||||||
|
else:
|
||||||
|
id = users[-1]['id'] + 1
|
||||||
|
user = {'id': id, 'name': name}
|
||||||
users.append(user)
|
users.append(user)
|
||||||
return jsonify(user)
|
return jsonify(user)
|
||||||
|
|
||||||
|
|
||||||
# READ
|
# READ
|
||||||
@app.route('/users/<int:user_id>', methods=['GET'])
|
@app.route('/users/get_<int:user_id>', methods=['GET'])
|
||||||
def get_user(user_id):
|
def get_user(user_id):
|
||||||
user = next((user for user in users if user['id'] == user_id), None)
|
user = next((user for user in users if user['id'] == user_id), None)
|
||||||
if user:
|
if user:
|
||||||
@ -139,7 +145,7 @@ def get_user(user_id):
|
|||||||
|
|
||||||
|
|
||||||
# UPDATE
|
# UPDATE
|
||||||
@app.route('/users/<int:user_id>_<string:name>', methods=['PUT'])
|
@app.route('/users/upd_<int:user_id>_<string:name>', methods=['PUT'])
|
||||||
def update_user(user_id, name):
|
def update_user(user_id, name):
|
||||||
user = next((user for user in users if user['id'] == user_id), None)
|
user = next((user for user in users if user['id'] == user_id), None)
|
||||||
if user:
|
if user:
|
||||||
@ -150,7 +156,7 @@ def update_user(user_id, name):
|
|||||||
|
|
||||||
|
|
||||||
# DELETE
|
# DELETE
|
||||||
@app.route('/users/<int:user_id>', methods=['DELETE'])
|
@app.route('/users/del_<int:user_id>', methods=['DELETE'])
|
||||||
def delete_user(user_id):
|
def delete_user(user_id):
|
||||||
global users
|
global users
|
||||||
users = [user for user in users if user['id'] != user_id]
|
users = [user for user in users if user['id'] != user_id]
|
||||||
|
Loading…
Reference in New Issue
Block a user