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

53 lines
1.4 KiB
Python

from flask import Flask, jsonify, request
import uuid
app = Flask(__name__)
games = {}
@app.route('/games', methods=['GET'])
def get_games():
return jsonify(list(games.values()))
@app.route('/games/<uuid:game_uuid>', methods=['GET'])
def get_game(game_uuid):
game = games.get(str(game_uuid))
if game:
return jsonify(game)
return jsonify({'error': 'Not found'}), 404
@app.route('/games', methods=['POST'])
def create_game():
data = request.get_json()
game_uuid = str(uuid.uuid4())
game = {
'uuid': game_uuid,
'name': data['name'],
'developer': data['developer'],
'genres': data.get('genres', []) # List of genre UUIDs
}
games[game_uuid] = game
return jsonify(game), 201
@app.route('/games/<uuid:game_uuid>', methods=['PUT'])
def update_game(game_uuid):
game = games.get(str(game_uuid))
if not game:
return jsonify({'error': 'Not found'}), 404
data = request.get_json()
game['name'] = data['name']
game['developer'] = data['developer']
game['genres'] = data.get('genres', [])
return jsonify(game)
@app.route('/games/<uuid:game_uuid>', methods=['DELETE'])
def delete_game(game_uuid):
if str(game_uuid) in games:
del games[str(game_uuid)]
return '', 204
return jsonify({'error': 'Not found'}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)