예제 #1
0
        public ActionResult New(CreateProductViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Create the command
                    var command = new ProductCommand()
                    {
                        Name        = model.Name,
                        Status      = Headstone.MetaData.API.Models.EntityStatus.Active,
                        Type        = Headstone.MetaData.API.Models.ProductType.Physical,
                        Code        = model.Code,
                        Environment = this.Environment,
                        UserToken   = this.UserToken,
                        SessionId   = this.SessionId,
                        ThreadId    = ThreadId,
                        Properties  = new List <ProductProperty>()
                        {
                            new ProductProperty()
                            {
                                Created = DateTime.UtcNow,
                                Key     = "Price",
                                Value   = model.Price.ToString()
                            },
                            new ProductProperty()
                            {
                                Created = DateTime.UtcNow,
                                Key     = "ListPrice",
                                Value   = model.ListPrice.ToString()
                            }
                        },
                    };

                    // Add the product
                    var productResponse = metadataServiceClient.CreateProduct(command);

                    // Check the response
                    if (productResponse.Type != Headstone.MetaData.Common.Models.ServiceResponseTypes.Success)
                    {
                        Log(Headstone.Framework.Models.LogMode.Error, $"There is an error while creating the product! Error:{productResponse.Message}", null);

                        return(View(model));
                    }

                    #region [ Categories ]

                    foreach (var category in model.CategoryIds)
                    {
                        // Add categories
                        var categoryResponse = metadataServiceClient.CreateCategoryProduct(new CategoryProductCommand()
                        {
                            Environment = this.Environment,
                            UserToken   = this.UserToken,
                            SessionId   = this.SessionId,
                            ThreadId    = ThreadId,
                            ProductId   = productResponse.ProductId,
                            CategoryId  = Convert.ToInt32(category),
                            Status      = Headstone.MetaData.API.Models.EntityStatus.Active
                        });

                        // Check the category response
                        if (categoryResponse.Type != Headstone.MetaData.Common.Models.ServiceResponseTypes.Success)
                        {
                            Log(Headstone.Framework.Models.LogMode.Error, $"There is an error while creating the category product! Error:{categoryResponse.Message}", null);

                            return(View(model));
                        }
                    }

                    #endregion

                    #region [ Trademark ]

                    // Add trademark
                    var trademarkResponse = metadataServiceClient.CreateTrademarkProduct(new TrademarkProductCommand()
                    {
                        Environment = this.Environment,
                        UserToken   = this.UserToken,
                        SessionId   = this.SessionId,
                        ThreadId    = ThreadId,
                        ProductId   = productResponse.ProductId,
                        TrademarkId = Convert.ToInt32(model.Trademark),
                        Status      = Headstone.MetaData.API.Models.EntityStatus.Active,
                        SortOrder   = 1
                    });

                    // Check the category response
                    if (trademarkResponse.Type != Headstone.MetaData.Common.Models.ServiceResponseTypes.Success)
                    {
                        Log(Headstone.Framework.Models.LogMode.Error, $"There is an error while creating the trademark product! Error:{trademarkResponse.Message}", null);

                        return(View(model));
                    }


                    return(RedirectToAction("Details", new { id = productResponse.ProductId }));

                    #endregion
                }
            }
            catch (Exception ex)
            {
                Log(Headstone.Framework.Models.LogMode.Error, "There is an error while creating the products", ex);

                model.Errors.Add("Ürün eklerken hata oluştu");
            }
            return(RedirectToAction("Products", "My"));
        }