fn addExpense(context: &Context, input: AddExpenseInput) -> Result<bool, GraphQLError> {
        let AddExpenseInput {
            group_id,
            person_id,
            name,
            amount,
        } = input;
        // Check input validity
        let group_id = match uuid::Uuid::parse_str(group_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        let person_id = match uuid::Uuid::parse_str(person_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        // Check amount validity
        if amount < 1 {
            return Err(GraphQLError::InvalidAmount);
        }
        // FIXME: Very inefficient quering. Should use joins instead ?
        let viewer = repositories::UserRepository::find_one(context.viewer.id(), &context.db_pool)
            .map_err(GraphQLError::InternalServerError)
            .and_then(|o| match o {
                None => Err(GraphQLError::UserNotFound),
                Some(u) => Ok(u),
            });
        let group = viewer.and_then(|u| {
            repositories::GroupRepository::find_by_user(&u, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|v| v.into_iter().find(|g| g.id == group_id))
                .and_then(|o| match o {
                    None => Err(GraphQLError::GroupNotFound),
                    Some(g) => Ok(g),
                })
        });
        let person = group.and_then(|g| {
            repositories::PersonRepository::find_by_group(&g, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|v| v.into_iter().find(|p| p.id == person_id))
                .and_then(|o| match o {
                    None => Err(GraphQLError::PersonNotFound),
                    Some(p) => Ok(p),
                })
        });
        // Add this expense to the viewer's group if the person exists
        person.and_then(|p| {
            let new_expense = repositories::NewExpense {
                id: uuid::Uuid::new_v4(),
                group_id,
                person_id,
                name,
                amount,
            };
            repositories::ExpenseRepository::save(&new_expense, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|_| true)
        })
    }
// FIXME: Extract domain and repository logic to own module
/// Adds an expense to the specified group.
/// This is a user context dependant mutation.
Extract domain and repository logic to own module
Adds an expense to the specified group.
This is a user context dependant mutation.
Extract domain and repository logic to own module Adds an expense to the specified group . This is a user context dependant mutation .
fn addExpense(context: &Context, input: AddExpenseInput) -> Result<bool, GraphQLError> {
        let AddExpenseInput {
            group_id,
            person_id,
            name,
            amount,
        } = input;
        // Check input validity
        let group_id = match uuid::Uuid::parse_str(group_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        let person_id = match uuid::Uuid::parse_str(person_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        // Check amount validity
        if amount < 1 {
            return Err(GraphQLError::InvalidAmount);
        }
        // FIXME: Very inefficient quering. Should use joins instead ?
        let viewer = repositories::UserRepository::find_one(context.viewer.id(), &context.db_pool)
            .map_err(GraphQLError::InternalServerError)
            .and_then(|o| match o {
                None => Err(GraphQLError::UserNotFound),
                Some(u) => Ok(u),
            });
        let group = viewer.and_then(|u| {
            repositories::GroupRepository::find_by_user(&u, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|v| v.into_iter().find(|g| g.id == group_id))
                .and_then(|o| match o {
                    None => Err(GraphQLError::GroupNotFound),
                    Some(g) => Ok(g),
                })
        });
        let person = group.and_then(|g| {
            repositories::PersonRepository::find_by_group(&g, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|v| v.into_iter().find(|p| p.id == person_id))
                .and_then(|o| match o {
                    None => Err(GraphQLError::PersonNotFound),
                    Some(p) => Ok(p),
                })
        });
        // Add this expense to the viewer's group if the person exists
        person.and_then(|p| {
            let new_expense = repositories::NewExpense {
                id: uuid::Uuid::new_v4(),
                group_id,
                person_id,
                name,
                amount,
            };
            repositories::ExpenseRepository::save(&new_expense, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|_| true)
        })
    }
fn addExpense ( context : & Context , input : AddExpenseInput ) -> Result < bool , GraphQLError > { let AddExpenseInput { group_id , person_id , name , amount , } = input ; // Check input validity let group_id = match uuid :: Uuid :: parse_str ( group_id . as_str ( ) ) { Err ( e ) => return Err ( GraphQLError :: InvalidId ) , Ok ( u ) => u , } ; let person_id = match uuid :: Uuid :: parse_str ( person_id . as_str ( ) ) { Err ( e ) => return Err ( GraphQLError :: InvalidId ) , Ok ( u ) => u , } ; // Check amount validity if amount < 1 { return Err ( GraphQLError :: InvalidAmount ) ; } // FIXME: Very inefficient quering. Should use joins instead ? let viewer = repositories :: UserRepository :: find_one ( context . viewer . id ( ) , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . and_then ( | o | match o { None => Err ( GraphQLError :: UserNotFound ) , Some ( u ) => Ok ( u ) , } ) ; let group = viewer . and_then ( | u | { repositories :: GroupRepository :: find_by_user ( & u , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . map ( | v | v . into_iter ( ) . find ( | g | g . id == group_id ) ) . and_then ( | o | match o { None => Err ( GraphQLError :: GroupNotFound ) , Some ( g ) => Ok ( g ) , } ) } ) ; let person = group . and_then ( | g | { repositories :: PersonRepository :: find_by_group ( & g , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . map ( | v | v . into_iter ( ) . find ( | p | p . id == person_id ) ) . and_then ( | o | match o { None => Err ( GraphQLError :: PersonNotFound ) , Some ( p ) => Ok ( p ) , } ) } ) ; // Add this expense to the viewer's group if the person exists person . and_then ( | p | { let new_expense = repositories :: NewExpense { id : uuid :: Uuid :: new_v4 ( ) , group_id , person_id , name , amount , } ; repositories :: ExpenseRepository :: save ( & new_expense , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . map ( | _ | true ) } ) }
FIXME: Extract domain and repository logic to own module
Adds an expense to the specified group.
FIXME : Extract domain and repository logic to own module Adds an expense to the specified group .
context
&Context
input
AddExpenseInput
context
&Context
input
AddExpenseInput