fn addGroup(context: &Context, input: AddGroupInput) -> Result<bool, GraphQLError> {
        let AddGroupInput { name } = input;
        // Check name validity
        if !(1..=50).contains(&name.graphemes(true).count()) {
            return Err(GraphQLError::InvalidName);
        }
        // Check name uniqueness
        // FIXME: Very inefficient query. Should use joins instead ?
        let result = repositories::UserRepository::find_one(context.viewer.id(), &context.db_pool)
            .and_then(|o| {
                o.map(|u| {
                    repositories::GroupRepository::find_by_user(&u, &context.db_pool)
                        .map(|g| (u.id, g))
                })
                .transpose()
            });

        match result {
            Err(e) => Err(GraphQLError::InternalServerError(e)),
            Ok(None) => Err(GraphQLError::UserNotFound),
            Ok(Some((user_id, v))) => {
                if v.iter().any(|g| g.name == name) {
                    Err(GraphQLError::NonUniqueName(name))
                } else {
                    // Add this group to viewer's
                    let new_group = repositories::NewGroup {
                        id: uuid::Uuid::new_v4(),
                        user_id,
                        name,
                    };
                    repositories::GroupRepository::save(&new_group, &context.db_pool)
                        .map_err(GraphQLError::InternalServerError)
                        .map(|_| true)
                }
            }
        }
    }
// FIXME: Extract domain and repository logic to own module
/// Adds a group.
/// This is a user context dependant mutation.
Extract domain and repository logic to own module
Adds a group.
This is a user context dependant mutation.
Extract domain and repository logic to own module Adds a group . This is a user context dependant mutation .
fn addGroup(context: &Context, input: AddGroupInput) -> Result<bool, GraphQLError> {
        let AddGroupInput { name } = input;
        // Check name validity
        if !(1..=50).contains(&name.graphemes(true).count()) {
            return Err(GraphQLError::InvalidName);
        }
        // Check name uniqueness
        // FIXME: Very inefficient query. Should use joins instead ?
        let result = repositories::UserRepository::find_one(context.viewer.id(), &context.db_pool)
            .and_then(|o| {
                o.map(|u| {
                    repositories::GroupRepository::find_by_user(&u, &context.db_pool)
                        .map(|g| (u.id, g))
                })
                .transpose()
            });
        match result {
            Err(e) => Err(GraphQLError::InternalServerError(e)),
            Ok(None) => Err(GraphQLError::UserNotFound),
            Ok(Some((user_id, v))) => {
                if v.iter().any(|g| g.name == name) {
                    Err(GraphQLError::NonUniqueName(name))
                } else {
                    // Add this group to viewer's
                    let new_group = repositories::NewGroup {
                        id: uuid::Uuid::new_v4(),
                        user_id,
                        name,
                    };
                    repositories::GroupRepository::save(&new_group, &context.db_pool)
                        .map_err(GraphQLError::InternalServerError)
                        .map(|_| true)
                }
            }
        }
    }
fn addGroup ( context : & Context , input : AddGroupInput ) -> Result < bool , GraphQLError > { let AddGroupInput { name } = input ; // Check name validity if ! ( 1 ..= 50 ) . contains ( & name . graphemes ( true ) . count ( ) ) { return Err ( GraphQLError :: InvalidName ) ; } // Check name uniqueness // FIXME: Very inefficient query. Should use joins instead ? let result = repositories :: UserRepository :: find_one ( context . viewer . id ( ) , & context . db_pool ) . and_then ( | o | { o . map ( | u | { repositories :: GroupRepository :: find_by_user ( & u , & context . db_pool ) . map ( | g | ( u . id , g ) ) } ) . transpose ( ) } ) ; match result { Err ( e ) => Err ( GraphQLError :: InternalServerError ( e ) ) , Ok ( None ) => Err ( GraphQLError :: UserNotFound ) , Ok ( Some ( ( user_id , v ) ) ) => { if v . iter ( ) . any ( | g | g . name == name ) { Err ( GraphQLError :: NonUniqueName ( name ) ) } else { // Add this group to viewer's let new_group = repositories :: NewGroup { id : uuid :: Uuid :: new_v4 ( ) , user_id , name , } ; repositories :: GroupRepository :: save ( & new_group , & context . db_pool ) . map_err ( GraphQLError :: InternalServerError ) . map ( | _ | true ) } } } }
FIXME: Extract domain and repository logic to own module
Adds a group.
FIXME : Extract domain and repository logic to own module Adds a group .
context
&Context
input
AddGroupInput
context
&Context
input
AddGroupInput