Exemple #1
0
        public CarVersionQuery(CarVersionRepository versionRepository)
        {
            Field <ListGraphType <CarVersionType> >("versions",
                                                    resolve: context =>
            {
                return(versionRepository.FindAll());
            });

            Field <CarVersionType>("version",
                                   arguments: new QueryArguments(new List <QueryArgument>
            {
                new QueryArgument <IdGraphType>
                {
                    Name = "id"
                }
            }),
                                   resolve: context =>
            {
                var id = context.GetArgument <int>("id");
                if (id <= 0)
                {
                    context.Errors.Add(new ExecutionError("Version Id is invalid or missing"));
                    return(null);
                }

                return(versionRepository.FindById(id));
            });
        }
Exemple #2
0
        public CarVersionMutation(CarVersionRepository versionRepository)
        {
            /**
             * NEW VERSION
             */
            Field <CarVersionType, CarVersion>()
            .Name("createVersion")
            .Argument <NonNullGraphType <CarVersionInputType> >("version", "version input")
            .ResolveAsync(async ctx =>
            {
                var version = ctx.GetArgument <CarVersion>("version");
                return(await versionRepository.Add(version));
            });

            /**
             * UPDATE VERSION
             */
            Field <CarVersionType, CarVersion>()
            .Name("updateVersion")
            .Argument <NonNullGraphType <CarVersionInputType> >("version", "version input")
            .ResolveAsync(async ctx =>
            {
                var version = ctx.GetArgument <CarVersion>("version");

                // Check if version exists
                var currentVersion = await versionRepository.FindById(version.Id);
                if (currentVersion == null)
                {
                    ctx.Errors.Add(new ExecutionError("Version not found"));
                    return(null);
                }
                // Update version
                return(await versionRepository.Update(version));
            });

            /**
             * DELETE VERSION
             */
            Field <CarVersionType, CarVersion>()
            .Name("deleteVersion")
            .Argument <NonNullGraphType <IdGraphType> >("id", "brand id input")
            .ResolveAsync(async ctx =>
            {
                var id = ctx.GetArgument <int>("id");

                // Check if version exists
                var currentVersion = await versionRepository.FindById(id);
                if (currentVersion == null)
                {
                    ctx.Errors.Add(new ExecutionError("Version not found"));
                    return(null);
                }
                // delete Version
                await versionRepository.Remove(id);

                return(null);
            });
        }