Esempio n. 1
0
        /// <summary>
        /// This is a helper method for resolvers to resolve an entity via an <see cref="ICrudAsync"/>
        /// </summary>
        /// <param name="context">The GraphQL context. Used to access parameters.</param>
        /// <param name="crud">The <see cref="ICrudAsync{EntityT}"/> used to resolve the entity.</param>
        /// <param name="idParamName">The name of the GraphQL parameter containing the entity's ID. Defaults to 'id'.</param>
        /// <param name="policy">The ASP Core policy that the user must pass in order to access this function.</param>
        /// <param name="userContext">Used to gain access to the user performing this action.</param>
        protected async Task <object> ResolveCrudAsync(
            string policy,
            ICrudAsync <TResolveEntity> crud,
            IResolveFieldContext <object> context,
            DataAccessUserContext userContext,
            string idParamName = "id"
            )
        {
            await userContext.EnforceHasPolicyAsync(policy);

            // ARGS
            var id = context.GetArgument <int>(idParamName);

            // Find the entity
            var entity = await crud.GetByIdAsync(id);

            if (!entity.Succeeded)
            {
                throw new ExecutionError(entity.GatherErrorMessages().Aggregate((a, b) => $"{a}\n{b}"));
            }

            return(entity.Value);
        }
Esempio n. 2
0
        public static void AddNoteMutationsAsync <TSourceType>(
            this ObjectGraphType <TSourceType> mutator,
            DataAccessUserContext context,
            IUnitOfWork unitOfWork,
            INoteManager notes,
            string writeNotesPerm,
            GetNoteOwnerFunc <TSourceType> getNoteOwner
            )
            where TSourceType : class
        {
            mutator.FieldAsync <BooleanGraphType>(
                "addNote",
                "Adds a note.",
                new QueryArguments(
                    // category: String!
                    new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "category"
            },
                    // content: String!
                    new QueryArgument <NonNullGraphType <StringGraphType> >
            {
                Name = "content"
            }
                    ),
                async ctx =>
            {
                await context.EnforceHasPolicyAsync(writeNotesPerm);

                using (var scope = unitOfWork.Begin("Add note"))
                {
                    var owner = getNoteOwner(ctx);
                    var note  = new NoteEntry
                    {
                        NoteOwner = owner,
                        Category  = ctx.GetArgument <string>("category"),
                        Content   = ctx.GetArgument <string>("content")
                    };
                    await notes.CreateAsync(note);
                    scope.Commit();
                }

                return(true);
            }
                );

            mutator.FieldAsync <BooleanGraphType>(
                "deleteNotes",
                "Deletes a set of notes.",
                new QueryArguments(
                    // ids: [ID!]!
                    new QueryArgument <NonNullGraphType <ListGraphType <NonNullGraphType <IdGraphType> > > >
            {
                Name = "ids"
            }
                    ),
                async ctx =>
            {
                await context.EnforceHasPolicyAsync(writeNotesPerm);

                using (var scope = unitOfWork.Begin("Delete notes"))
                {
                    var owner = getNoteOwner(ctx);
                    foreach (var id in ctx.GetArgument <List <int> >("ids"))
                    {
                        var note = await notes.GetByIdAsync(id);
                        if (!note.Succeeded)
                        {
                            scope.Rollback("Note was not found");
                            return(false);
                        }

                        if (!owner.NoteEntries.Any(e => e.NoteEntryId == note.Value.NoteEntryId))
                        {
                            scope.Rollback("Note does not belong to contact");
                            return(false);
                        }

                        notes.Delete(note.Value);
                    }

                    scope.Commit();
                }

                return(true);
            }
                );
        }