from flask import Flask, jsonify, request import uuid app = Flask(__name__) tariffs = {} @app.route('/', methods=['GET']) def get_tariffs(): return jsonify(list(tariffs.values())) @app.route('/with-service/<uuid:service_uuid>', methods=['GET']) def get_tariffs_with_service(service_uuid): return jsonify(list(filter(lambda t: t["service_uuid"] == str(service_uuid), tariffs.values()))) @app.route('/<uuid:tariff_uuid>', methods=['GET']) def get_tariff(tariff_uuid): if str(tariff_uuid) in tariffs: return jsonify(tariffs[str(tariff_uuid)]) return jsonify({'message': 'Tariff not found'}), 404 @app.route('/', methods=['POST']) def create_tariff(): data = request.get_json() if not data or 'name' not in data or 'price' not in data or 'service_uuid' not in data: return jsonify({'message': 'Invalid data'}), 400 tariff_uuid = str(uuid.uuid4()) tariffs[tariff_uuid] = {'uuid': tariff_uuid, 'name': data['name'], 'price': data['price'], 'service_uuid': data['service_uuid']} return jsonify(tariffs[tariff_uuid]), 201 @app.route('/<uuid:tariff_uuid>', methods=['PUT']) def update_tariff(tariff_uuid): if str(tariff_uuid) not in tariffs: return jsonify({'message': 'Tariff not found'}), 404 data = request.get_json() if not data or 'name' not in data or 'price' not in data or 'service_uuid' not in data: return jsonify({'message': 'Invalid data'}), 400 tariffs[str(tariff_uuid)]['name'] = data['name'] tariffs[str(tariff_uuid)]['price'] = data['price'] tariffs[str(tariff_uuid)]['service_uuid'] = data['service_uuid'] return jsonify(tariffs[str(tariff_uuid)]), 200 @app.route('/<uuid:tariff_uuid>', methods=['DELETE']) def delete_tariff(tariff_uuid): if str(tariff_uuid) in tariffs: del tariffs[str(tariff_uuid)] return jsonify({'message': 'Tariff deleted'}), 200 return jsonify({'message': 'Tariff not found'}), 404 if __name__ == '__main__': app.run(host='0.0.0.0', port=5001)