Files
OSIT-AE-API-FastAPI/app/routers/order_cart.py
2021-03-17 20:53:52 +00:00

205 lines
7.6 KiB
Python

import datetime
#from datetime import datetime, time, timedelta
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from ..lib_general import *
from ..log import *
from app.config import settings
from app.db_sql import *
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
from ..models.order_cart_model import Order_Cart_Base
from ..models.order_cart_methods import load_order_cart_obj
from ..models.response_model import *
router = APIRouter()
@router.post('', response_model=Resp_Body_Base)
async def post_order_cart_obj(
obj: Order_Cart_Base,
x_account_id: str = Header(...),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'order_cart'
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
result = post_obj_template(
obj_type=obj_type,
data=obj_data_dict,
return_obj=True,
by_alias=True,
exclude_unset=True,
)
return result
@router.patch('/{obj_id}', response_model=Resp_Body_Base)
async def patch_order_cart_obj(
obj_id: str = Query(..., min_length=1, max_length=22),
obj: Order_Cart_Base = None,
x_account_id: Optional[str] = Header(..., ),
return_obj: Optional[bool] = True,
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'order_cart'
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
obj_data_dict['id_random'] = obj_id
result = patch_obj_template(
obj_type=obj_type,
data=obj_data_dict,
obj_id=obj_id,
return_obj=True,
by_alias=True,
exclude_unset=True,
)
return result
@router.get('/list', response_model=Resp_Body_Base)
async def get_order_cart_obj_li(
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'order_cart'
result = get_obj_li_template(
obj_type=obj_type,
for_obj_type=for_obj_type,
for_obj_id=for_obj_id,
by_alias=True,
exclude_unset=True,
)
return result
# Look up is only for account, order, person, or user records
@router.get('/lookup', response_model=Resp_Body_Base)
async def lookup_order_cart_obj(
response: Response,
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
for_obj_id: Optional[Union[int,str]] = None,
inc_order_cart_line_li: Optional[bool] = True,
inc_order_cart_cfg: Optional[bool] = True,
x_account_id: str = Header(...),
by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'order_cart'
base_name = Order_Cart_Base
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
else: return mk_resp(data=False, status_code=404) # Not Found
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
data = {}
as_list = False
if for_obj_type == 'account' and for_obj_id:
data['account_id'] = for_obj_id
sql_where_for_obj_type = """`order_cart`.account_id = :account_id"""
sql_limit = ''
as_list = True
elif for_obj_type == 'order' and for_obj_id:
data['order_id'] = for_obj_id
sql_where_for_obj_type = """`order_cart`.order_id = :order_id"""
sql_limit = 'LIMIT 1'
elif for_obj_type == 'person' and for_obj_id:
data['person_id'] = for_obj_id
sql_where_for_obj_type = """`order_cart`.person_id = :person_id"""
sql_limit = 'LIMIT 1'
elif for_obj_type == 'user' and for_obj_id:
data['user_id'] = for_obj_id
sql_where_for_obj_type = """`order_cart`.user_id = :user_id"""
sql_limit = 'LIMIT 1'
else:
log.debug(f'Object type={for_obj_type}; Object ID={for_obj_id}')
return mk_resp(data=False, status_code=400) # Bad Request
sql = f"""
SELECT id AS 'order_cart_id', id_random AS 'order_cart_id_random'
FROM `order_cart` AS `order_cart`
WHERE {sql_where_for_obj_type}
{sql_limit}
"""
# This will return a list if selecting by account ID
order_cart_obj_result = sql_select(data=data, sql=sql, as_list=as_list)
if isinstance(order_cart_obj_result, dict):
order_cart_id = order_cart_obj_result.get('order_cart_id', None)
order_cart_id_random = order_cart_obj_result.get('order_cart_id_random', None)
if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
data = order_cart_obj.dict(by_alias=True, exclude_unset=False)
return mk_resp(data=data)
else:
return mk_resp(data=False, status_code=404) # Not Found
elif isinstance(order_cart_obj_result, list):
order_cart_obj_li = []
for order_cart_obj in order_cart_obj_result:
order_cart_id = order_cart_obj.get('order_cart_id', None)
order_cart_obj_li.append(
load_order_cart_obj(
order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg
)
)
data = order_cart_obj_li
return mk_resp(data=data)
else:
log.debug(order_cart_obj_result)
return mk_resp(data=False, status_code=404, response=response) # Not Found
@router.get('/{order_cart_id}', response_model=Resp_Body_Base)
async def get_order_cart_obj(
order_cart_id: str = Query(..., min_length=1, max_length=22),
inc_order_cart_line_li: bool = False,
inc_order_cart_cfg: bool = False,
x_account_id: str = Header(...),
by_alias: bool = True,
exclude_unset: bool = True,
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
data = order_cart_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
return mk_resp(data=data)
else:
return mk_resp(data=False, status_code=404) # Not Found
@router.delete('/{obj_id}', response_model=Resp_Body_Base)
async def delete_order_cart_obj(
obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: str = Header(...),
):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
obj_type = 'order_cart'
result = delete_obj_template(
obj_type=obj_type,
obj_id=obj_id,
)
return result