Exemple #1
0
async def edit_order_status_for_owner(
        *,
        order_id: PositiveInt,
        data: EditOrderStatusForOwner,
        db: Session = Depends(get_db),
        current_owner: Owner = Depends(get_current_owner),
):
    if order := get_order_by_id(db, id=order_id):
        if shop := get_shop_by_id(db,
                                  shop_id=order.shop_id,
                                  owner_id=current_owner.id):
            current_status = order.status.code
            if (data.status == OrderStatusForOwner.accepted
                    or data.status == OrderStatusForOwner.declined):
                if current_status != "pending":
                    raise HTTPException(
                        status_code=400,
                        detail=
                        "Only pending order can be accepted or declined.",
                    )
            else:
                if current_status != "accepted":
                    raise HTTPException(
                        status_code=400,
                        detail="Only accepted order can be delivered.")
            if order := edit_order_status(db,
                                          order=order,
                                          order_status=data.status):
                # TODO: Notify user regarding the new order status through email.
                return EditOrderStatusMessage(status=order.status.value,
                                              message="Order status changed.")
Exemple #2
0
async def owner_update_shop(
        *,
        shop_id: PositiveInt,
        db: Session = Depends(get_db),
        data: ShopUpdate,
        current_owner: DBOwnerModel = Depends(get_current_owner),
):
    if shop := get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id):
        if shop := update_shop(db, shop=shop, data=data):
            return shop
Exemple #3
0
async def owner_update_shop(
    *,
    shop_id: constr(to_lower=True, min_length=8, max_length=100),
    db: Session = Depends(get_db),
    data: ShopUpdate,
    current_owner: DBOwnerModel = Depends(get_current_owner),
):
    if shop := get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id):
        if shop := update_shop(db, shop=shop, data=data):
            return shop
Exemple #4
0
async def get_items_for_owner(
        *,
        shop_id: PositiveInt,
        db: Session = Depends(get_db),
        current_owner: Owner = Depends(get_current_owner),
):
    shop = get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id)
    if shop:
        if items := shop.items:
            return convert_cost_units(items)
        raise HTTPException(status_code=404,
                            detail="This shop does not have any items.")
Exemple #5
0
async def shop_by_id(
        *,
        shop_id: PositiveInt,
        db: Session = Depends(get_db),
        current_owner: DBOwnerModel = Depends(get_current_owner),
):
    shop = get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id)
    if shop:
        return shop
    raise HTTPException(
        status_code=404,
        detail="This owner does not have any shop with this id.")
Exemple #6
0
async def shop_by_id(
    *,
    shop_id: constr(to_lower=True, min_length=8, max_length=100),
    db: Session = Depends(get_db),
    current_owner: DBOwnerModel = Depends(get_current_owner),
):
    shop = get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id)
    if shop:
        return shop
    raise HTTPException(
        status_code=404, detail="This owner does not have any shop with this id."
    )
Exemple #7
0
async def add_item_to_shop(
        *,
        shop_id: PositiveInt,
        db: Session = Depends(get_db),
        data: Item,
        current_owner: Owner = Depends(get_current_owner),
):
    if get_shop_by_id(db, shop_id=shop_id, owner_id=current_owner.id):
        if item_by_name_and_shop(db, shop_id=shop_id, name=data.name):
            # Item name not unique within the shop. Show exception.
            raise HTTPException(status_code=409,
                                detail="Item with this name already exists.")
        return add_item(db,
                        shop_id=shop_id,
                        owner_id=current_owner.id,
                        data=data)
    raise HTTPException(
        status_code=403,
        detail="This owner is not allowed to add items to this shop.")