Пример #1
0
        public ActionResult Edit(CreateProductViewModel model)
        {
            try
            {
                #region [ Load model data ]

                var existmodel = new CreateProductViewModel()
                {
                    CurrentUser    = CurrentUser,
                    TempPictureKey = CreateThreadId(5),
                    IsEdit         = true
                };

                // Load the exist product
                var product = metadataServiceClient.GetProducts(new ProductQuery()
                {
                    Envelope   = "full",
                    ProductIds = new List <int> {
                        model.ProductId
                    }
                }).Result.FirstOrDefault();

                existmodel.Name                = model.Name;
                existmodel.Pictures            = product.Pictures;
                existmodel.SelectedTrademarkId = Convert.ToInt32(model.Trademark);
                existmodel.EANCode             = model.EANCode;
                existmodel.Code                = model.Code;
                existmodel.ProductId           = model.ProductId;
                //existmodel.TaxRate = model.TaxRate,

                // Get the produc categories
                var productcategories = product.CategoryProducts.Select(c => c.CategoryId).ToList();
                model.SelectedCategoryIds = productcategories;

                // Get the categories
                var categoryResponse = metadataServiceClient.GetCategories(new CategoryQuery()
                {
                    Envelope = "full"
                });

                // Get the trademarks
                var trademarks = metadataServiceClient.GetTrademarks(new TrademarkQuery());

                // Get the listing categories
                var categories = categoryResponse.Result.ToList();

                var parentList = new List <CategoryViewModel>();

                // Get parent list
                foreach (var category in categories)
                {
                    // Get the path
                    var idPath   = GetIDPath(category);
                    var namePath = GetNamePath(category);

                    parentList.Add(new CategoryViewModel()
                    {
                        CategoryId = category.CategoryId,
                        ParentId   = category.ParentId,
                        Parent     = category.Parent,
                        IdPath     = idPath,
                        NamePath   = namePath,
                        Name       = category.Name,
                        IsLeaf     = category.Children.Count == 0
                    });
                }

                // Set data to model
                existmodel.Categories = parentList;
                existmodel.Trademarks = trademarks.Result.ToList();

                #endregion

                if (ModelState.IsValid)
                {
                    // Get exist product
                    var existProduct = metadataServiceClient.GetProducts(new ProductQuery()
                    {
                        ProductIds = new List <int> {
                            model.ProductId
                        }
                    }).Result.FirstOrDefault();

                    var productCommand = new ProductCommand()
                    {
                        Environment = this.Environment,
                        UserToken   = this.UserToken,
                        ProductId   = model.ProductId,
                        SessionId   = this.SessionId,
                        Status      = Headstone.MetaData.API.Models.EntityStatus.Active,
                        ThreadId    = ThreadId,
                        Name        = model.Name,
                        Code        = model.Code,
                        //TaxRate = model.TaxRate,
                    };

                    #region [ Product Properties ]

                    #region [ EAN Code ]

                    if (!String.IsNullOrEmpty(model.EANCode))
                    {
                        // Check the any exist property
                        var anyExistEAN = existProduct.Properties.Where(s => s.Key == "EANCode").Any();

                        if (anyExistEAN)
                        {
                            // Get the exist ean
                            var existEAN = existProduct.Properties.Where(s => s.Key == "EANCode").FirstOrDefault();

                            // Check the different EAN
                            var isEqual = existProduct.Properties.Where(s => s.Key == "EANCode" && s.Value == model.EANCode).Any();

                            if (!isEqual)
                            {
                                // Delete the exist property
                                var propertyResponse = metadataServiceClient.DeleteProductProperty(new ProductPropertyCommand()
                                {
                                    PropertyId  = existEAN.PropertyId,
                                    Environment = this.Environment,
                                    UserToken   = this.UserToken,
                                    ProductId   = model.ProductId,
                                    SessionId   = this.SessionId,
                                    ThreadId    = ThreadId,
                                    UserId      = CurrentUser.Id
                                });

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

                                    existmodel.Errors.Add("Ürün güncellenirken hata oluştu !");

                                    return(View("Create", existmodel));
                                }

                                // Create the product property
                                var propertycommand = metadataServiceClient.CreateProductProperty(new ProductPropertyCommand()
                                {
                                    Environment = this.Environment,
                                    UserToken   = this.UserToken,
                                    SessionId   = this.SessionId,
                                    ThreadId    = ThreadId,
                                    UserId      = CurrentUser.Id,
                                    ProductId   = model.ProductId,
                                    Key         = "EANCode",
                                    Value       = model.EANCode
                                });

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

                                    existmodel.Errors.Add("Ürün güncellenirken hata oluştu !");

                                    return(View("Create", existmodel));
                                }
                            }
                        }
                        else
                        {
                            // Create the product property
                            var propertycommand = metadataServiceClient.CreateProductProperty(new ProductPropertyCommand()
                            {
                                Environment = this.Environment,
                                UserToken   = this.UserToken,
                                SessionId   = this.SessionId,
                                ThreadId    = ThreadId,
                                UserId      = CurrentUser.Id,
                                ProductId   = model.ProductId,
                                Key         = "EANCode",
                                Value       = model.EANCode
                            });

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

                                existmodel.Errors.Add("Ürün güncellenirken hata oluştu !");

                                return(View("Create", existmodel));
                            }
                        }
                    }
                    else
                    {
                        // Check the any exist property
                        var anyExistEAN = existProduct.Properties.Where(s => s.Key == "EANCode").Any();

                        if (anyExistEAN)
                        {
                            // Get the exist ean
                            var existEAN = existProduct.Properties.Where(s => s.Key == "EANCode").FirstOrDefault();

                            // Delete the exist property
                            var propertyResponse = metadataServiceClient.DeleteProductProperty(new ProductPropertyCommand()
                            {
                                PropertyId  = existEAN.PropertyId,
                                Environment = this.Environment,
                                UserToken   = this.UserToken,
                                ProductId   = model.ProductId,
                                SessionId   = this.SessionId,
                                ThreadId    = ThreadId,
                                UserId      = CurrentUser.Id
                            });

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

                                existmodel.Errors.Add("Ürün güncellenirken hata oluştu !");

                                return(View("Create", existmodel));
                            }
                        }
                    }

                    #endregion

                    #endregion

                    #region [ Categories ]

                    if (model.CategoryIds.Count() != 0)
                    {
                        var categoryList = new List <CategoryProduct>();

                        // Add categories to object
                        foreach (var id in model.CategoryIds)
                        {
                            var categoryProductCommand = new CategoryProduct()
                            {
                                ProductId  = model.ProductId,
                                CategoryId = Convert.ToInt32(id),
                                Status     = Headstone.MetaData.API.Models.EntityStatus.Active
                            };
                            categoryList.Add(categoryProductCommand);
                        }

                        // Add categories to command
                        productCommand.Categories = categoryList;
                    }

                    #endregion

                    // Update the product
                    var response = metadataServiceClient.UpdateProduct(productCommand);

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

                        existmodel.Errors.Add("Ürün güncellenirken hata oluştu !");

                        return(View("Create", existmodel));
                    }
                }
                else
                {
                    return(View("Create", existmodel));
                }
            }
            catch (Exception ex)
            {
                Log(Headstone.Framework.Models.LogMode.Error, "There is an error while creating the products(POST)", ex);
            }

            #region [ Breadcrumb ]

            // Create the breadcrumb
            var breadcrumb = new List <BreadcrumbItemViewModel>();
            breadcrumb.Add(new BreadcrumbItemViewModel()
            {
                Text = "Ürünler",
                Link = "/products"
            });

            breadcrumb.Add(new BreadcrumbItemViewModel()
            {
                Text = model.Name,
                Link = "/products/" + model.ProductId
            });


            breadcrumb.Add(new BreadcrumbItemViewModel()
            {
                Text = "Düzenle"
            });

            ViewBag.Breadcrumb = breadcrumb;

            #endregion

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