示例#1
0
async def create_new_order(
        *,
        db: Session = Depends(get_db),
        data: CreateOrder,
        current_user: User = Depends(get_current_user),
):
    if shop := shop_by_id(db, shop_id=data.shop_id):
        if get_address_by_id(db, id=data.address_id, user_id=current_user.id):
            if owner := shop.owner:
                ordered_item_ids = [
                    item.item_id for item in data.ordered_items
                ]
                items = item_by_ids_and_shop(db,
                                             shop_id=data.shop_id,
                                             ids=ordered_item_ids)
                if items and len(items) == len(ordered_item_ids):
                    with expected_integrity_error(
                            db, detail="Error. Cannot create order.",
                            debug=False):
                        if order := create_order(db,
                                                 data=data,
                                                 user=current_user):
                            ordered_items = add_ordered_items(
                                db,
                                order=order,
                                ordered_items=data.ordered_items,
                                db_items=items,
                            )
                            order.ordered_items = ordered_items
                            # TODO: Send order confirmation email to user
                            # and new order email to owner of the shop
                        return order
                raise HTTPException(
                    status_code=400,
                    detail="Some item(s) does not exists in this shop.")
示例#2
0
async def add_item_in_cart(
        *,
        db: Session = Depends(get_db),
        data: CartItem,
        current_user: User = Depends(get_current_user),
):
    """Authenticated user can add items to his/her cart
    and each item added should be from the same shop.

    Status code = 400 and error detail tag = `CONSTRAINT_VIOLATION`.
        Error will occur when a user tries to add item which already exist
        or when the item is from a different shop as compared to the items
        which already exist in the cart.

    Status code = 400 and error detail tag = `NOT_AVAILABLE`.
        Error will occur when the item or the concerned shop is not available.
    """

    if shop_and_item := get_available_item(db, item_id=data.item_id):
        item, shop = shop_and_item
        with expected_integrity_error(
                db,
                detail=
                "CONSTRAINT_VIOLATION: Item already exist in your cart or you are trying to add item from different shops",
                status_code=status.HTTP_400_BAD_REQUEST,
                debug=False,
        ):
            add_cart_item(db,
                          user_id=current_user.id,
                          shop_id=shop.id,
                          item_id=item.id)
            cart_info: ViewCartResponse = get_cart_items_detailed(
                db, user_id=current_user.id)
            return cart_info
示例#3
0
async def register_user(*,
                        db: Session = Depends(get_db),
                        data: UserCreate,
                        background_tasks: BackgroundTasks):
    """registering new users."""
    otp = generate_random_otp()
    with expected_integrity_error(
            db, detail="There was a conflict with an existing user.",
            debug=False):
        if user := create_user(db, user_in=data, otp=otp):
            background_tasks.add_task(send_email_verify_otp, user.email,
                                      user.otp)
示例#4
0
async def update_owner(
    *,
    db: Session = Depends(get_db),
    data: OwnerUpdate,
    current_owner: DBOwnerModel = Depends(get_current_owner),
    background_tasks: BackgroundTasks,
):
    """If new `email` is found in data, email will no longer be
    verified and new OTP as well as OTP datetime will be generated.
    """
    otp = None
    if data.email and data.email != current_owner.email:
        otp = generate_random_otp()
    with expected_integrity_error(
        db, detail="There was a conflict with an existing owner", debug=False
    ):
        if owner := update_owner_info(db, data=data, owner=current_owner, otp=otp):
            if otp and owner.email:
                background_tasks.add_task(send_email_verify_otp, owner.email, otp)