fn addPerson(context: &Context, input: AddPersonInput) -> Result<bool, GraphQLError> {
        let AddPersonInput {
            group_id,
            name,
            resources,
        } = input;
        // Check name validity
        if !(1..=50).contains(&name.graphemes(true).count()) {
            return Err(GraphQLError::InvalidName);
        }
        // Check resources validity
        if resources.is_negative() {
            return Err(GraphQLError::InvalidResources);
        }
        // Check id validity
        let group_id = match uuid::Uuid::parse_str(group_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        // 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.name == name))
                // Check name uniqueness
                .and_then(|o| match o {
                    Some(p) => Err(GraphQLError::NonUniqueName(name.clone())),
                    None => Ok(()),
                })
        });
        // Add this person to viewer's group
        person.and_then(|_| {
            let new_person = repositories::NewPerson {
                id: uuid::Uuid::new_v4(),
                group_id,
                name,
                resources,
            };
            repositories::PersonRepository::save(&new_person, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|_| true)
        })
    }
// FIXME: Extract domain and repository logic to own module
/// Adds a person to the specified group.
/// This is a user context dependant mutation.
Extract domain and repository logic to own module
Adds a person to the specified group.
This is a user context dependant mutation.
Extract domain and repository logic to own module Adds a person to the specified group . This is a user context dependant mutation .
fn addPerson(context: &Context, input: AddPersonInput) -> Result<bool, GraphQLError> {
        let AddPersonInput {
            group_id,
            name,
            resources,
        } = input;
        // Check name validity
        if !(1..=50).contains(&name.graphemes(true).count()) {
            return Err(GraphQLError::InvalidName);
        }
        // Check resources validity
        if resources.is_negative() {
            return Err(GraphQLError::InvalidResources);
        }
        // Check id validity
        let group_id = match uuid::Uuid::parse_str(group_id.as_str()) {
            Err(e) => return Err(GraphQLError::InvalidId),
            Ok(u) => u,
        };
        // 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.name == name))
                // Check name uniqueness
                .and_then(|o| match o {
                    Some(p) => Err(GraphQLError::NonUniqueName(name.clone())),
                    None => Ok(()),
                })
        });
        // Add this person to viewer's group
        person.and_then(|_| {
            let new_person = repositories::NewPerson {
                id: uuid::Uuid::new_v4(),
                group_id,
                name,
                resources,
            };
            repositories::PersonRepository::save(&new_person, &context.db_pool)
                .map_err(GraphQLError::InternalServerError)
                .map(|_| true)
        })
    }
fn addPerson ( context : & Context , input : AddPersonInput ) -> Result < bool , GraphQLError > { let AddPersonInput { group_id , name , resources , } = input ; // Check name validity if ! ( 1 ..= 50 ) . contains ( & name . graphemes ( true ) . count ( ) ) { return Err ( GraphQLError :: InvalidName ) ; } // Check resources validity if resources . is_negative ( ) { return Err ( GraphQLError :: InvalidResources ) ; } // Check id validity let group_id = match uuid :: Uuid :: parse_str ( group_id . as_str ( ) ) { Err ( e ) => return Err ( GraphQLError :: InvalidId ) , Ok ( u ) => u , } ; // 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 . name == name ) ) // Check name uniqueness . and_then ( | o | match o { Some ( p ) => Err ( GraphQLError :: NonUniqueName ( name . clone ( ) ) ) , None => Ok ( ( ) ) , } ) } ) ; // Add this person to viewer's group person . and_then ( | _ | { let new_person = repositories :: NewPerson { id : uuid :: Uuid :: new_v4 ( ) , group_id , name , resources , } ; repositories :: PersonRepository :: save ( & new_person , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . map ( | _ | true ) } ) }
FIXME: Extract domain and repository logic to own module
Adds a person to the specified group.
FIXME : Extract domain and repository logic to own module Adds a person to the specified group .
context
&Context
input
AddPersonInput
context
&Context
input
AddPersonInput