From 890263c586197b54100515f20aae10f38e3fddc9 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Fri, 5 Mar 2021 17:58:25 -0500 Subject: [PATCH] Working on the basic SQL select API CRUD --- app/routers/api_crud.py | 51 ++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/app/routers/api_crud.py b/app/routers/api_crud.py index 72ed8d9..a1f4f33 100644 --- a/app/routers/api_crud.py +++ b/app/routers/api_crud.py @@ -13,6 +13,20 @@ from app.db import * from .user_model import * from .response_model import * + +obj_l1_type_li = {} +obj_l1_type_li['account'] = 'v_account' +obj_l1_type_li['address'] = 'v_address' +obj_l1_type_li['archive'] = 'v_archive' +obj_l1_type_li['contact'] = 'v_contact' +obj_l1_type_li['event'] = 'v_event' +obj_l1_type_li['order'] = 'v_order' +obj_l1_type_li['organization'] = 'v_organization' +obj_l1_type_li['person'] = 'v_person' +obj_l1_type_li['site'] = 'v_site' +obj_l1_type_li['user'] = 'v_user' + + router = APIRouter() @@ -37,10 +51,11 @@ async def get_obj_li(object_l1: str=None, object_l2: str=None, object_l3: str=No return response_data -@router.get('/{object_l1}/{object_id}') -@router.get('/{object_l1}/{object_l2}/{object_id}') -@router.get('/{object_l1}/{object_l2}/{object_l3}/{object_id}') -async def get_obj(object_l1: str=None, object_l2: str=None, object_l3: str=None, object_id: str=None, x_account_id: str = Header(...), +#@router.get('/{obj_type_l1}/{object_id_int}') +@router.get('/{obj_type_l1}/{object_id}') +@router.get('/{obj_type_l1}/{object_l2}/{object_id}') +@router.get('/{obj_type_l1}/{object_l2}/{object_l3}/{object_id}') +async def get_obj(obj_type_l1: str=None, object_l2: str=None, object_l3: str=None, object_id: str=None, x_account_id: str = Header(...), qry_str: Optional[str] = Query(None, max_length=50), qry_int: Optional[int] = None, by_alias: Optional[bool] = True, @@ -49,16 +64,26 @@ async def get_obj(object_l1: str=None, object_l2: str=None, object_l3: str=None, log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) - log.debug(by_alias) - log.debug(exclude_unset) - log.debug(qry_str) - log.debug(qry_int) + debug_data = {} + debug_data['obj_type_l1'] = obj_type_l1 + debug_data['object_l2'] = object_l2 + debug_data['object_l3'] = object_l3 + debug_data['object_id'] = object_id + #debug_data['object_id_int'] = object_id_int + #debug_data['object_id_rand'] = object_id_rand - response_data = {} - response_data['object_l1'] = object_l1 - response_data['object_l2'] = object_l2 - response_data['object_l3'] = object_l3 - response_data['object_id'] = object_id + log.debug(debug_data) + + if obj_type_l1 in obj_l1_type_li: + object_type = obj_l1_type_li[obj_type_l1] + else: + return mk_resp(data=False, status_code=400) + + # NOTE: Add a check for the object ID... assuming it is a random ID string for now. + sql_result = sql_select(table_name=object_type, record_id_random=object_id) + log.debug(sql_result) + resp_data = User_Base(**sql_result).dict(by_alias=by_alias, exclude_unset=exclude_unset) + return mk_resp(data=resp_data)#, details=debug_data) data = {} data['id_random'] = 1