gabcares commited on
Commit
5d1b562
·
verified ·
1 Parent(s): 4035319

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +46 -46
main.py CHANGED
@@ -173,57 +173,57 @@ async def endpoint_output(endpoint_result: ResultItem, code: int = 0, error: str
173
  # Caching Post requests is challenging
174
  async def sms_posts(instance: ResultItem, idx: str = None, action: str = "add") -> Union[ErrorResponse, EndpointResponse]:
175
  async with AsyncSession(sms_resource["engine"]) as session:
176
- async with session.begin():
177
- code = 1
178
- error = None
179
- result = None
180
- existing = await session.get(instance.__class__, idx)
181
-
182
- # For add action, do db operation if instance is not existing. Other actions, do db operation if instance exists in db
183
- checker = existing is None if action == "add" else existing is not None
184
-
185
- try:
186
- if checker:
187
- if action == "delete":
188
- await session.delete(existing) # Asynchronous
189
- await session.commit()
190
- else: # add or update use add
191
- session.add(instance) # Not asynchronous
192
- await session.commit()
193
- await session.refresh(instance)
194
- result = instance
195
- except Exception as e:
196
- code = 0
197
- error = str(e)
198
-
199
- finally:
200
- return await endpoint_output(result, code, error)
201
 
202
 
203
  # @cache(expire=ONE_DAY_SEC, namespace='sms_gets') # Cache for 1 day
204
  async def sms_gets(sms_class: Type[Result], action: str = "first", idx: str = None, stmt: SelectOfScalar[Type[Result]] = None) -> Union[ErrorResponse, EndpointResponse]:
205
  async with AsyncSession(sms_resource["engine"]) as session:
206
- async with session.begin():
207
- result = None
208
- error = None
209
- code = 1
210
- try:
211
- if action == "all":
212
- statement = select(sms_class) if stmt is None else stmt
213
- instance_list = (await session.exec(statement)).all()
214
- if instance_list:
215
- result = {
216
- str(instance.id): instance for instance in instance_list}
217
- elif action == "first":
218
- statement = select(sms_class).where(
219
- sms_class.id == idx) if stmt is None else stmt
220
- result = (await session.exec(statement)).first()
221
-
222
- except Exception as e:
223
- code = 0
224
- error = str(e)
225
- finally:
226
- return await endpoint_output(result, code, error)
227
 
228
 
229
  # Student Routes
 
173
  # Caching Post requests is challenging
174
  async def sms_posts(instance: ResultItem, idx: str = None, action: str = "add") -> Union[ErrorResponse, EndpointResponse]:
175
  async with AsyncSession(sms_resource["engine"]) as session:
176
+
177
+ code = 1
178
+ error = None
179
+ result = None
180
+ existing = await session.get(instance.__class__, idx)
181
+
182
+ # For add action, do db operation if instance is not existing. Other actions, do db operation if instance exists in db
183
+ checker = existing is None if action == "add" else existing is not None
184
+
185
+ try:
186
+ if checker:
187
+ if action == "delete":
188
+ await session.delete(existing) # Asynchronous
189
+ await session.commit()
190
+ else: # add or update use add
191
+ session.add(instance) # Not asynchronous
192
+ await session.commit()
193
+ await session.refresh(instance)
194
+ result = instance
195
+ except Exception as e:
196
+ code = 0
197
+ error = str(e)
198
+
199
+ finally:
200
+ return await endpoint_output(result, code, error)
201
 
202
 
203
  # @cache(expire=ONE_DAY_SEC, namespace='sms_gets') # Cache for 1 day
204
  async def sms_gets(sms_class: Type[Result], action: str = "first", idx: str = None, stmt: SelectOfScalar[Type[Result]] = None) -> Union[ErrorResponse, EndpointResponse]:
205
  async with AsyncSession(sms_resource["engine"]) as session:
206
+
207
+ result = None
208
+ error = None
209
+ code = 1
210
+ try:
211
+ if action == "all":
212
+ statement = select(sms_class) if stmt is None else stmt
213
+ instance_list = (await session.exec(statement)).all()
214
+ if instance_list:
215
+ result = {
216
+ str(instance.id): instance for instance in instance_list}
217
+ elif action == "first":
218
+ statement = select(sms_class).where(
219
+ sms_class.id == idx) if stmt is None else stmt
220
+ result = (await session.exec(statement)).first()
221
+
222
+ except Exception as e:
223
+ code = 0
224
+ error = str(e)
225
+ finally:
226
+ return await endpoint_output(result, code, error)
227
 
228
 
229
  # Student Routes