diff --git a/basharin_sevastyan_lab_3/order_service/order_service.py b/basharin_sevastyan_lab_3/order_service/order_service.py index 05b437c..81d5ac2 100644 --- a/basharin_sevastyan_lab_3/order_service/order_service.py +++ b/basharin_sevastyan_lab_3/order_service/order_service.py @@ -21,22 +21,22 @@ def show_orders(): # CREATE -@app.route('/add_order/__', methods=['POST']) -def create_order(user_id, product, action): +@app.route('/add_order/_', 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/', methods=['GET']) +@app.route('/order/get_', 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//__', methods=['PUT']) -def update_order(order_id, user_id, product, action): +@app.route('/order//', 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/', methods=['DELETE']) +@app.route('/order/del_', 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}) diff --git a/basharin_sevastyan_lab_3/order_service/requirements.txt b/basharin_sevastyan_lab_3/order_service/requirements.txt index 411f23b..3d70f32 100644 --- a/basharin_sevastyan_lab_3/order_service/requirements.txt +++ b/basharin_sevastyan_lab_3/order_service/requirements.txt @@ -1,2 +1,3 @@ Flask==3.0.0 -requests==2.31.0 \ No newline at end of file +requests==2.31.0 +gunicorn==21.2.0 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_service/requirements.txt b/basharin_sevastyan_lab_3/user_service/requirements.txt index 411f23b..3d70f32 100644 --- a/basharin_sevastyan_lab_3/user_service/requirements.txt +++ b/basharin_sevastyan_lab_3/user_service/requirements.txt @@ -1,2 +1,3 @@ Flask==3.0.0 -requests==2.31.0 \ No newline at end of file +requests==2.31.0 +gunicorn==21.2.0 \ No newline at end of file diff --git a/basharin_sevastyan_lab_3/user_service/user_service.py b/basharin_sevastyan_lab_3/user_service/user_service.py index b9026ba..7249978 100644 --- a/basharin_sevastyan_lab_3/user_service/user_service.py +++ b/basharin_sevastyan_lab_3/user_service/user_service.py @@ -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/', 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/', methods=['GET']) +@app.route('/users/get_', 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/_', methods=['PUT']) +@app.route('/users/upd__', 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/', methods=['DELETE']) +@app.route('/users/del_', methods=['DELETE']) def delete_user(user_id): global users users = [user for user in users if user['id'] != user_id]