125 lines
3.5 KiB
Python
125 lines
3.5 KiB
Python
|
from flask import Flask, jsonify, request
|
|||
|
from uuid import uuid4
|
|||
|
import uuid
|
|||
|
import requests
|
|||
|
|
|||
|
|
|||
|
class Genre:
|
|||
|
def __init__(self, name, uuid_: uuid):
|
|||
|
if uuid_ is None:
|
|||
|
self.uuid_: uuid = uuid4()
|
|||
|
else:
|
|||
|
self.uuid_: uuid = uuid.UUID(uuid_)
|
|||
|
self.name: str = name
|
|||
|
|
|||
|
def to_dict(self):
|
|||
|
return {
|
|||
|
"uuid": self.uuid_,
|
|||
|
"name": self.name,
|
|||
|
}
|
|||
|
|
|||
|
def to_dict_with_films(self, films: list):
|
|||
|
return {
|
|||
|
"uuid": self.uuid_,
|
|||
|
"name": self.name,
|
|||
|
"films": films
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
app = Flask(__name__)
|
|||
|
|
|||
|
genres: list[Genre] = [
|
|||
|
Genre(name='Комедия', uuid_='450a9c7c-fb6f-4a42-a354-acef65af4c9b'),
|
|||
|
Genre(name='Драма', uuid_='6f26879d-bdf2-4ccd-bd6f-23275c86a9ac'),
|
|||
|
Genre(name='Экшен', uuid_='139a4578-5922-4688-92e2-22e749ff8c47'),
|
|||
|
]
|
|||
|
|
|||
|
films_url = 'http://film_service:8002/'
|
|||
|
|
|||
|
|
|||
|
def list_jsonify():
|
|||
|
return jsonify([genre.to_dict() for genre in genres])
|
|||
|
|
|||
|
|
|||
|
# получение всех жанров
|
|||
|
@app.route('/', methods=['GET'])
|
|||
|
def get_all():
|
|||
|
return list_jsonify(), 200
|
|||
|
|
|||
|
|
|||
|
# получение жанра по uuid
|
|||
|
@app.route('/<uuid:uuid_>', methods=['GET'])
|
|||
|
def get_one(uuid_):
|
|||
|
for genre in genres:
|
|||
|
if genre.uuid_ == uuid_:
|
|||
|
return genre.to_dict(), 200
|
|||
|
|
|||
|
return 'Факультет с указанным uuid не обнаружен', 404
|
|||
|
|
|||
|
|
|||
|
# получение жанра со списком фильмов в этом жанре
|
|||
|
@app.route('/with-films/<uuid:uuid_>', methods=['GET'])
|
|||
|
def get_one_with_films(uuid_):
|
|||
|
for genre in genres:
|
|||
|
if genre.uuid_ == uuid_:
|
|||
|
response = requests.get(films_url + f'by-genre/{uuid_}')
|
|||
|
print(response.json())
|
|||
|
return genre.to_dict_with_films(response.json()), 200
|
|||
|
|
|||
|
return 'Факультет с указанным uuid не обнаружен', 404
|
|||
|
|
|||
|
|
|||
|
# проверка наличия жанра по uuid
|
|||
|
@app.route('/check/<uuid:uuid_>', methods=['GET'])
|
|||
|
def check_exist(uuid_):
|
|||
|
for genre in genres:
|
|||
|
if genre.uuid_ == uuid_:
|
|||
|
return '', 200
|
|||
|
return '', 404
|
|||
|
|
|||
|
|
|||
|
# создание жанра
|
|||
|
@app.route('/', methods=['POST'])
|
|||
|
def create():
|
|||
|
data = request.json
|
|||
|
name = data.get('name', None)
|
|||
|
description = data.get('description', None)
|
|||
|
if name is None or description is None:
|
|||
|
return 'Не хватает данных для создания нового факультета', 404
|
|||
|
|
|||
|
new_genre = Genre(name, description, None)
|
|||
|
genres.append(new_genre)
|
|||
|
return get_one(new_genre.uuid_)
|
|||
|
|
|||
|
|
|||
|
# изменения жанра по uuid
|
|||
|
@app.route('/<uuid:uuid_>', methods=['PUT'])
|
|||
|
def update_by_id(uuid_):
|
|||
|
data = request.json
|
|||
|
new_name = data.get('name', None)
|
|||
|
new_description = data.get('description', None)
|
|||
|
|
|||
|
for genre in genres:
|
|||
|
if genre.uuid_ == uuid_:
|
|||
|
if new_name is not None:
|
|||
|
genre.name = new_name
|
|||
|
if new_description is not None:
|
|||
|
genre.description = new_description
|
|||
|
return get_one(genre.uuid_)
|
|||
|
|
|||
|
return 'Факультет с указанным uuid не обнаружен', 404
|
|||
|
|
|||
|
|
|||
|
# удаление жанра по uuid
|
|||
|
@app.route('/<uuid:uuid_>', methods=['DELETE'])
|
|||
|
def delete(uuid_):
|
|||
|
for genre in genres:
|
|||
|
if genre.uuid_ == uuid_:
|
|||
|
genres.remove(genre)
|
|||
|
return 'Факультет был удален', 200
|
|||
|
|
|||
|
return 'Факультет с указанным uuid не обнаружен', 404
|
|||
|
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
app.run(host='0.0.0.0', port=8001, debug=True)
|