forked from Alexey/DAS_2024_1
сервис авторов
This commit is contained in:
parent
37996c249a
commit
a0ef65e0f9
2
tsukanova_irina_lab_3/.gitignore
vendored
Normal file
2
tsukanova_irina_lab_3/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/.venv
|
||||||
|
/.idea
|
20
tsukanova_irina_lab_3/author_service/Dockerfile
Normal file
20
tsukanova_irina_lab_3/author_service/Dockerfile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Использую базовый образ Python
|
||||||
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
# Устанавливаю рабочую директорию внутри контейнера
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Копирую файл requirements.txt в контейнер
|
||||||
|
COPY author_service/requirements.txt .
|
||||||
|
|
||||||
|
# Устанавливаю зависимости
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Копирую все файлы в контейнер
|
||||||
|
COPY author_service/author_service.py .
|
||||||
|
|
||||||
|
## Открываю порт 20001 для Flask
|
||||||
|
#EXPOSE 20001
|
||||||
|
|
||||||
|
# Команда для запуска Python-скрипта
|
||||||
|
CMD ["python", "author_service.py"]
|
88
tsukanova_irina_lab_3/author_service/author_service.py
Normal file
88
tsukanova_irina_lab_3/author_service/author_service.py
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
from flask import Flask, jsonify, request
|
||||||
|
from uuid import uuid1
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
class Author:
|
||||||
|
def __init__(self, name, surname):
|
||||||
|
self.uuid_: uuid = uuid1()
|
||||||
|
self.name: str = name
|
||||||
|
self.surname: str = surname
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {
|
||||||
|
"uuid": self.uuid_,
|
||||||
|
"name": self.name,
|
||||||
|
"surname": self.surname
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
authors: list[Author] = [
|
||||||
|
Author(name='Leon', surname='Kane'),
|
||||||
|
Author(name='James', surname='Rasal'),
|
||||||
|
Author(name='Tess', surname='Root')
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
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('/<uuid:uuid>', methods=['GET'])
|
||||||
|
def get_one(uuid_):
|
||||||
|
for author in authors:
|
||||||
|
if author.uuid_ == uuid_:
|
||||||
|
return author.to_dict(), 200
|
||||||
|
|
||||||
|
return 'Автор с таким uuid не был найден', 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
|
||||||
|
|
||||||
|
new_author = Author(name, surname)
|
||||||
|
authors.append(new_author)
|
||||||
|
return get_one(new_author.uuid_)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/<uuid:uuid>', methods=['PUT'])
|
||||||
|
def update_by_id(uuid_):
|
||||||
|
data = request.json
|
||||||
|
new_name = data.get('name', None)
|
||||||
|
new_surname = data.get('surname', None)
|
||||||
|
|
||||||
|
for author in authors:
|
||||||
|
if author.uuid_ == uuid_:
|
||||||
|
if new_name is not None:
|
||||||
|
author.name = new_name
|
||||||
|
if new_surname is not None:
|
||||||
|
author.surname = new_surname
|
||||||
|
return get_one(author.uuid_)
|
||||||
|
|
||||||
|
return 'Автор с таким uuid не был найден', 404
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/<uuid:uuid>', methods=['DELETE'])
|
||||||
|
def delete(uuid_):
|
||||||
|
for author in authors:
|
||||||
|
if author.uuid_ == uuid_:
|
||||||
|
authors.remove(author)
|
||||||
|
return 200
|
||||||
|
|
||||||
|
return 'Автор с таким uuid не был найден', 404
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5000, debug=True)
|
BIN
tsukanova_irina_lab_3/author_service/requirements.txt
Normal file
BIN
tsukanova_irina_lab_3/author_service/requirements.txt
Normal file
Binary file not shown.
0
tsukanova_irina_lab_3/book_service/book_service.py
Normal file
0
tsukanova_irina_lab_3/book_service/book_service.py
Normal file
BIN
tsukanova_irina_lab_3/book_service/requirements.txt
Normal file
BIN
tsukanova_irina_lab_3/book_service/requirements.txt
Normal file
Binary file not shown.
10
tsukanova_irina_lab_3/docker-compose.yaml
Normal file
10
tsukanova_irina_lab_3/docker-compose.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
|
||||||
|
author_service:
|
||||||
|
container_name: author_service
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: ./author_service/Dockerfile
|
||||||
|
ports:
|
||||||
|
- "20001:5000"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user