11from __future__ import annotations
22
33from dataclasses import dataclass
4- from typing import TYPE_CHECKING , Any , Dict , Type
4+ from typing import TYPE_CHECKING , Any , Dict , Optional , Type
55
66from fastapi import Request
77
88from fastapi_jsonapi import RoutersJSONAPI
9- from fastapi_jsonapi .atomic .schemas import AtomicOperationAction , OperationDataType
9+ from fastapi_jsonapi .atomic .schemas import AtomicOperationAction , AtomicOperationRef , OperationDataType
1010
1111if TYPE_CHECKING :
1212 from fastapi_jsonapi .data_layers .base import BaseDataLayer
1919class OperationBase :
2020 jsonapi : RoutersJSONAPI
2121 view : ViewBase
22+ ref : Optional [AtomicOperationRef ]
2223 data : OperationDataType
2324 data_layer_view_dependencies : Dict [str , Any ]
2425
@@ -28,6 +29,7 @@ def prepare(
2829 action : str ,
2930 request : Request ,
3031 jsonapi : RoutersJSONAPI ,
32+ ref : Optional [AtomicOperationRef ],
3133 data : OperationDataType ,
3234 data_layer_view_dependencies : Dict [str , Any ],
3335 ) -> "OperationBase" :
@@ -49,6 +51,7 @@ def prepare(
4951 return operation_cls (
5052 jsonapi = jsonapi ,
5153 view = view ,
54+ ref = ref ,
5255 data = data ,
5356 data_layer_view_dependencies = data_layer_view_dependencies ,
5457 )
@@ -81,9 +84,10 @@ async def handle(self, dl: BaseDataLayer):
8184class OperationUpdate (DetailOperationBase ):
8285 async def handle (self , dl : BaseDataLayer ):
8386 data_in = self .jsonapi .schema_in_patch (data = self .data )
87+ obj_id = self .ref and self .ref .id or self .data and self .data .id
8488 response = await self .view .process_update_object (
8589 dl = dl ,
86- obj_id = data_in . data . id ,
90+ obj_id = obj_id ,
8791 data_update = data_in .data ,
8892 )
8993 return response
@@ -93,9 +97,19 @@ class OperationRemove(DetailOperationBase):
9397 async def handle (
9498 self ,
9599 dl : BaseDataLayer ,
96- ):
97- response = await self .view .process_delete_object (
100+ ) -> None :
101+ """
102+ Todo: fix atomic delete
103+ Deleting Resources
104+ An operation that deletes a resource
105+ MUST target that resource
106+ through the operation’s ref or href members,
107+ but not both.
108+
109+ :param dl:
110+ :return:
111+ """
112+ await self .view .process_delete_object (
98113 dl = dl ,
99- obj_id = self .data .id ,
114+ obj_id = self .ref and self . ref .id ,
100115 )
101- return response
0 commit comments