Working on general PATCH template function.

This commit is contained in:
Scott Idem
2021-03-09 15:27:17 -05:00
parent bc22a8c9d8
commit 291dfc8896
6 changed files with 403 additions and 22 deletions

View File

@@ -9,6 +9,8 @@ from ..log import *
from app.config import settings
from app.db_sql import *
from .api_crud import patch_obj_template
from ..models.address_model import Address_Base
from ..models.response_model import *
@@ -52,3 +54,66 @@ async def post_address_obj(address: Address_Base,
resp_data = base_name(**sql_select_result).dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=resp_data)
@router.patch('/{address_id}')
async def patch_address_obj(
address_id: str,
address: Address_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
table_name_update = 'address'
obj_id = address_id # actually id_random
obj_data_dict = address.dict(by_alias=False, exclude_unset=True)
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=table_name_update)
obj_data_dict['id_random'] = obj_id
result = patch_obj_template(table_name_update=table_name_update,
data=obj_data_dict,
obj_id=obj_id,
return_obj=True,
by_alias=True,
exclude_unset=True
)
return result
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
table_name_update = 'address'
table_name_select = 'v_address'
obj_id = address_id
obj_data_dict = address.dict(by_alias=False, exclude_unset=True)
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=table_name_update)
obj_data_dict['id_random'] = address_id # NOTE: Adding this in so the id_random is NOT updated
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(obj_data_dict)
obj_data = lookup_id_random_pop(obj_data_dict)
base_name = Address_Base
if sql_update_result := sql_update(table_name=table_name_update, data=obj_data):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(sql_update_result)
#obj_id = sql_update_result
obj_id = obj_data_dict['id']
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(sql_update_result)
return mk_resp(data=False, status_code=400)
if sql_select_result := sql_select(table_name=table_name_select, record_id=obj_id):
log.debug(sql_select_result)
resp_data = base_name(**sql_select_result).dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=resp_data)
else:
log.debug(sql_select_result)
return mk_resp(data=False, status_code=404)