<filename>apps/my_app/handlers.py
import datetime

import fastapi
import pymongo
import pymongo.errors
import pymongo.results

from apps.common.enums import CodeAudiences
from apps.common.handlers import PasswordsHandler, TokensHandler

from fastapi_mongodb.exceptions import HandlerException, RepositoryException
from fastapi_mongodb.handlers import BaseHandler, mongo_duplicate_key_error_handler
from fastapi_mongodb.pagination import Paginator
from fastapi_mongodb.projectors import BaseProjector
from fastapi_mongodb.repositories import BaseRepositoryConfig
from fastapi_mongodb.sorting import SortBuilder
from fastapi_mongodb.my_types import OID


from apps.users.models import UserModel
from apps.users.repositories import UserRepository
from apps.users.schemas import JWTPayloadSchema, JWTRefreshSchema, UserCreateSchema, UserLoginSchema, UserUpdateSchema


from apps.my_app.models import DeviceModel
from apps.my_app.repositories import DeviceRepository
from apps.my_app.schemas import DeviceCreateSchema

__all__ = ["DeviceHandler"]


class DeviceHandler(BaseHandler):
    def __init__(self, request: fastapi.Request):
        super().__init__(request=request)
        self.device_repository = DeviceRepository()

    async def create_device(self, request: fastapi.Request, device: DeviceCreateSchema) -> dict:
        """Create new device"""
        device_model = DeviceModel(**device.dict(exclude_unset=True))
        
        try:
            result: pymongo.results.InsertOneResult = await self.device_repository.insert_one(
                document=device_model.to_db(),
                session=request.state.db_session,
            )
        except pymongo.errors.DuplicateKeyError as error:
            mongo_duplicate_key_error_handler(model_name="Device", fields=["name"], error=error)
        else:
            return {"acknowledged": result.acknowledged, "inserted_id": result.inserted_id}
    
        #return {"acknowledged": "True", "inserted_id": "000000000000000000000000"}






    
<filename>apps/my_app/handlers.py
import datetime

import fastapi
import pymongo
import pymongo.errors
import pymongo.results

from apps.common.enums import CodeAudiences
from apps.common.handlers import PasswordsHandler, TokensHandler

from fastapi_mongodb.exceptions import HandlerException, RepositoryException
from fastapi_mongodb.handlers import BaseHandler, mongo_duplicate_key_error_handler
from fastapi_mongodb.pagination import Paginator
from fastapi_mongodb.projectors import BaseProjector
from fastapi_mongodb.repositories import BaseRepositoryConfig
from fastapi_mongodb.sorting import SortBuilder
from fastapi_mongodb.my_types import OID


from apps.users.models import UserModel
from apps.users.repositories import UserRepository
from apps.users.schemas import JWTPayloadSchema, JWTRefreshSchema, UserCreateSchema, UserLoginSchema, UserUpdateSchema


from apps.my_app.models import DeviceModel
from apps.my_app.repositories import DeviceRepository
from apps.my_app.schemas import DeviceCreateSchema

__all__ = ["DeviceHandler"]


class DeviceHandler(BaseHandler):
    def __init__(self, request: fastapi.Request):
        super().__init__(request=request)
        self.device_repository = DeviceRepository()

    async def create_device(self, request: fastapi.Request, device: DeviceCreateSchema) -> dict:
        """Create new device"""
        device_model = DeviceModel(**device.dict(exclude_unset=True))
        
        try:
            result: pymongo.results.InsertOneResult = await self.device_repository.insert_one(
                document=device_model.to_db(),
                session=request.state.db_session,
            )
        except pymongo.errors.DuplicateKeyError as error:
            mongo_duplicate_key_error_handler(model_name="Device", fields=["name"], error=error)
        else:
            return {"acknowledged": result.acknowledged, "inserted_id": result.inserted_id}
    
        #return {"acknowledged": "True", "inserted_id": "000000000000000000000000"}






    
Create new device #return {"acknowledged": "True", "inserted_id": "000000000000000000000000"}