Beispiel #1
0
        public CarModelMutation(CarModelRepository modelRepository)
        {
            /**
             * NEW MODEL
             */
            Field <CarModelType, CarModel>()
            .Name("createModel")
            .Argument <NonNullGraphType <CarModelInputType> >("model", "model input")
            .ResolveAsync(async ctx =>
            {
                var model = ctx.GetArgument <CarModel>("model");
                return(await modelRepository.Add(model));
            });

            /**
             * UPDATE MODEL
             */
            Field <CarModelType, CarModel>()
            .Name("updateModel")
            .Argument <NonNullGraphType <CarBrandInputType> >("model", "model input")
            .ResolveAsync(async ctx =>
            {
                var model = ctx.GetArgument <CarModel>("model");

                // Check if model exists
                var currentModel = await modelRepository.FindById(model.Id);
                if (currentModel == null)
                {
                    ctx.Errors.Add(new ExecutionError("Model not found"));
                    return(null);
                }
                // Update model
                return(await modelRepository.Update(model));
            });

            /**
             * DELETE MODEL
             */
            Field <CarModelType, CarModel>()
            .Name("deleteModel")
            .Argument <NonNullGraphType <IdGraphType> >("id", "model id input")
            .ResolveAsync(async ctx =>
            {
                var id = ctx.GetArgument <int>("id");

                // Check if model exists
                var currentModel = await modelRepository.FindById(id);
                if (currentModel == null)
                {
                    ctx.Errors.Add(new ExecutionError("Model not found"));
                    return(null);
                }
                // delete model
                await modelRepository.Remove(id);

                return(null);
            });
        }
        public void CanAddCarModel()
        {
            var repo = new CarModelRepository();

            Assert.AreEqual(9, repo.GetAll().Count);
            repo.Add(new CarModel()
            {
                MakeId = 1, CarModelName = "Big"
            });
            Assert.AreEqual(10, repo.GetAll().Count);
        }