public HttpResponseMessage Post(CreateCartCommand command)
 {
     bus.Send(command);
     string link = Url.Link("DefaultApi", new { controller = "Cart", Id = command.Id });
     HttpResponseMessage message = this.Request.CreateResponse(HttpStatusCode.Created);
     message.Headers.Location = new Uri(link);
     return message;
 }
Example #2
0
        public async Task <IActionResult> CreateAsync(string customerId)
        {
            var command       = new CreateCartCommand(customerId);
            var commandResult = await mediator.Send(command);

            if (commandResult.WasSuccessful)
            {
                return(RedirectToAction(nameof(IndexAsync)));
            }

            var viewModel = await GetIndexViewModel();

            viewModel.AddMessage(MessageModel.Alert(commandResult.BrokenRules.First().Message));

            return(View(nameof(IndexAsync), viewModel));
        }
Example #3
0
        public async Task post_should_create_a_cart(string[] items, string email)
        {
            var client  = _factory.CreateClient();
            var request = new CreateCartCommand {
                ItemsIds = items, UserEmail = email
            };

            var httpContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
            var response    = await client.PostAsync("/api/cart", httpContent);

            response.EnsureSuccessStatusCode();

            var responseHeader = response.Headers.Location;

            response.StatusCode.ShouldBe(HttpStatusCode.Created);
            responseHeader.ToString().Contains("/api/cart").ShouldBeTrue();
        }
Example #4
0
 public async Task <string> Add(CreateCartCommand command)
 {
     return(await Mediator.Send(command));
 }
