1111from werkzeug .exceptions import HTTPException
1212
1313from commons .data_access_layer .database import CRUDDao
14- from time_tracker_api .security import current_user_tenant_id , current_user_id
14+ from time_tracker_api .security import current_user_tenant_id
1515
1616
1717class CosmosDBFacade :
@@ -105,10 +105,26 @@ def create_sql_condition_for_visibility(visible_only: bool, container_name='c')
105105 return ''
106106
107107 @staticmethod
108- def create_sql_condition_for_owner_id (owner_id : str , container_name = 'c' ) -> str :
109- if owner_id :
110- return 'AND %s.owner_id=@owner_id' % container_name
111- return ''
108+ def create_sql_where_conditions (conditions : dict , container_name = 'c' ) -> str :
109+ where_conditions = []
110+ for k in conditions .keys ():
111+ where_conditions .append ('{c}.{var} = @{var}' .format (c = container_name , var = k ))
112+
113+ if len (where_conditions ) > 0 :
114+ return "AND {where_conditions_clause}" .format (
115+ where_conditions_clause = " AND " .join (where_conditions ))
116+ else :
117+ return ""
118+
119+ @staticmethod
120+ def append_conditions_values (params : list , conditions : dict ) -> dict :
121+ for k , v in conditions .items ():
122+ params .append ({
123+ "name" : "@%s" % k ,
124+ "value" : v
125+ })
126+
127+ return params
112128
113129 @staticmethod
114130 def check_visibility (item , throw_not_found_if_deleted ):
@@ -123,39 +139,41 @@ def create(self, data: dict, mapper: Callable = None):
123139 function_mapper = self .get_mapper_or_dict (mapper )
124140 return function_mapper (self .container .create_item (body = data ))
125141
126- def find (self , id : str , partition_key_value , visible_only = True , mapper : Callable = None ):
142+ def find (self , id : str , partition_key_value , peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
127143 found_item = self .container .read_item (id , partition_key_value )
144+ if peeker :
145+ peeker (found_item )
146+
128147 function_mapper = self .get_mapper_or_dict (mapper )
129148 return function_mapper (self .check_visibility (found_item , visible_only ))
130149
131- def find_all (self , partition_key_value : str , owner_id = None , max_count = None , offset = 0 ,
150+ def find_all (self , partition_key_value : str , conditions : dict = {} , max_count = None , offset = 0 ,
132151 visible_only = True , mapper : Callable = None ):
133152 # TODO Use the tenant_id param and change container alias
134153 max_count = self .get_page_size_or (max_count )
135- result = self .container .query_items (
136- query = """
154+ params = self .append_conditions_values ([
155+ {"name" : "@partition_key_value" , "value" : partition_key_value },
156+ {"name" : "@offset" , "value" : offset },
157+ {"name" : "@max_count" , "value" : max_count },
158+ ], conditions )
159+ result = self .container .query_items (query = """
137160 SELECT * FROM c WHERE c.{partition_key_attribute}=@partition_key_value
138- {owner_condition } {visibility_condition} {order_clause}
161+ {conditions_clause } {visibility_condition} {order_clause}
139162 OFFSET @offset LIMIT @max_count
140163 """ .format (partition_key_attribute = self .partition_key_attribute ,
141164 visibility_condition = self .create_sql_condition_for_visibility (visible_only ),
142- owner_condition = self .create_sql_condition_for_owner_id ( owner_id ),
165+ conditions_clause = self .create_sql_where_conditions ( conditions ),
143166 order_clause = self .create_sql_order_clause ()),
144- parameters = [
145- {"name" : "@partition_key_value" , "value" : partition_key_value },
146- {"name" : "@offset" , "value" : offset },
147- {"name" : "@max_count" , "value" : max_count },
148- {"name" : "@owner_id" , "value" : owner_id },
149- ],
150- partition_key = partition_key_value ,
151- max_item_count = max_count )
167+ parameters = params ,
168+ partition_key = partition_key_value ,
169+ max_item_count = max_count )
152170
153171 function_mapper = self .get_mapper_or_dict (mapper )
154172 return list (map (function_mapper , result ))
155173
156174 def partial_update (self , id : str , changes : dict , partition_key_value : str ,
157- visible_only = True , mapper : Callable = None ):
158- item_data = self .find (id , partition_key_value , visible_only = visible_only , mapper = dict )
175+ peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
176+ item_data = self .find (id , partition_key_value , peeker = peeker , visible_only = visible_only , mapper = dict )
159177 item_data .update (changes )
160178 return self .update (id , item_data , mapper = mapper )
161179
@@ -164,10 +182,11 @@ def update(self, id: str, item_data: dict, mapper: Callable = None):
164182 function_mapper = self .get_mapper_or_dict (mapper )
165183 return function_mapper (self .container .replace_item (id , body = item_data ))
166184
167- def delete (self , id : str , partition_key_value : str , mapper : Callable = None ):
185+ def delete (self , id : str , partition_key_value : str ,
186+ peeker : 'function' = None , mapper : Callable = None ):
168187 return self .partial_update (id , {
169188 'deleted' : str (uuid .uuid4 ())
170- }, partition_key_value , visible_only = True , mapper = mapper )
189+ }, partition_key_value , peeker = peeker , visible_only = True , mapper = mapper )
171190
172191 def delete_permanently (self , id : str , partition_key_value : str ) -> None :
173192 self .container .delete_item (id , partition_key_value )
@@ -190,30 +209,21 @@ def on_update(self, update_item_data: dict):
190209 def create_sql_order_clause (self ):
191210 if len (self .order_fields ) > 0 :
192211 return "ORDER BY c.{}" .format (", c." .join (self .order_fields ))
193- else :
194- return ""
212+ return ""
195213
196214
197215class CosmosDBDao (CRUDDao ):
198216 def __init__ (self , repository : CosmosDBRepository ):
199217 self .repository = repository
200218
201- @property
202- def partition_key_value (self ):
203- return current_user_tenant_id ()
204-
205219 def get_all (self ) -> list :
206- tenant_id : str = self .partition_key_value
207- owner_id = current_user_id ()
208- return self .repository .find_all (partition_key_value = tenant_id , owner_id = owner_id )
220+ return self .repository .find_all (partition_key_value = self .partition_key_value )
209221
210222 def get (self , id ):
211- tenant_id : str = self .partition_key_value
212- return self .repository .find (id , partition_key_value = tenant_id )
223+ return self .repository .find (id , partition_key_value = self .partition_key_value )
213224
214225 def create (self , data : dict ):
215226 data [self .repository .partition_key_attribute ] = self .partition_key_value
216- data ['owner_id' ] = current_user_id ()
217227 return self .repository .create (data )
218228
219229 def update (self , id , data : dict ):
@@ -222,8 +232,11 @@ def update(self, id, data: dict):
222232 partition_key_value = self .partition_key_value )
223233
224234 def delete (self , id ):
225- tenant_id : str = current_user_tenant_id ()
226- self .repository .delete (id , partition_key_value = tenant_id )
235+ self .repository .delete (id , partition_key_value = self .partition_key_value )
236+
237+ @property
238+ def partition_key_value (self ):
239+ return current_user_tenant_id ()
227240
228241
229242class CustomError (HTTPException ):
0 commit comments