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