DAS_2024_1/kosheev_maksim_lab_3/subscriptions_service/app.py

65 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
# Данные об абонементах (в памяти)
subscriptions = [
{
"uuid": str(uuid.uuid4()),
"number": 135,
"fullName": "Иванов И.И.",
"issued": "2023-10-18T05:41:00Z"
}
]
@app.route('/subscriptions/', methods=['GET'])
def get_subscriptions():
return jsonify(subscriptions), 200
@app.route('/subscriptions/<uuid:subscription_id>', methods=['GET'])
def get_subscription(subscription_id):
subscription = next((s for s in subscriptions if s['uuid'] == str(subscription_id)), None)
if subscription:
return jsonify(subscription), 200
return jsonify({"error": "Subscription not found"}), 404
@app.route('/subscriptions/', methods=['POST'])
def create_subscription():
data = request.json
new_subscription = {
"uuid": str(uuid.uuid4()),
"number": data["number"],
"fullName": data["fullName"],
"issued": data["issued"]
}
subscriptions.append(new_subscription)
return jsonify(new_subscription), 201
@app.route('/subscriptions/<uuid:subscription_id>', methods=['PUT'])
def update_subscription(subscription_id):
data = request.json
subscription = next((s for s in subscriptions if s['uuid'] == str(subscription_id)), None)
if not subscription:
return jsonify({"error": "Subscription not found"}), 404
# Обновляем поля, если они переданы
subscription["number"] = data.get("number", subscription["number"])
subscription["fullName"] = data.get("fullName", subscription["fullName"])
subscription["issued"] = data.get("issued", subscription["issued"])
return jsonify(subscription), 200
@app.route('/subscriptions/<uuid:subscription_id>', methods=['DELETE'])
def delete_subscription(subscription_id):
global subscriptions
subscription = next((s for s in subscriptions if s['uuid'] == str(subscription_id)), None)
if not subscription:
return jsonify({"error": "Subscription not found"}), 404
# Удаляем запись
subscriptions = [s for s in subscriptions if s['uuid'] != str(subscription_id)]
return jsonify({"message": "Subscription deleted successfully"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)