generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
20 changed files
with
368 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import List, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from src.core.entities.category.category import Category, CategoryInDB, CategoryCreateDB | ||
from src.core.entities.order.order import Order, OrderInDB, OrderCreateDB | ||
from src.core.entities.review.review import Review, ReviewInDB, ReviewCreateDB | ||
from src.core.entities.tool.tool import Tool, ToolInDB, ToolCreateDB | ||
from src.core.entities.type.type import Type, TypeInDB, TypeCreateDB | ||
from src.core.entities.users.client.client import Client, ClientInDB, ClientCreateDB | ||
from src.core.entities.users.worker.worker import Worker, WorkerInDB, WorkerCreateDB | ||
|
||
|
||
class DBModel(BaseModel): | ||
workers: Optional[List[WorkerInDB]] = Field( | ||
... | ||
) | ||
clients: Optional[List[ClientInDB]] = Field( | ||
..., | ||
) | ||
tools: Optional[List[ToolInDB]] = Field( | ||
..., | ||
) | ||
orders: Optional[List[OrderInDB]] = Field( | ||
..., | ||
) | ||
categories: Optional[List[CategoryInDB]] = Field( | ||
..., | ||
) | ||
types: Optional[List[TypeInDB]] = Field( | ||
..., | ||
) | ||
reviews: Optional[List[ReviewInDB]] = Field( | ||
..., | ||
) | ||
|
||
class DBModelCreate(BaseModel): | ||
workers: Optional[List[WorkerCreateDB]] = Field( | ||
... | ||
) | ||
clients: Optional[List[ClientCreateDB]] = Field( | ||
..., | ||
) | ||
tools: Optional[List[ToolCreateDB]] = Field( | ||
..., | ||
) | ||
orders: Optional[List[OrderCreateDB]] = Field( | ||
..., | ||
) | ||
categories: Optional[List[CategoryCreateDB]] = Field( | ||
..., | ||
) | ||
types: Optional[List[TypeCreateDB]] = Field( | ||
..., | ||
) | ||
reviews: Optional[List[ReviewCreateDB]] = Field( | ||
..., | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from src.core.entities.db_model.db_model import DBModel, DBModelCreate | ||
|
||
|
||
class IImExRepository(ABC): | ||
@abstractmethod | ||
async def export_data(self) -> DBModel: | ||
pass | ||
|
||
@abstractmethod | ||
async def import_data(self, data: DBModelCreate) -> None: | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from src.core.entities.db_model.db_model import DBModel, DBModelCreate | ||
from src.core.repositories.im_ex_repo.iim_ex_repo import IImExRepository | ||
|
||
|
||
class ImExService: | ||
def __init__(self, | ||
im_ex_repository: IImExRepository, | ||
): | ||
self.im_ex_repository = im_ex_repository | ||
|
||
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) |
31 changes: 31 additions & 0 deletions
31
backend/src/infrastructure/api/import_export_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from fastapi import APIRouter, Depends | ||
from fastapi.security import OAuth2PasswordBearer | ||
|
||
from src.core.entities.db_model.db_model import DBModel, DBModelCreate | ||
from src.core.services.im_ex_service.im_ex_service import ImExService | ||
from src.infrastructure.api.client_controller import oauth2_scheme | ||
from src.infrastructure.api.security.role_required import is_worker | ||
from src.infrastructure.services_instances import get_im_ex_service | ||
|
||
im_ex_router = APIRouter() | ||
|
||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login") | ||
|
||
|
||
@im_ex_router.get(path="/export", status_code=201, response_model=DBModel) | ||
async def export( | ||
im_ex_service: ImExService = Depends(get_im_ex_service), | ||
token: str = Depends(oauth2_scheme), | ||
): | ||
is_worker(token) | ||
return await im_ex_service.export() | ||
|
||
|
||
@im_ex_router.post(path="/import", status_code=201, response_model=None) | ||
async def import_data( | ||
data: DBModelCreate, | ||
im_ex_service: ImExService = Depends(get_im_ex_service), | ||
token: str = Depends(oauth2_scheme), | ||
): | ||
is_worker(token) | ||
return await im_ex_service.import_data(data) |
Empty file.
90 changes: 90 additions & 0 deletions
90
backend/src/infrastructure/repo_implementations/im_ex_repo/im_ex_repo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from motor.motor_asyncio import AsyncIOMotorDatabase | ||
from pymongo.errors import PyMongoError | ||
|
||
from src.core.entities.category.category import Category, CategoryInDB | ||
from src.core.entities.db_model.db_model import DBModel, DBModelCreate | ||
from src.core.entities.order.order import Order, OrderInDB | ||
from src.core.entities.review.review import Review, ReviewInDB | ||
from src.core.entities.tool.tool import Tool, ToolInDB | ||
from src.core.entities.type.type import Type, TypeInDB | ||
from src.core.entities.users.client.client import Client, ClientInDB | ||
from src.core.entities.users.worker.worker import Worker, WorkerInDB | ||
from src.core.exceptions.server_error import DatabaseError | ||
from src.core.repositories.im_ex_repo.iim_ex_repo import IImExRepository | ||
from src.infrastructure.repo_implementations.helpers.id_mapper import str_to_objectId | ||
|
||
|
||
class MongoImExRepository(IImExRepository): | ||
def __init__( | ||
self, | ||
db: AsyncIOMotorDatabase, | ||
order_collection: str, | ||
tool_collection: str, | ||
client_collection: str, | ||
worker_collection: str, | ||
review_collection: str, | ||
category_collection: str, | ||
type_collection: str): | ||
self.order_collection = db[order_collection] | ||
self.tool_collection = db[tool_collection] | ||
self.client_collection = db[client_collection] | ||
self.worker_collection = db[worker_collection] | ||
self.review_collection = db[review_collection] | ||
self.category_collection = db[category_collection] | ||
self.type_collection = db[type_collection] | ||
|
||
async def export_data(self) -> DBModel: | ||
try: | ||
workers = [WorkerInDB(**worker) for worker in (await self.worker_collection.find().to_list(None))] | ||
clients = [ClientInDB(**client) for client in (await self.client_collection.find().to_list(None))] | ||
tools = [ToolInDB(**tool) for tool in (await self.tool_collection.find().to_list(None))] | ||
orders = [OrderInDB(**order) for order in (await self.order_collection.find().to_list(None))] | ||
categories = [CategoryInDB(**category) for category in (await self.category_collection.find().to_list(None))] | ||
types = [TypeInDB(**type) for type in (await self.type_collection.find().to_list(None))] | ||
reviews = [ReviewInDB(**review)for review in (await self.review_collection.find().to_list(None))] | ||
|
||
return DBModel( | ||
workers=workers, | ||
clients=clients, | ||
tools=tools, | ||
orders=orders, | ||
categories=categories, | ||
types=types, | ||
reviews=reviews | ||
) | ||
except PyMongoError: | ||
raise DatabaseError() | ||
|
||
async def import_data(self, data: DBModelCreate) -> None: | ||
try: | ||
if data.workers: | ||
await self.worker_collection.delete_many({}) | ||
await self.worker_collection.insert_many([worker.model_dump() for worker in data.workers]) | ||
|
||
if data.clients: | ||
await self.client_collection.delete_many({}) | ||
await self.client_collection.insert_many([client.model_dump() for client in data.clients]) | ||
|
||
if data.tools: | ||
await self.tool_collection.delete_many({}) | ||
await self.tool_collection.insert_many([tool.model_dump() for tool in data.tools]) | ||
|
||
if data.orders: | ||
await self.order_collection.delete_many({}) | ||
await self.order_collection.insert_many([order.model_dump() for order in data.orders]) | ||
|
||
if data.categories: | ||
await self.category_collection.delete_many({}) | ||
await self.category_collection.insert_many([category.model_dump() for category in data.categories]) | ||
|
||
if data.types: | ||
await self.type_collection.delete_many({}) | ||
await self.type_collection.insert_many([typee.model_dump() for typee in data.types]) | ||
|
||
if data.reviews: | ||
await self.review_collection.delete_many({}) | ||
await self.review_collection.insert_many([review.model_dump() for review in data.reviews]) | ||
|
||
except PyMongoError: | ||
raise DatabaseError() | ||
|
Oops, something went wrong.