DAS_2024_1/pupkov_alexey_lab_3/services/subscription/app.py

54 lines
1.6 KiB
Python

from flask import Flask, request, jsonify
import uuid
app = Flask(__name__)
# Хранилище данных
subscriptions = []
@app.route('/api/subscriptions/', methods=['GET'])
def get_subscriptions():
return jsonify(subscriptions), 200
@app.route('/api/subscriptions/<uuid:uuid>', methods=['GET'])
def get_subscription(uuid):
subscription = next((sub for sub in subscriptions if sub['uuid'] == str(uuid)), None)
if not subscription:
return jsonify({'error': 'Subscription not found'}), 404
return jsonify(subscription), 200
@app.route('/api/subscriptions/', methods=['POST'])
def create_subscription():
data = request.json
subscription = {
'uuid': str(uuid.uuid4()),
'number': data['number'],
'fullName': data['fullName'],
'issued': data['issued']
}
subscriptions.append(subscription)
return jsonify(subscription), 201
@app.route('/api/subscriptions/<uuid:uuid>', methods=['PUT'])
def update_subscription(uuid):
data = request.json
subscription = next((sub for sub in subscriptions if sub['uuid'] == str(uuid)), None)
if not subscription:
return jsonify({'error': 'Subscription not found'}), 404
subscription.update({
'number': data['number'],
'fullName': data['fullName'],
'issued': data['issued']
})
return jsonify(subscription), 200
@app.route('/api/subscriptions/<uuid:uuid>', methods=['DELETE'])
def delete_subscription(uuid):
global subscriptions
subscriptions = [sub for sub in subscriptions if sub['uuid'] != str(uuid)]
return '', 204
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)