DAS_2024_1/nikolaeva_yana_lab_3/service_subscription/app.py

44 lines
1.4 KiB
Python
Raw Normal View History

2024-12-10 20:14:57 +04:00
from flask import Flask, request, jsonify
from uuid import uuid4
app = Flask(__name__)
subscriptions = []
@app.route("/", methods=["GET"])
def list_subscriptions():
return jsonify(subscriptions)
@app.route("/<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)
return jsonify({"error": "Not found"}), 404
@app.route("/", methods=["POST"])
def create_subscription():
data = request.json
new_subscription = {
"uuid": str(uuid4()),
"number": data["number"],
"fullName": data["fullName"],
"issued": data["issued"],
}
subscriptions.append(new_subscription)
return jsonify(new_subscription), 201
@app.route("/<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 subscription:
subscription.update(data)
return jsonify(subscription)
return jsonify({"error": "Not found"}), 404
@app.route("/<uuid:subscription_id>", methods=["DELETE"])
def delete_subscription(subscription_id):
global subscriptions
subscriptions = [s for s in subscriptions if s["uuid"] != str(subscription_id)]
return "", 204