Example #5
0
        public async Task <IActionResult> Post(CreateCartCommand request)
        {
            var result = await _mediator.Send(request);

            return(CreatedAtAction(nameof(GetById), new { id = result.Id }, null));
        }
        public void Build(ISchema schema)
        {
            //Queries
            //We can't use the fluent syntax for new types registration provided by dotnet graphql here, because we have the strict requirement for underlying types extensions
            //and must use GraphTypeExtenstionHelper to resolve the effective type on execution time
            var cartField = new FieldType
            {
                Name      = "cart",
                Arguments = new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                    Name = "storeId", Description = "Store Id"
                },
                    new QueryArgument <StringGraphType> {
                    Name = "userId", Description = "User Id"
                },
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                    Name = "currencyCode", Description = "Currency code (\"USD\")"
                },
                    new QueryArgument <StringGraphType> {
                    Name = "cultureName", Description = "Culture name (\"en-Us\")"
                },
                    new QueryArgument <StringGraphType> {
                    Name = "cartName", Description = "Cart name"
                },
                    new QueryArgument <StringGraphType> {
                    Name = "type", Description = "Cart type"
                }),
                Type     = GraphTypeExtenstionHelper.GetActualType <CartType>(),
                Resolver = new AsyncFieldResolver <object>(async context =>
                {
                    var getCartQuery           = context.GetCartQuery <GetCartQuery>();
                    getCartQuery.IncludeFields = context.SubFields.Values.GetAllNodesPaths().ToArray();
                    context.CopyArgumentsToUserContext();
                    var cartAggregate = await _mediator.Send(getCartQuery);
                    if (cartAggregate == null)
                    {
                        var createCartCommand = new CreateCartCommand(getCartQuery.StoreId, getCartQuery.CartType, getCartQuery.CartName, getCartQuery.UserId, getCartQuery.CurrencyCode, getCartQuery.CultureName);
                        cartAggregate         = await _mediator.Send(createCartCommand);
                    }

                    context.SetExpandedObjectGraph(cartAggregate);

                    return(cartAggregate);
                })
            };

            schema.Query.AddField(cartField);


            var orderConnectionBuilder = GraphTypeExtenstionHelper.CreateConnection <CartType, object>()
                                         .Name("carts")
                                         .Argument <StringGraphType>("storeId", "")
                                         .Argument <StringGraphType>("userId", "")
                                         .Argument <StringGraphType>("currencyCode", "")
                                         .Argument <StringGraphType>("cultureName", "")
                                         .Argument <StringGraphType>("cartType", "")
                                         .Argument <StringGraphType>("filter", "This parameter applies a filter to the query results")
                                         .Argument <StringGraphType>("sort", "The sort expression")
                                         .Unidirectional()
                                         .PageSize(20);

            orderConnectionBuilder.ResolveAsync(async context => await ResolveConnectionAsync(_mediator, context));

            schema.Query.AddField(orderConnectionBuilder.FieldType);

            //Mutations
            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputAddItemType!){ addItem(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "productId": "9cbd8f316e254a679ba34a900fccb076",
            ///          "quantity": 1
            ///      }
            ///   }
            /// }
            /// </example>
            var addItemField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                               .Name("addItem")
                               .Argument <NonNullGraphType <InputAddItemType> >(_commandName)
                               //TODO: Write the unit-tests for successfully mapping input variable to the command
                               .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <AddCartItemCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            })
                               .FieldType;

            schema.Mutation.AddField(addItemField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputClearCartType!){ clearCart(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart"
            ///      }
            ///   }
            /// }
            /// </example>
            var clearCartField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                 .Name("clearCart")
                                 .Argument <NonNullGraphType <InputClearCartType> >(_commandName)
                                 .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <ClearCartCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(clearCartField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputChangeCommentType!){ changeComment(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "comment": "Hi, Virto!"
            ///      }
            ///   }
            /// }
            /// </example>
            var changeCommentField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                     .Name("changeComment")
                                     .Argument <InputChangeCommentType>(_commandName)
                                     .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <ChangeCommentCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            })
                                     .FieldType;

            schema.Mutation.AddField(changeCommentField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputChangeCartItemPriceType!){ changeCartItemPrice(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "lineItemId": "9cbd8f316e254a679ba34a900fccb076",
            ///          "price": 777
            ///      }
            ///   }
            /// }
            /// </example>
            var changeCartItemPriceField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                           .Name("changeCartItemPrice")
                                           .Argument <NonNullGraphType <InputChangeCartItemPriceType> >(_commandName)
                                           .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <ChangeCartItemPriceCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(changeCartItemPriceField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputChangeCartItemQuantityType!){ changeCartItemQuantity(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "lineItemId": "9cbd8f316e254a679ba34a900fccb076",
            ///          "quantity": 777
            ///      }
            ///   }
            /// }
            /// </example>
            var changeCartItemQuantityField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                              .Name("changeCartItemQuantity")
                                              .Argument <NonNullGraphType <InputChangeCartItemQuantityType> >(_commandName)
                                              .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <ChangeCartItemQuantityCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(changeCartItemQuantityField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputChangeCartItemCommentType!){ changeCartItemComment(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "lineItemId": "9cbd8f316e254a679ba34a900fccb076",
            ///          "comment": "verynicecomment"
            ///      }
            ///   }
            /// }
            /// </example>
            var changeCartItemCommentField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                             .Name("changeCartItemComment")
                                             .Argument <InputChangeCartItemCommentType>(_commandName)
                                             .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <ChangeCartItemCommentCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(changeCartItemCommentField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputRemoveItemType!){ removeCartItem(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "lineItemId": "9cbd8f316e254a679ba34a900fccb076"
            ///      }
            ///   }
            /// }
            /// </example>
            var removeCartItemField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                      .Name("removeCartItem")
                                      .Argument <NonNullGraphType <InputRemoveItemType> >(_commandName)
                                      .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <RemoveCartItemCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(removeCartItemField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputAddCouponType!){ addCoupon(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "couponCode": "verynicecouponcode"
            ///      }
            ///   }
            /// }
            /// </example>
            var addCouponField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                 .Name("addCoupon")
                                 .Argument <NonNullGraphType <InputAddCouponType> >(_commandName)
                                 .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <AddCouponCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(addCouponField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputRemoveCouponType!){ removeCoupon(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "couponCode": "verynicecouponcode"
            ///      }
            ///   }
            /// }
            /// </example>
            var removeCouponField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                    .Name("removeCoupon")
                                    .Argument <NonNullGraphType <InputRemoveCouponType> >(_commandName)
                                    .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <RemoveCouponCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(removeCouponField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputRemoveShipmentType!){ removeShipment(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "shipmentId": "7777-7777-7777-7777"
            ///      }
            ///   }
            /// }
            /// </example>
            var removeShipmentField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                      .Name("removeShipment")
                                      .Argument <NonNullGraphType <InputRemoveShipmentType> >(_commandName)
                                      .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <RemoveShipmentCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(removeShipmentField);

            //TODO: add shipment model to example
            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputAddOrUpdateCartShipmentType!){ addOrUpdateCartShipment(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "shipment": { }
            ///      }
            ///   }
            /// }
            /// </example>
            var addOrUpdateCartShipmentField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                               .Name("addOrUpdateCartShipment")
                                               .Argument <NonNullGraphType <InputAddOrUpdateCartShipmentType> >(_commandName)
                                               .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <AddOrUpdateCartShipmentCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(addOrUpdateCartShipmentField);

            //TODO: add payment model to example
            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputAddOrUpdateCartPaymentType!){ addOrUpdateCartPayment(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "payment": { }
            ///      }
            ///   }
            /// }
            /// </example>
            var addOrUpdateCartPaymentField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                              .Name("addOrUpdateCartPayment")
                                              .Argument <NonNullGraphType <InputAddOrUpdateCartPaymentType> >(_commandName)
                                              .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <AddOrUpdateCartPaymentCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(addOrUpdateCartPaymentField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputValidateCouponType!){ validateCoupon(command: $command) }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "coupon": {
            ///             "code":"verynicecodeforvalidation"
            ///         }
            ///      }
            ///   }
            /// }
            /// </example>
            var validateCouponField = FieldBuilder.Create <CartAggregate, bool>(typeof(BooleanGraphType))
                                      .Name("validateCoupon")
                                      .Argument <NonNullGraphType <InputValidateCouponType> >(_commandName)
                                      .ResolveAsync(async context => await _mediator.Send(context.GetArgument <ValidateCouponCommand>(_commandName)))
                                      .FieldType;

            schema.Mutation.AddField(validateCouponField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:MergeCartType!){ mergeCart(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "secondCartId": "7777-7777-7777-7777"
            ///      }
            ///   }
            /// }
            /// </example>
            var margeCartField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                 .Name("mergeCart")
                                 .Argument <NonNullGraphType <InputMergeCartType> >(_commandName)
                                 .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetCartCommand <MergeCartCommand>());
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            }).FieldType;

            schema.Mutation.AddField(margeCartField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputRemoveCartType!){ removeCart(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "cartId": "7777-7777-7777-7777"
            ///      }
            ///   }
            /// }
            /// </example>
            var removeCartField = FieldBuilder.Create <CartAggregate, bool>(typeof(BooleanGraphType))
                                  .Name("removeCart")
                                  .Argument <NonNullGraphType <InputRemoveCartType> >(_commandName)
                                  .ResolveAsync(async context => await _mediator.Send(context.GetArgument <RemoveCartCommand>(_commandName)))
                                  .FieldType;

            schema.Mutation.AddField(removeCartField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputClearShipmentsType!){ clearShipments(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "cartId": "7777-7777-7777-7777"
            ///      }
            ///   }
            /// }
            /// </example>
            var clearShipmentsField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                      .Name("clearShipments")
                                      .Argument <NonNullGraphType <InputClearShipmentsType> >(_commandName)
                                      .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetArgument <ClearShipmentsCommand>(_commandName));
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            })
                                      .FieldType;

            schema.Mutation.AddField(clearShipmentsField);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputClearPaymentsType!){ clearPayments(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "cartId": "7777-7777-7777-7777"
            ///      }
            ///   }
            /// }
            /// </example>
            var clearPaymentsField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                     .Name("clearPayments")
                                     .Argument <NonNullGraphType <InputClearPaymentsType> >(_commandName)
                                     .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetArgument <ClearPaymentsCommand>(_commandName));
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            })
                                     .FieldType;

            schema.Mutation.AddField(clearPaymentsField);


            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputAddOrUpdateCartAddressType!){ addOrUpdateCartAddress(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "storeId": "Electronics",
            ///          "cartName": "default",
            ///          "userId": "b57d06db-1638-4d37-9734-fd01a9bc59aa",
            ///          "language": "en-US",
            ///          "currency": "USD",
            ///          "cartType": "cart",
            ///          "address": {
            ///             "line1":"st street 1"
            ///         }
            ///      }
            ///   }
            /// }
            /// </example>
            var addOrUpdateCartAddress = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                         .Name("addOrUpdateCartAddress")
                                         .Argument <NonNullGraphType <InputAddOrUpdateCartAddressType> >(_commandName)
                                         .ResolveAsync(async context =>
            {
                //TODO: Need to refactor later to prevent ugly code duplication
                //We need to add cartAggregate to the context to be able use it on nested cart types resolvers (e.g for currency)
                var cartAggregate = await _mediator.Send(context.GetArgument <AddOrUpdateCartAddressCommand>(_commandName));
                //store cart aggregate in the user context for future usage in the graph types resolvers
                context.SetExpandedObjectGraph(cartAggregate);
                return(cartAggregate);
            })
                                         .FieldType;

            schema.Mutation.AddField(addOrUpdateCartAddress);

            /// <example>
            /// This is an example JSON request for a mutation
            /// {
            ///   "query": "mutation ($command:InputRemoveCartAddressType!){ removeCartAddress(command: $command) {  total { formatedAmount } } }",
            ///   "variables": {
            ///      "command": {
            ///          "cartId": "7777-7777-7777-7777",
            ///          "addressId": "111"
            ///      }
            ///   }
            /// }
            /// </example>
            var removeCartAddressField = FieldBuilder.Create <CartAggregate, CartAggregate>(typeof(CartType))
                                         .Name("removeCartAddress")
                                         .Argument <NonNullGraphType <InputRemoveCartAddressType> >(_commandName)
                                         .ResolveAsync(async context => await _mediator.Send(context.GetArgument <RemoveCartAddressCommand>(_commandName)))
                                         .FieldType;

            schema.Mutation.AddField(removeCartAddressField);
        }
Example #7
0
        static async Task Main(string[] args)
        {
            // create and configure the service container
            IServiceCollection serviceCollection = ConfigureServices();

            // build the service provider
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            IUnitOfWork uow = serviceProvider.GetService <IUnitOfWork>();

            // get all users
            var users = await uow.GetUserRepository().GetAllAsync();

            PrintUsers(users);

            // get all products
            var prods = await uow.GetProductRepository().GetAllAsync();

            PrintProducts(prods);

            Command cmd;
            Command saveCommand = new SaveCommand(uow);

            // create carts for users[0] & users[1]
            cmd = new CreateCartCommand(users[0]);
            cmd.Execute();

            cmd = new CreateCartCommand(users[1]);
            cmd.Execute();

            var cart1 = users[0].Carts.Last();
            var cart2 = users[1].Carts.Last();

            saveCommand.Execute();

            // some operation on npaul's cart
            cmd = new AddProductToCartCommand(cart1, prods[0], 5);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart1, prods[1], 3);
            cmd.Execute();

            cmd = new UpdateCartItemQuantityCommand(cart1, prods[0], 6);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart1);

            // some operation on jdoe's cart
            cmd = new AddProductToCartCommand(cart2, prods[0], 1);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[1], 3);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[2], 2);
            cmd.Execute();

            cmd = new AddProductToCartCommand(cart2, prods[3], 2);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart2);

            cmd = new UpdateProductPriceCommand(prods[1], 15);
            cmd.Execute();

            saveCommand.Execute();

            PrintCart(cart1);
            PrintCart(cart2);

            // cleaning
            cmd = new DeleteCartCommand(cart1, uow.GetCartRepository());
            cmd.Execute();

            cmd = new DeleteCartCommand(cart2, uow.GetCartRepository());
            cmd.Execute();

            saveCommand.Execute();

            cmd = new UpdateProductPriceCommand(prods[1], 17.50);
            cmd.Execute();

            saveCommand.Execute();
        }
Example #8
0
 public async Task <IActionResult> CreateCartAsync(CreateCartCommand command)
 {
     return(Ok(await _mediator.Send(command)));
 }