1- from typing import Any , Dict , List , Optional , Type , TypedDict , Union
1+ from __future__ import annotations
2+
3+ from typing import (
4+ TYPE_CHECKING ,
5+ Any ,
6+ Dict ,
7+ List ,
8+ Optional ,
9+ TypedDict ,
10+ Union ,
11+ )
212
313from starlette .requests import Request
414
515from fastapi_jsonapi import RoutersJSONAPI
6- from fastapi_jsonapi .atomic .prepared_atomic_operation import PreparedOperation
16+ from fastapi_jsonapi .atomic .prepared_atomic_operation import OperationBase
717from fastapi_jsonapi .atomic .schemas import AtomicOperationRequest , AtomicResultResponse
8- from fastapi_jsonapi .data_layers .base import BaseDataLayer
918from fastapi_jsonapi .utils .dependency_helper import DependencyHelper
10- from fastapi_jsonapi .views .detail_view import DetailViewBase
11- from fastapi_jsonapi .views .list_view import ListViewBase
1219from fastapi_jsonapi .views .utils import HTTPMethodConfig
13- from fastapi_jsonapi .views .view_base import ViewBase
20+
21+ if TYPE_CHECKING :
22+ from fastapi_jsonapi .data_layers .base import BaseDataLayer
1423
1524AtomicResponseDict = TypedDict ("AtomicResponseDict" , {"atomic:results" : List [Any ]})
1625
@@ -26,7 +35,6 @@ def __init__(
2635
2736 async def handle_view_dependencies (
2837 self ,
29- request : Request ,
3038 jsonapi : RoutersJSONAPI ,
3139 ) -> Dict [str , Any ]:
3240 method_config : HTTPMethodConfig = jsonapi .get_method_config_for_create ()
@@ -39,30 +47,24 @@ def handle_dependencies(**dep_kwargs):
3947 method_config = method_config ,
4048 )
4149
42- dependencies_result : Dict [str , Any ] = await DependencyHelper (request = request ).run (handle_dependencies )
50+ dependencies_result : Dict [str , Any ] = await DependencyHelper (request = self . request ).run (handle_dependencies )
4351 return dependencies_result
4452
45- async def prepare_operations (self ) -> List [PreparedOperation ]:
46- prepared_operations : List [PreparedOperation ] = []
53+ async def prepare_operations (self ) -> List [OperationBase ]:
54+ prepared_operations : List [OperationBase ] = []
4755
4856 for operation in self .operations_request .operations :
4957 jsonapi = RoutersJSONAPI .all_jsonapi_routers [operation .data .type ]
50- view_cls : Type ["ViewBase" ] = jsonapi .detail_view_resource
51- if operation .op == "add" :
52- view_cls = jsonapi .list_view_resource
53- view = view_cls (request = self .request , jsonapi = jsonapi )
58+
5459 dependencies_result : Dict [str , Any ] = await self .handle_view_dependencies (
55- request = self .request ,
5660 jsonapi = jsonapi ,
5761 )
58- dl : "BaseDataLayer" = await view .get_data_layer (dependencies_result )
59-
60- one_operation = PreparedOperation (
62+ one_operation = OperationBase .prepare (
6163 action = operation .op ,
62- data_layer = dl ,
63- view = view ,
64+ request = self .request ,
6465 jsonapi = jsonapi ,
6566 data = operation .data ,
67+ data_layer_view_dependencies = dependencies_result ,
6668 )
6769 prepared_operations .append (one_operation )
6870
@@ -74,42 +76,17 @@ async def handle(self) -> Union[AtomicResponseDict, AtomicResultResponse]:
7476
7577 # TODO: try/except, catch schema ValidationError
7678
77- previous_dl : Optional ["BaseDataLayer" ] = None
79+ success = True
80+ previous_dl : Optional [BaseDataLayer ] = None
7881 for operation in prepared_operations :
79- dl = operation .data_layer
82+ dl : BaseDataLayer = await operation .get_data_layer ()
8083 await dl .atomic_start (previous_dl = previous_dl )
8184 previous_dl = dl
82- if operation .action == "add" :
83- data_in = operation .jsonapi .schema_in_post (data = operation .data )
84- assert isinstance (operation .view , ListViewBase )
85- view : "ListViewBase" = operation .view
86- response = await view .process_create_object (dl = operation .data_layer , data_create = data_in .data )
87- # response.data.id
88- results .append ({"data" : response .data })
89- elif operation .action == "update" :
90- data_in = operation .jsonapi .schema_in_patch (data = operation .data )
91- assert isinstance (operation .view , DetailViewBase )
92- view : "DetailViewBase" = operation .view
93- response = await view .process_update_object (
94- dl = dl ,
95- obj_id = data_in .data .id ,
96- data_update = data_in .data ,
97- )
98- # response.data.id
99- results .append ({"data" : response .data })
100- elif operation .action == "remove" :
101- assert isinstance (operation .view , DetailViewBase )
102- view : "DetailViewBase" = operation .view
103- response = await view .process_delete_object (
104- dl = dl ,
105- obj_id = operation .data .id ,
106- )
107- results .append ({"data" : response .data })
108- else :
109- msg = f"unknown action { operation .action !r} "
110- raise ValueError (msg )
85+ response = await operation .handle (dl = dl )
86+ # response.data.id
87+ results .append ({"data" : response .data })
11188
11289 if previous_dl :
113- await previous_dl .atomic_end (success = True )
90+ await previous_dl .atomic_end (success = success )
11491
11592 return {"atomic:results" : results }
0 commit comments