1
1
import typing
2
2
3
3
import litestar
4
+ from advanced_alchemy .exceptions import NotFoundError
4
5
from litestar import status_codes
5
6
from litestar .contrib .pydantic import PydanticDTO
6
7
from litestar .exceptions import HTTPException
8
+ from sqlalchemy import orm
7
9
from that_depends .providers import container_context
8
10
9
11
from app import ioc , models , schemas
10
12
11
13
14
+ if typing .TYPE_CHECKING :
15
+ from app .repositories import CardsService , DecksService
16
+
17
+
12
18
@litestar .get ("/decks/" )
13
19
@container_context ()
14
20
async def list_decks () -> schemas .Decks :
15
- decks_repo = await ioc .IOCContainer .decks_repo ()
16
- objects = await decks_repo . all ()
21
+ decks_service : DecksService = await ioc .IOCContainer .decks_service ()
22
+ objects = await decks_service . list ()
17
23
return schemas .Decks (items = objects ) # type: ignore[arg-type]
18
24
19
25
20
26
@litestar .get ("/decks/{deck_id:int}/" )
21
27
@container_context ()
22
28
async def get_deck (deck_id : int ) -> schemas .Deck :
23
- decks_repo = await ioc .IOCContainer .decks_repo ()
24
- instance = await decks_repo .get_by_id (deck_id , prefetch = ("cards" ,))
29
+ decks_service : DecksService = await ioc .IOCContainer .decks_service ()
30
+ instance = await decks_service .get_one_or_none (
31
+ models .Deck .id == deck_id ,
32
+ load = [orm .selectinload (models .Deck .cards )],
33
+ )
25
34
if not instance :
26
35
raise HTTPException (status_code = status_codes .HTTP_404_NOT_FOUND , detail = "Deck is not found" )
27
36
@@ -34,38 +43,35 @@ async def update_deck(
34
43
deck_id : int ,
35
44
data : schemas .DeckCreate ,
36
45
) -> schemas .Deck :
37
- decks_repo = await ioc .IOCContainer .decks_repo ()
38
- instance = await decks_repo .get_by_id (deck_id )
39
- if not instance :
40
- raise HTTPException (status_code = status_codes .HTTP_404_NOT_FOUND , detail = "Deck is not found" )
41
-
42
- await decks_repo .update_attrs (instance , ** data .model_dump ())
43
- await decks_repo .save (instance )
46
+ decks_service : DecksService = await ioc .IOCContainer .decks_service ()
47
+ try :
48
+ instance = await decks_service .update (data = data .model_dump (), item_id = deck_id )
49
+ except NotFoundError :
50
+ raise HTTPException (status_code = status_codes .HTTP_404_NOT_FOUND , detail = "Deck is not found" ) from None
44
51
return schemas .Deck .model_validate (instance )
45
52
46
53
47
54
@litestar .post ("/decks/" )
48
55
@container_context ()
49
56
async def create_deck (data : schemas .DeckCreate ) -> schemas .Deck :
50
- decks_repo = await ioc .IOCContainer .decks_repo ()
51
- instance = models .Deck (** data .model_dump ())
52
- await decks_repo .save (instance )
57
+ decks_service : DecksService = await ioc .IOCContainer .decks_service ()
58
+ instance = await decks_service .create (data )
53
59
return schemas .Deck .model_validate (instance )
54
60
55
61
56
62
@litestar .get ("/decks/{deck_id:int}/cards/" )
57
63
@container_context ()
58
64
async def list_cards (deck_id : int ) -> schemas .Cards :
59
- cards_repo = await ioc .IOCContainer .cards_repo ()
60
- objects = await cards_repo . filter ({ " deck_id" : deck_id } )
65
+ cards_service : CardsService = await ioc .IOCContainer .cards_service ()
66
+ objects = await cards_service . list ( models . Card . deck_id == deck_id )
61
67
return schemas .Cards (items = objects ) # type: ignore[arg-type]
62
68
63
69
64
70
@litestar .get ("/cards/{card_id:int}/" , return_dto = PydanticDTO [schemas .Card ])
65
71
@container_context ()
66
72
async def get_card (card_id : int ) -> schemas .Card :
67
- cards_repo = await ioc .IOCContainer .cards_repo ()
68
- instance = await cards_repo . get_by_id ( card_id )
73
+ cards_service : CardsService = await ioc .IOCContainer .cards_service ()
74
+ instance = await cards_service . get_one_or_none ( models . Card . id == card_id )
69
75
if not instance :
70
76
raise HTTPException (status_code = status_codes .HTTP_404_NOT_FOUND , detail = "Card is not found" )
71
77
return schemas .Card .model_validate (instance )
@@ -77,9 +83,9 @@ async def create_cards(
77
83
deck_id : int ,
78
84
data : list [schemas .CardCreate ],
79
85
) -> schemas .Cards :
80
- cards_repo = await ioc .IOCContainer .cards_repo ()
81
- objects = await cards_repo . bulk_create (
82
- [models .Card (** card .model_dump (), deck_id = deck_id ) for card in data ],
86
+ cards_service : CardsService = await ioc .IOCContainer .cards_service ()
87
+ objects = await cards_service . create_many (
88
+ data = [models .Card (** card .model_dump (), deck_id = deck_id ) for card in data ],
83
89
)
84
90
return schemas .Cards (items = objects ) # type: ignore[arg-type]
85
91
@@ -90,9 +96,9 @@ async def update_cards(
90
96
deck_id : int ,
91
97
data : list [schemas .Card ],
92
98
) -> schemas .Cards :
93
- cards_repo = await ioc .IOCContainer .cards_repo ()
94
- objects = await cards_repo . bulk_update (
95
- [models .Card (** card .model_dump (exclude = {"deck_id" }), deck_id = deck_id ) for card in data ],
99
+ cards_service : CardsService = await ioc .IOCContainer .cards_service ()
100
+ objects = await cards_service . upsert_many (
101
+ data = [models .Card (** card .model_dump (exclude = {"deck_id" }), deck_id = deck_id ) for card in data ],
96
102
)
97
103
return schemas .Cards (items = objects ) # type: ignore[arg-type]
98
104
0 commit comments