basharin_sevastyan_lab_3 #25

Merged
Alexey merged 7 commits from basharin_sevastyan_lab_3 into main 2023-12-05 22:25:43 +04:00
2 changed files with 45 additions and 25 deletions
Showing only changes of commit 06eba2e084 - Show all commits

View File

@ -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/<int:user_id>_<string:product>_<string:action>', 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/<int:order_id>', methods=['GET'])
@app.route('/order/<int:order_id>', 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/<int:order_id>', 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/<int:order_id>/<int:user_id>_<string:product>_<string:action>', 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/<int:order_id>', 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})

View File

@ -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/<string:name>', methods=['POST'])
# CREATE
@app.route('/add_user/<string:name>', methods=['POST'])
def create_user(name):
if request.method == 'POST':
user = {'id': len(users) + 1, 'name': name}