예제 #1
0
async def post_orders(order_list: Orders):
    db = Session()
    order_ids = []
    order_fail_ids = []
    for order in order_list.data:
        db_order = OrderSchema(order_id=order.order_id,
                               weight=order.weight,
                               region=order.region,
                               delivery_hours=order.delivery_hours)
        db.add(db_order)
        try:
            db.commit()
        except IntegrityError:
            order_fail_ids.append(order.order_id)
        else:
            db.refresh(db_order)
            order_ids.append(order.order_id)

    if len(order_fail_ids) == 0:
        return JSONResponse(
            content={'couriers': [{
                'id': c_id
            } for c_id in order_ids]},
            status_code=status.HTTP_201_CREATED)
    else:
        return JSONResponse(content={
            "validation_error": {
                'couriers': [{
                    'id': c_id
                } for c_id in order_fail_ids]
            }
        },
                            status_code=status.HTTP_400_BAD_REQUEST)
예제 #2
0
async def post_couriers(courier_list: Couriers):
    db = Session()
    courier_ids = []
    courier_fail_ids = []
    for courier in courier_list.data:
        db_courier = CourierSchema(courier_id=courier.courier_id,
                                   courier_type=courier.courier_type,
                                   regions=courier.regions,
                                   working_hours=courier.working_hours)

        db.add(db_courier)
        try:
            db.commit()
        except IntegrityError:
            courier_fail_ids.append(courier.courier_id)
        else:
            db.refresh(db_courier)
            courier_ids.append(courier.courier_id)

    if len(courier_fail_ids) == 0:
        return JSONResponse(
            content={'couriers': [{
                'id': c_id
            } for c_id in courier_ids]},
            status_code=status.HTTP_201_CREATED)
    else:
        return JSONResponse(content={
            "validation_error": {
                'couriers': [{
                    'id': c_id
                } for c_id in courier_fail_ids]
            }
        },
                            status_code=status.HTTP_400_BAD_REQUEST)