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