157 lines
5.2 KiB
Python
157 lines
5.2 KiB
Python
from flask import Flask, jsonify, request
|
||
import requests
|
||
from uuid import uuid4
|
||
import uuid
|
||
|
||
|
||
class Film:
|
||
def __init__(self, name, description, uuid_: uuid, genre_id: uuid):
|
||
if uuid_ is None:
|
||
self.uuid_: uuid = uuid4()
|
||
else:
|
||
self.uuid_: uuid = uuid.UUID(uuid_)
|
||
self.name: str = name
|
||
self.description: str = description
|
||
self.genre_id: uuid = uuid.UUID(genre_id)
|
||
|
||
def to_dict(self):
|
||
return {
|
||
'name': self.name,
|
||
'description': self.description,
|
||
'genre_id': self.genre_id,
|
||
'uuid': self.uuid_
|
||
}
|
||
|
||
def to_dict_for_genres(self):
|
||
return {
|
||
'name': self.name,
|
||
'description': self.description,
|
||
'uuid': self.uuid_
|
||
}
|
||
|
||
def to_dict_with_info(self, genre: dict):
|
||
return {
|
||
'name': self.name,
|
||
'description': self.description,
|
||
'genre_id': self.genre_id,
|
||
'genre_info': genre,
|
||
'uuid': self.uuid_
|
||
}
|
||
|
||
|
||
app = Flask(__name__)
|
||
|
||
specialities: list[Film] = [
|
||
Film(name='Пираты карибского моря', description='фильм про пиратов', uuid_='51e5c5c1-1e6e-4455-b4e1-aec5774a2961',
|
||
genre_id='450a9c7c-fb6f-4a42-a354-acef65af4c9b'),
|
||
Film(name='1+1', description='=2', uuid_='660e4da0-abe9-4d33-9070-f3e8f67484f4',
|
||
genre_id='6f26879d-bdf2-4ccd-bd6f-23275c86a9ac'),
|
||
Film(name='Во все тяжкие', description='познавательный сериал про химию', uuid_='801d599b-ce70-4755-a2ef-00e1c092811d',
|
||
genre_id='139a4578-5922-4688-92e2-22e749ff8c47'),
|
||
]
|
||
|
||
genres_url = 'http://genre_service:8001/'
|
||
|
||
|
||
def list_jsonify():
|
||
return jsonify([speciality.to_dict() for speciality in specialities])
|
||
|
||
|
||
# получение всех фильмов
|
||
@app.route('/', methods=['GET'])
|
||
def get_all():
|
||
return list_jsonify(), 200
|
||
|
||
|
||
# получение всех фильмов с информацие о жанре
|
||
@app.route('/full', methods=['GET'])
|
||
def get_all_full():
|
||
genres: list[dict] = requests.get(genres_url).json()
|
||
response = []
|
||
for speciality in specialities:
|
||
for genre in genres:
|
||
if speciality.genre_id == uuid.UUID(genre.get('uuid')):
|
||
response.append(speciality.to_dict_with_info(genre))
|
||
|
||
return response, 200
|
||
|
||
|
||
# получение фильмов по uuid жанра
|
||
@app.route('/by-genre/<uuid:genre_uuid>', methods=['GET'])
|
||
def get_by_genre_id(genre_uuid):
|
||
return [speciality.to_dict_for_genres() for speciality in specialities if speciality.genre_id == genre_uuid], 200
|
||
|
||
|
||
# получение фильма по uuid
|
||
@app.route('/<uuid:uuid_>', methods=['GET'])
|
||
def get_one(uuid_):
|
||
for speciality in specialities:
|
||
if speciality.uuid_ == uuid_:
|
||
return speciality.to_dict(), 200
|
||
|
||
return 'Специальность с указанным uuid не обнаружена', 404
|
||
|
||
|
||
# получение фильма по uuid с информацие о жанре
|
||
@app.route('/full/<uuid:uuid_>', methods=['GET'])
|
||
def get_one_full(uuid_):
|
||
for speciality in specialities:
|
||
if speciality.uuid_ == uuid_:
|
||
response = requests.get(genres_url + str(speciality.genre_id))
|
||
return speciality.to_dict_with_info(response.json()), 200
|
||
|
||
return 'Специальность с указанным uuid не обнаружена', 404
|
||
|
||
|
||
# создание фильма
|
||
@app.route('/', methods=['POST'])
|
||
def create():
|
||
data = request.json
|
||
name = data.get('name', None)
|
||
description = data.get('description', None)
|
||
genre_id = data.get('genre_id', None)
|
||
checking = requests.get(genres_url + f'/check/{genre_id}')
|
||
print(checking)
|
||
if checking.status_code == 200:
|
||
new_speciality = Film(name, description, None, genre_id)
|
||
specialities.append(new_speciality)
|
||
return get_one(new_speciality.uuid_)
|
||
if checking.status_code == 404:
|
||
return 'Факультета с таким uuid не существует', 404
|
||
|
||
return 'Неизвестная ошибка', 500
|
||
|
||
|
||
# изменение фильма по 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 speciality in specialities:
|
||
print(speciality.uuid_)
|
||
|
||
if speciality.uuid_ == uuid_:
|
||
if new_name is not None:
|
||
speciality.name = new_name
|
||
if new_description is not None:
|
||
speciality.description = new_description
|
||
return get_one(speciality.uuid_)
|
||
|
||
return 'Специальность с указанным uuid не обнаружена', 404
|
||
|
||
|
||
# удаление фильма по uuid
|
||
@app.route('/<uuid:uuid_>', methods=['DELETE'])
|
||
def delete(uuid_):
|
||
for speciality in specialities:
|
||
if speciality.uuid_ == uuid_:
|
||
specialities.remove(speciality)
|
||
return 'Специальность была удалена', 200
|
||
|
||
return 'Специальность с указанным uuid не обнаружена', 404
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app.run(host='0.0.0.0', port=8002, debug=True) |