Skip to content

Commit

Permalink
change di context managing
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 committed May 28, 2024
1 parent 4a0713a commit 2d93689
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 207 deletions.
15 changes: 6 additions & 9 deletions app/api/decks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
from app.repositories.decks import CardsRepository, DecksRepository


ROUTER: typing.Final = fastapi.APIRouter()
async def init_di_context() -> typing.AsyncIterator[None]:
async with container_context():
yield


ROUTER: typing.Final = fastapi.APIRouter(dependencies=[fastapi.Depends(init_di_context)])


@ROUTER.get("/decks/")
@container_context()
async def list_decks(
decks_repo: DecksRepository = fastapi.Depends(ioc.IOCContainer.decks_repo),
) -> schemas.Decks:
Expand All @@ -21,7 +25,6 @@ async def list_decks(


@ROUTER.get("/decks/{deck_id}/")
@container_context()
async def get_deck(
deck_id: int,
decks_repo: DecksRepository = fastapi.Depends(ioc.IOCContainer.decks_repo),
Expand All @@ -34,7 +37,6 @@ async def get_deck(


@ROUTER.put("/decks/{deck_id}/")
@container_context()
async def update_deck(
deck_id: int,
data: schemas.DeckCreate,
Expand All @@ -50,7 +52,6 @@ async def update_deck(


@ROUTER.post("/decks/")
@container_context()
async def create_deck(
data: schemas.DeckCreate,
decks_repo: DecksRepository = fastapi.Depends(ioc.IOCContainer.decks_repo),
Expand All @@ -61,7 +62,6 @@ async def create_deck(


@ROUTER.get("/decks/{deck_id}/cards/")
@container_context()
async def list_cards(
deck_id: int,
cards_repo: CardsRepository = fastapi.Depends(ioc.IOCContainer.cards_repo),
Expand All @@ -71,7 +71,6 @@ async def list_cards(


@ROUTER.get("/cards/{card_id}/")
@container_context()
async def get_card(
card_id: int,
cards_repo: CardsRepository = fastapi.Depends(ioc.IOCContainer.cards_repo),
Expand All @@ -83,7 +82,6 @@ async def get_card(


@ROUTER.post("/decks/{deck_id}/cards/")
@container_context()
async def create_cards(
deck_id: int,
data: list[schemas.CardCreate],
Expand All @@ -96,7 +94,6 @@ async def create_cards(


@ROUTER.put("/decks/{deck_id}/cards/")
@container_context()
async def update_cards(
deck_id: int,
data: list[schemas.Card],
Expand Down
2 changes: 0 additions & 2 deletions app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import typing

import fastapi
from that_depends.providers import DIContextMiddleware

from app import exceptions, ioc
from app.api.decks import ROUTER
Expand All @@ -22,7 +21,6 @@ def __init__(self) -> None:
lifespan=self.lifespan_manager,
)
include_routers(self.app)
self.app.add_middleware(DIContextMiddleware)
self.app.add_exception_handler(
DatabaseValidationError,
exceptions.database_validation_exception_handler, # type: ignore[arg-type]
Expand Down
Loading

0 comments on commit 2d93689

Please sign in to comment.