diff --git a/basharin_sevastyan_lab_3/order_service/order_service.py b/basharin_sevastyan_lab_3/order_service/order_service.py index 9d7c1c4..f0b06be 100644 --- a/basharin_sevastyan_lab_3/order_service/order_service.py +++ b/basharin_sevastyan_lab_3/order_service/order_service.py @@ -11,47 +11,65 @@ time.sleep(20) connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq')) channel = connection.channel() channel.queue_declare(queue='order_queue', durable=True) +orders = [] + + +# READ ALL +@app.route('/orders', methods=['GET']) +def show_orders(): + return orders # CREATE -@app.route('/orders', methods=['POST']) -def create_order(): - data = request.get_json() - order = {'user_id': data['user_id'], 'product': data['product'], 'action': 'create'} - +@app.route('/add_order/__', methods=['POST']) +def create_order(user_id, product, action): + order = {'id': len(orders) + 1, + 'user_id': user_id, + 'product': product, + 'action': action} + orders.append(order) # Отправка сообщения о создании заказа - channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) + channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order))) - return order, 201 + return jsonify(order) # READ -@app.route('/orders/', methods=['GET']) +@app.route('/order/', methods=['GET']) def get_order(order_id): - # Реализация READ операции по желанию - return jsonify({'error': 'Read operation not implemented in order service'}), 501 + order = next((order for order in orders if order['id'] == order_id), None) + if order: + read_order = {'id': order['id'], + 'user_id': order['user_id'], + 'product': order['product']} + return jsonify(read_order) + else: + return jsonify({'error': 'Order not found'}) # UPDATE -@app.route('/orders/', methods=['PUT']) -def update_order(order_id): - data = request.get_json() - order = {'user_id': data['user_id'], 'product': data['product'], 'order_id': order_id, 'action': 'update'} - - # Отправка сообщения об обновлении заказа - channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) - - return jsonify(order) +@app.route('/order//__', methods=['PUT']) +def update_order(order_id, user_id, product, action): + 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# Отправка сообщения об обновлении заказа + channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order))) + return jsonify(order) + else: + return jsonify({'error': 'Order not found'}) # DELETE @app.route('/orders/', methods=['DELETE']) def delete_order(order_id): - user_id = request.args.get('user_id') - order = {'user_id': user_id, 'order_id': order_id, 'action': 'delete'} + 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] # Отправка сообщения об удалении заказа - channel.basic_publish(exchange='microservices', routing_key='order', body=json.dumps(order)) + channel.basic_publish(exchange='microservices', routing_key='order', body=bytes(json.dumps(order))) return jsonify({'result': True}) diff --git a/basharin_sevastyan_lab_3/user_service/user_service.py b/basharin_sevastyan_lab_3/user_service/user_service.py index a73ba14..a4b4397 100644 --- a/basharin_sevastyan_lab_3/user_service/user_service.py +++ b/basharin_sevastyan_lab_3/user_service/user_service.py @@ -16,8 +16,9 @@ def connect_to_rabbitmq(): try: connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq')) channel = connection.channel() + channel.exchange_declare(exchange='microservices', exchange_type='direct') channel.queue_declare(queue='order_service', durable=True) - #channel.queue_bind(queue='order_service', routing_key='order') + channel.queue_bind(exchange='microservices', queue='order_service', routing_key='order') channel.basic_consume(queue='order_service', on_message_callback=process_order_message, auto_ack=True) print('Waiting for messages. To exit press CTRL+C') channel.start_consuming() @@ -69,13 +70,14 @@ def process_order_message(ch, method, properties, body): # CRUD операции для пользователей -# CREATE +# READ ALL @app.route('/users', methods=['GET']) def show_users(): return users -@app.route('/add_users/', methods=['POST']) +# CREATE +@app.route('/add_user/', methods=['POST']) def create_user(name): if request.method == 'POST': user = {'id': len(users) + 1, 'name': name}