diff --git a/tsukanova_irina_lab_3/author_service/author_service.py b/tsukanova_irina_lab_3/author_service/author_service.py index a475a84..1269b38 100644 --- a/tsukanova_irina_lab_3/author_service/author_service.py +++ b/tsukanova_irina_lab_3/author_service/author_service.py @@ -37,7 +37,6 @@ authors: list[Author] = [ Author(name='Tess', surname='Root', uuid_='eb815350-c7b9-4446-8434-4c0640c21995') ] - books_url = 'http://localhost:5001/' @@ -45,11 +44,13 @@ def list_jsonify(): return jsonify([author.to_dict() for author in authors]) +# получение списка всех авторов @app.route('/', methods=['GET']) def get_all(): return list_jsonify(), 200 +# получение автора по идентификатору @app.route('/', methods=['GET']) def get_one(uuid_): for author in authors: @@ -59,6 +60,7 @@ def get_one(uuid_): return 'Автор с таким uuid не был найден', 404 +# получение автора со списком его книг @app.route('/with-books/', methods=['GET']) def get_one_with_books(uuid_): for author in authors: @@ -70,19 +72,30 @@ def get_one_with_books(uuid_): return 'Автор с таким uuid не был найден', 404 +# проверка наличия автора по его идентификатору (для book_service) +@app.route('/check/', methods=['GET']) +def check_exist(uuid_): + for author in authors: + if author.uuid_ == uuid_: + return '', 200 + return '', 404 + + +# создание автора @app.route('/', methods=['POST']) def create(): data = request.json name = data.get('name', None) surname = data.get('surname', None) - # if name is None or surname is None: - # return 'Недостаточно полей для создания нового автора', 404 + if name is None or surname is None: + return 'Недостаточно полей для создания нового автора', 404 new_author = Author(name, surname, None) authors.append(new_author) return get_one(new_author.uuid_) +# изменение автора по идентификатору @app.route('/', methods=['PUT']) def update_by_id(uuid_): data = request.json @@ -100,15 +113,16 @@ def update_by_id(uuid_): return 'Автор с таким uuid не был найден', 404 +# удаление автора по идентификатору @app.route('/', methods=['DELETE']) def delete(uuid_): for author in authors: if author.uuid_ == uuid_: authors.remove(author) - return '', 200 + return 'Автор успешно удален', 200 return 'Автор с таким uuid не был найден', 404 if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) + app.run(host='0.0.0.0', port=20001, debug=True) diff --git a/tsukanova_irina_lab_3/author_service/requirements.txt b/tsukanova_irina_lab_3/author_service/requirements.txt index 404c0b6..bbbb04b 100644 Binary files a/tsukanova_irina_lab_3/author_service/requirements.txt and b/tsukanova_irina_lab_3/author_service/requirements.txt differ diff --git a/tsukanova_irina_lab_3/book_service/book_service.py b/tsukanova_irina_lab_3/book_service/book_service.py index 624a6df..67b75ba 100644 --- a/tsukanova_irina_lab_3/book_service/book_service.py +++ b/tsukanova_irina_lab_3/book_service/book_service.py @@ -4,23 +4,6 @@ from uuid import uuid4 import uuid -class Author: - def __init__(self, name, surname, uuid_: uuid): - if uuid_ is None: - self.uuid_: uuid = uuid4() - else: - self.uuid_: uuid = uuid.UUID(uuid_) - self.name: str = name - self.surname: str = surname - - def to_dict(self): - return { - "uuid": self.uuid_, - "name": self.name, - "surname": self.surname - } - - class Book: def __init__(self, title, year, uuid_: uuid, author_id: uuid): if uuid_ is None: @@ -59,15 +42,20 @@ class Book: app = Flask(__name__) books: list[Book] = [ - Book(title='Garden', year=1977, uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19', author_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), - Book(title='New York', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='The story of flowers', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, author_id='eb815350-c7b9-4446-8434-4c0640c21995'), - Book(title='Little Rock', year=1990, uuid_='3fad0e6b-cefc-40dd-99c0-adc5ec0290d2', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='One Piece', year=1994, uuid_='cf0cf26e-c1c6-43ea-bf59-1f8edb180e41', author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), - Book(title='Black clover', year=1940, uuid_='2ac3c734-21ad-4450-884d-7de1f5452b9d', author_id='eb815350-c7b9-4446-8434-4c0640c21995'), + Book(title='Garden', year=1977, uuid_='89fa1e7a-7e88-445e-a4d8-6d4497ea8f19', + author_id='997aa4c5-ebb2-4794-ba81-e742f9f1fa30'), + Book(title='New York', year=1989, uuid_='0351ee11-f11b-4d83-b2c8-1075b0c357dc', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='The story of flowers', uuid_='dfc17619-7690-47aa-ae8e-6a5068f8ddec', year=1977, + author_id='eb815350-c7b9-4446-8434-4c0640c21995'), + Book(title='Little Rock', year=1990, uuid_='3fad0e6b-cefc-40dd-99c0-adc5ec0290d2', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='One Piece', year=1994, uuid_='cf0cf26e-c1c6-43ea-bf59-1f8edb180e41', + author_id='694827e4-0f93-45a5-8f75-bad7ef2d21fe'), + Book(title='Black clover', year=1940, uuid_='2ac3c734-21ad-4450-884d-7de1f5452b9d', + author_id='eb815350-c7b9-4446-8434-4c0640c21995'), ] - authors_url = 'http://localhost:5000/' @@ -75,11 +63,13 @@ def list_jsonify(): return jsonify([book.to_dict() for book in books]) +# получить список книг без подробной инфы об авторе @app.route('/', methods=['GET']) def get_all(): return list_jsonify(), 200 +# получение всех книг с информацией об авторе @app.route('/full', methods=['GET']) def get_all_full(): authors: list[dict] = requests.get(authors_url).json() @@ -92,11 +82,13 @@ def get_all_full(): return response, 200 +# получение списка книг по идентификатору автора (для author_service) @app.route('/by-author/', methods=['GET']) def get_by_author_id(author_uuid): return [book.to_dict_for_authors() for book in books if book.author_id == author_uuid], 200 +# получение книги по идентификатору @app.route('/', methods=['GET']) def get_one(uuid_): for book in books: @@ -106,6 +98,7 @@ def get_one(uuid_): return 'Книга с таким uuid не была найдена', 404 +# получение книги по идентификатору с инфой об авторе @app.route('/full/', methods=['GET']) def get_one_full(uuid_): for book in books: @@ -116,18 +109,26 @@ def get_one_full(uuid_): return 'Книга с таким uuid не была найдена', 404 +# создание новой книги @app.route('/', methods=['POST']) def create(): data = request.json title = data.get('title', None) year = data.get('year', None) author_id = data.get('author_id', None) + checking = requests.get(authors_url + f'/check/{author_id}') + print(checking) + if checking.status_code == 200: + new_book = Book(title, year, None, author_id) + books.append(new_book) + return get_one(new_book.uuid_) + if checking.status_code == 404: + return 'Автор с таким uuid не существует', 404 - new_book = Book(title, year, None, author_id) - books.append(new_book) - return get_one(new_book.uuid_) + return 'Неизвестная ошибка', 500 +# изменение книги по идентификатору @app.route('/', methods=['PUT']) def update_by_id(uuid_): data = request.json @@ -135,6 +136,8 @@ def update_by_id(uuid_): new_year = data.get('year', None) for book in books: + print(book.uuid_) + if book.uuid_ == uuid_: if new_title is not None: book.title = new_title @@ -145,15 +148,16 @@ def update_by_id(uuid_): return 'Книга с таким uuid не была найдена', 404 +# удаление книги по идентификатору @app.route('/', methods=['DELETE']) def delete(uuid_): for book in books: if book.uuid_ == uuid_: books.remove(book) - return '', 200 + return 'Книга успешно удалена', 200 return 'Книга с таким uuid не была найдена', 404 if __name__ == '__main__': - app.run(host='0.0.0.0', port=5001, debug=True) + app.run(host='0.0.0.0', port=20002, debug=True) diff --git a/tsukanova_irina_lab_3/book_service/requirements.txt b/tsukanova_irina_lab_3/book_service/requirements.txt index 404c0b6..41917d8 100644 Binary files a/tsukanova_irina_lab_3/book_service/requirements.txt and b/tsukanova_irina_lab_3/book_service/requirements.txt differ diff --git a/tsukanova_irina_lab_3/docker-compose.yaml b/tsukanova_irina_lab_3/docker-compose.yaml index 58ca660..3e34647 100644 --- a/tsukanova_irina_lab_3/docker-compose.yaml +++ b/tsukanova_irina_lab_3/docker-compose.yaml @@ -6,22 +6,22 @@ services: context: . dockerfile: ./author_service/Dockerfile ports: - - "20001:5000" + - 20001 - book_service: - container_name: book_service - build: - context: . - dockerfile: ./book_service/Dockerfile - ports: - - "20002:5000" + book_service: + container_name: book_service + build: + context: . + dockerfile: ./book_service/Dockerfile + expose: + - 20002 - nginx: - image: nginx:latest - ports: - - "80:80" - depends_on: - - author_service - - book_service - volumes: - - ./nginx/nginx.conf:/etc/nginx/nginx.conf \ No newline at end of file + nginx: + image: nginx:latest + ports: + - "80:80" + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + depends_on: + - author_service + - book_service \ No newline at end of file diff --git a/tsukanova_irina_lab_3/nginx/nginx.conf b/tsukanova_irina_lab_3/nginx/nginx.conf index f6ff7c0..805d451 100644 --- a/tsukanova_irina_lab_3/nginx/nginx.conf +++ b/tsukanova_irina_lab_3/nginx/nginx.conf @@ -1,6 +1,10 @@ + +events { worker_connections 1024; } + http { server { - listen 80; + listen 80; + listen [::]:80; server_name localhost; location /author_service/ { diff --git a/tsukanova_irina_lab_3/requirements.txt b/tsukanova_irina_lab_3/requirements.txt new file mode 100644 index 0000000..41917d8 Binary files /dev/null and b/tsukanova_irina_lab_3/requirements.txt differ