DAS_2023_1/antonov_dmitry_lab_3/service_a/app.py

44 lines
1.1 KiB
Python
Raw Normal View History

2023-11-12 12:05:59 +04:00
from flask import Flask, jsonify, request
app = Flask(__name__)
2023-11-12 19:01:15 +04:00
# "customers"
customers_data = []
2023-11-12 12:05:59 +04:00
2023-11-12 19:01:15 +04:00
@app.route('/service_a/', methods=['GET'])
def get_index():
return "Hello from service_a"
2023-11-12 12:05:59 +04:00
2023-11-12 19:01:15 +04:00
@app.route('/service_a/customers', methods=['GET'])
def get_customers():
return jsonify(customers_data)
2023-11-12 12:05:59 +04:00
2023-11-12 19:01:15 +04:00
@app.route('/service_a/customers', methods=['POST'])
def create_customer():
new_customer = request.json
customers_data.append(new_customer)
return jsonify(new_customer), 201
2023-11-12 12:05:59 +04:00
2023-11-12 19:01:15 +04:00
@app.route('/service_a/customers/<int:id>', methods=['PUT'])
def update_customer(id):
for customer in customers_data:
if customer['id'] == id:
customer.update(request.json)
return jsonify(customer), 200
return jsonify({'error': 'Customer not found'}), 404
@app.route('/service_a/customers/<int:id>', methods=['DELETE'])
def delete_customer(id):
global customers_data
customers_data = [customer for customer in customers_data if customer['id'] != id]
return jsonify({'message': 'Customer deleted'}), 200
2023-11-12 12:05:59 +04:00
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)