DAS_2024_1/kadyrov_aydar_lab_3/item_service/main.py

52 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-10-25 01:10:46 +04:00
from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
items = {}
@app.route('/items', methods=['GET'])
def get_items():
return jsonify(list(items.values()))
@app.route('/items/<uuid:item_uuid>', methods=['GET'])
def get_item(item_uuid):
item = items.get(str(item_uuid))
if item:
return jsonify(item)
return jsonify({'error': 'Not found'}), 404
@app.route('/items', methods=['POST'])
def create_item():
data = request.json
item_uuid = str(uuid.uuid4())
item = {
'uuid': item_uuid,
'name': data['name'],
'type': data['type'],
'hero_uuid': data['hero_uuid']
}
items[item_uuid] = item
return jsonify(item), 201
@app.route('/items/<uuid:item_uuid>', methods=['PUT'])
def update_item(item_uuid):
item = items.get(str(item_uuid))
if not item:
return jsonify({'error': 'Not found'}), 404
data = request.json
item['name'] = data['name']
item['type'] = data['type']
item['hero_uuid'] = data['hero_uuid']
return jsonify(item)
@app.route('/items/<uuid:item_uuid>', methods=['DELETE'])
def delete_item(item_uuid):
if str(item_uuid) in items:
del items[str(item_uuid)]
return '', 204
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)