示例#1
0
        /// <summary>
        /// Creates a mutation that will delete things from the database by a where condition
        /// </summary>
        /// <param name="name">The name of the model to delete</param>
        /// <typeparam name="TModel">The type of the model to delete</typeparam>
        /// <returns>A function that takes a graphql context and returns whether the delete is successful</returns>
        public static Func <ResolveFieldContext <object>, Task <object> > CreateConditionalDeleteMutation <TModel>(string name)
            where TModel : class, IOwnerAbstractModel, new()
        {
            return(async context =>
            {
                var graphQlContext = (LactalisGraphQlContext)context.UserContext;
                var crudService = graphQlContext.CrudService;
                var user = graphQlContext.User;
                var dbSet = graphQlContext.DbContext.Set <TModel>().AsQueryable();

                var models = QueryHelpers.CreateConditionalWhere(context, dbSet);
                models = QueryHelpers.CreateIdsCondition(context, models);
                models = QueryHelpers.CreateIdCondition(context, models);

                try
                {
                    return await crudService.ConditionalDelete(models);
                }
                catch (AggregateException exception)
                {
                    context.Errors.AddRange(
                        exception.InnerExceptions.Select(error => new ExecutionError(error.Message)));
                    return false;
                }
            });
        }
示例#2
0
        /// <summary>
        /// Creates a mutation that will update things from the database by a where condition
        /// </summary>
        /// <param name="name">The name of the model to update</param>
        /// <typeparam name="TModel">The type of the model to update</typeparam>
        /// <returns>A function that takes a graphql context and returns whether the delte is successful</returns>
        public static Func <ResolveFieldContext <object>, Task <object> > CreateConditionalUpdateMutation <TModel>(string name)
            where TModel : class, IOwnerAbstractModel, new()
        {
            return(async context =>
            {
                var graphQlContext = (LactalisGraphQlContext)context.UserContext;
                var crudService = graphQlContext.CrudService;
                var user = graphQlContext.User;
                var dbSet = graphQlContext.DbContext.Set <TModel>().AsQueryable();

                var models = QueryHelpers.CreateConditionalWhere(context, dbSet);
                models = QueryHelpers.CreateIdsCondition(context, models);
                models = QueryHelpers.CreateIdCondition(context, models);
                var fieldsToUpdate = context.GetArgument <List <string> >("fieldsToUpdate");
                var valuesToUpdate = context.GetArgument <TModel>("valuesToUpdate");

                var createObject = Expression.New(typeof(TModel));

                var fields = new List <MemberBinding>();
                foreach (string field in fieldsToUpdate)
                {
                    var modelType = valuesToUpdate.GetType();
                    var prop = modelType.GetProperty(field.ConvertToPascalCase());

                    object value;
                    try
                    {
                        value = prop.GetValue(valuesToUpdate);
                    }
                    catch (NullReferenceException)
                    {
                        throw new ArgumentException($"Property {field} does not exist in the entity");
                    }

                    var target = Expression.Constant(value, prop.PropertyType);

                    fields.Add(Expression.Bind(prop, target));
                }
                var initializePropertiesOnObject = Expression.MemberInit(
                    createObject,
                    fields);

                try
                {
                    return await crudService.ConditionalUpdate(models, initializePropertiesOnObject);
                }
                catch (AggregateException exception)
                {
                    context.Errors.AddRange(
                        exception.InnerExceptions.Select(error => new ExecutionError(error.Message)));
                    return false;
                }
            });
        }
示例#3
0
        /// <summary>
        /// Creates a resolve function for a query that can both AND and OR conditions together
        /// </summary>
        /// <typeparam name="TModel">The type of model to create the query for</typeparam>
        /// <returns>A function that takes a graphql context and returns a list of models</returns>
        public static Func <ResolveFieldContext <object>, IQueryable <TModel> > CreateConditionalQuery <TModel>()
            where TModel : class, IOwnerAbstractModel, new()
        {
            return(context =>
            {
                // Fetch the models that we need
                var models = QueryHelpers.CreateResolveFunction <TModel>()(context);

                // Apply the conditions to the query
                models = QueryHelpers.CreateConditionalWhere(context, models);

                return models;
            });
        }
示例#4
0
        /// <summary>
        /// Creates a query that counts the number of models that comply to a set of conditions.
        /// This query can perform both AND and OR conditions.
        /// </summary>
        /// <typeparam name="TModel">The type of model to count</typeparam>
        /// <returns>A function that takes a graphql context and returns a list of models</returns>
        public static Func <ResolveFieldContext <object>, Task <object> > CreateConditionalCountQuery <TModel>()
            where TModel : class, IOwnerAbstractModel, new()
        {
            return(async context =>
            {
                // Fetch the models that we need
                var models = QueryHelpers.CreateResolveFunction <TModel>()(context);

                // Apply conditions to the query
                models = QueryHelpers.CreateConditionalWhere(context, models);
                models = QueryHelpers.CreateIdsCondition(context, models);
                models = QueryHelpers.CreateIdCondition(context, models);

                return new NumberObject {
                    Number = await models.CountAsync()
                };
            });
        }