Skip to content

Commit

Permalink
export file added
Browse files Browse the repository at this point in the history
  • Loading branch information
adoomaxas committed Dec 19, 2024
1 parent d41abc0 commit dcd01f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion backend/src/core/services/im_ex_service/im_ex_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from src.core.entities.db_model.db_model import DBModel, DBModelCreate
from src.core.repositories.im_ex_repo.iim_ex_repo import IImExRepository

Expand All @@ -12,4 +14,11 @@ async def export(self) -> DBModel:
return await self.im_ex_repository.export_data()

async def import_data(self, data: DBModelCreate) -> None:
return await self.im_ex_repository.import_data(data)
return await self.im_ex_repository.import_data(data)

async def export_file(self) -> str:
data = await self.im_ex_repository.export_data()
file_path = '/tmp/data.json'
with open(file_path, "w") as file:
json.dump(data.model_dump(), file, default=str)
return file_path
13 changes: 13 additions & 0 deletions backend/src/infrastructure/api/import_export_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends
from fastapi.security import OAuth2PasswordBearer
from fastapi.responses import FileResponse

from src.core.entities.db_model.db_model import DBModel, DBModelCreate
from src.core.services.im_ex_service.im_ex_service import ImExService
Expand All @@ -11,6 +12,18 @@

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")

@im_ex_router.get(path="/export_file", status_code=201)
async def export(
im_ex_service: ImExService = Depends(get_im_ex_service),
token: str = Depends(oauth2_scheme),
):
is_worker(token)
file_path = await im_ex_service.export_file()
return FileResponse(
path=file_path,
filename="data.json",
media_type='application/octet-stream'
)

@im_ex_router.get(path="/export", status_code=201, response_model=DBModel)
async def export(
Expand Down

0 comments on commit dcd01f7

Please sign in to comment.