DAS_2024_1/chernyshov_nikita_lab_3/genres_service/main.py
Никита Чернышов f3d2014ee9 chernyshov_nikita_lab_3 is ready
2024-12-15 16:51:11 +04:00

51 lines
1.3 KiB
Python

from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
genres = {}
@app.route('/genres', methods=['GET'])
def get_genres():
return jsonify(list(genres.values()))
@app.route('/genres/<uuid:genre_uuid>', methods=['GET'])
def get_genre(genre_uuid):
genre = genres.get(str(genre_uuid))
if genre:
return jsonify(genre)
return jsonify({'error': 'Not found'}), 404
@app.route('/genres', methods=['POST'])
def create_genre():
data = request.get_json()
genre_uuid = str(uuid.uuid4())
genre = {
'uuid': genre_uuid,
'name': data['name'],
'description': data['description']
}
genres[genre_uuid] = genre
return jsonify(genre), 201
@app.route('/genres/<uuid:genre_uuid>', methods=['PUT'])
def update_genre(genre_uuid):
genre = genres.get(str(genre_uuid))
if not genre:
return jsonify({'error': 'Not found'}), 404
data = request.get_json()
genre['name'] = data['name']
genre['description'] = data['description']
return jsonify(genre)
@app.route('/genres/<uuid:genre_uuid>', methods=['DELETE'])
def delete_genre(genre_uuid):
if str(genre_uuid) in genres:
del genres[str(genre_uuid)]
return '', 204
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001)