/// Find order corresponding to given user_id and order_id.
Find order corresponding to given user_id and order_id.
Find order corresponding to given user_id and order_id .
pub fn find_order_by_id(user_id_arg: Uuid, oid: Uuid, conn: &PgConnection) -> Result<OrderDetails, StatusCode> {
    // It is common when using Diesel with Actix web to import schema-related
    // modules inside a function's scope (rather than the normal module's scope)
    // to prevent import collisions and namespace pollution.
    use crate::schema::orders::dsl::*;
    use crate::schema::order_items::dsl::*;
    let vec: Vec<(Order, OrderItem)> = orders
        .inner_join(order_items)
        // Only allow to access user’s own order not others
        .filter(crate::schema::orders::dsl::user_id.eq(user_id_arg))
        .filter(crate::schema::orders::dsl::order_id.eq(oid))
        .into_boxed()
        .get_results(conn)
        .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
    // If vec is empty. Then return not found error.
    if vec.len() == 0 {
        return Err(StatusCode::NOT_FOUND);
    }
    // Order is common for all tuples in vector. Hence taking first one.
    let order = vec[0].0.clone();
    let mut ret_value: OrderDetails = OrderDetails {
        order_id: order.order_id,
        user_id: order.user_id,
        note: order.note,
        order_total: 0,
        order_at: order.created_at,
        // Mark items as None initially. This will be set to below again.
        items: None, //vec![]
    };
    let mut order_total: i64 = 0;
    let mut order_item_details_vec: Vec<OrderItemDetails> = vec![];
    // Iterate over all tuples and calcuate order_total. Also collect orter_items.
    vec.iter().for_each(|tup| {
        let order_item = tup.1.clone();
        order_total = order_total + i64::from(order_item.qty * order_item.price);
        order_item_details_vec.push(OrderItemDetails {
            item_id: order_item.item_id,
            description: order_item.description,
            qty: order_item.qty,
            price: order_item.price,
        });
    });
    ret_value.order_total = order_total;
    ret_value.items = Some(order_item_details_vec);
    Ok(ret_value)
}
pub fn find_order_by_id ( user_id_arg : Uuid , oid : Uuid , conn : & PgConnection ) -> Result < OrderDetails , StatusCode > { // It is common when using Diesel with Actix web to import schema-related // modules inside a function's scope (rather than the normal module's scope) // to prevent import collisions and namespace pollution. use crate :: schema :: orders :: dsl :: * ; use crate :: schema :: order_items :: dsl :: * ; let vec : Vec < ( Order , OrderItem ) > = orders . inner_join ( order_items ) // Only allow to access user’s own order not others . filter ( crate :: schema :: orders :: dsl :: user_id . eq ( user_id_arg ) ) . filter ( crate :: schema :: orders :: dsl :: order_id . eq ( oid ) ) . into_boxed ( ) . get_results ( conn ) . map_err ( | _ | StatusCode :: INTERNAL_SERVER_ERROR ) ? ; // If vec is empty. Then return not found error. if vec . len ( ) == 0 { return Err ( StatusCode :: NOT_FOUND ) ; } // Order is common for all tuples in vector. Hence taking first one. let order = vec [ 0 ] . 0 . clone ( ) ; let mut ret_value : OrderDetails = OrderDetails { order_id : order . order_id , user_id : order . user_id , note : order . note , order_total : 0 , order_at : order . created_at , // Mark items as None initially. This will be set to below again. items : None , //vec![] } ; let mut order_total : i64 = 0 ; let mut order_item_details_vec : Vec < OrderItemDetails > = vec ! [ ] ; // Iterate over all tuples and calcuate order_total. Also collect orter_items. vec . iter ( ) . for_each ( | tup | { let order_item = tup . 1 . clone ( ) ; order_total = order_total + i64 :: from ( order_item . qty * order_item . price ) ; order_item_details_vec . push ( OrderItemDetails { item_id : order_item . item_id , description : order_item . description , qty : order_item . qty , price : order_item . price , } ) ; } ) ; ret_value . order_total = order_total ; ret_value . items = Some ( order_item_details_vec ) ; Ok ( ret_value ) }
Find order corresponding to given user_id and order_id.
Find order corresponding to given user_id and order_id .
user_id_arg
Uuid
oid
Uuid
conn
&PgConnection