private void SaveManufacturerMappings(ProductCreateOrUpdateModel model)
        {
            var manufacturerMappings = new List <ProductManufacturerMapping>();

            if (model.ManufacturerIds != null)
            {
                foreach (var id in model.ManufacturerIds)
                {
                    // check if manufacture exist
                    Guid manufactureId;
                    if (Guid.TryParse(id, out manufactureId))
                    {
                        if (_manufacturerService.GetManufacturerById(manufactureId) != null)
                        {
                            // create mapping entity
                            var manufacturerMapping = new ProductManufacturerMapping
                            {
                                Id             = Guid.NewGuid(),
                                ProductId      = model.Id,
                                ManufacturerId = Guid.Parse(id)
                            };

                            manufacturerMappings.Add(manufacturerMapping);
                        }
                    }
                }
            }

            // save to database
            _manufacturerService.DeleteAllProductManufacturersMappings(model.Id);
            _manufacturerService.InsertProductManufacturerMappings(manufacturerMappings);
        }
        private void SaveImageMappings(ProductCreateOrUpdateModel model)
        {
            var imageMappings = new List <ProductImageMapping>();

            if (model.ImageIds != null)
            {
                for (int i = 0; i < model.ImageIds.Count; i++)
                {
                    // convert sort order to int
                    int sortOrder = Convert.ToInt32(Math.Floor(Convert.ToDouble(model.ImageSortOrder[i])));

                    // check if image exist
                    Guid imageId;
                    if (Guid.TryParse(model.ImageIds[i], out imageId))
                    {
                        // create mapping entity
                        var imageMapping = new ProductImageMapping
                        {
                            Id        = Guid.NewGuid(),
                            ProductId = model.Id,
                            ImageId   = Guid.Parse(model.ImageIds[i]),
                            SortOrder = sortOrder,
                            Position  = i
                        };

                        imageMappings.Add(imageMapping);
                    }
                }
            }

            // save to database
            _imageManagerService.DeleteAllProductImageMappings(model.Id);
            _imageManagerService.InsertProductImageMappings(imageMappings);
        }
        private void SaveCategoryMappings(ProductCreateOrUpdateModel model)
        {
            var categoryMappings = new List <ProductCategoryMapping>();

            if (model.CategoryIds != null)
            {
                foreach (var id in model.CategoryIds)
                {
                    // check if category exist
                    Guid categoryId;
                    if (Guid.TryParse(id, out categoryId))
                    {
                        if (_categoryService.GetCategoryById(categoryId) != null)
                        {
                            // create mapping entity
                            var categoryMapping = new ProductCategoryMapping
                            {
                                Id         = Guid.NewGuid(),
                                ProductId  = model.Id,
                                CategoryId = Guid.Parse(id)
                            };

                            categoryMappings.Add(categoryMapping);
                        }
                    }
                }
            }

            // save to database
            _categoryService.DeleteAllProductCategoryMappingsByProductId(model.Id);
            _categoryService.InsertProductCategoryMappings(categoryMappings);
        }
        // GET: /Product/Create
        public IActionResult Create()
        {
            var model = new ProductCreateOrUpdateModel();

            model.CategorySelectList         = _viewHelper.GetCategorySelectList();
            model.ManufacturerSelectList     = _viewHelper.GetManufacturerSelectList();
            model.SpecificationKeySelectList = _viewHelper.GetSpecificationKeySelectList();
            return(View(model));
        }
        private void SaveSpecificationMappings(ProductCreateOrUpdateModel model)
        {
            var specificationMappings = new List <ProductSpecificationMapping>();

            if (model.Specifications != null)
            {
                int i = 0;
                foreach (var spec in model.Specifications)
                {
                    // check if specification exist
                    Guid specificationId;
                    if (Guid.TryParse(spec.Key, out specificationId))
                    {
                        if (specificationId == Guid.Empty)
                        {
                            continue;
                        }

                        // create mapping entity
                        var specificationMapping = new ProductSpecificationMapping
                        {
                            Id              = Guid.NewGuid(),
                            ProductId       = model.Id,
                            SpecificationId = specificationId,
                            Value           = spec.Value,
                            SortOrder       = spec.SortOrder,
                            Position        = i
                        };
                        i++;

                        specificationMappings.Add(specificationMapping);
                    }
                }
            }

            // save to database
            _specificationService.DeleteAllProductSpecificationMappings(model.Id);
            _specificationService.InsertProductSpecificationMappings(specificationMappings);
        }
        public IActionResult Edit(ProductCreateOrUpdateModel model, bool continueEditing)
        {
            var hasError = false;

            if (ModelState.IsValid)
            {
                // get model from session
                var sessionModel = JsonConvert.DeserializeObject <ProductCreateOrUpdateModel>(Session.GetString(_sessionKey));
                model.Id        = sessionModel.Id;
                model.DateAdded = sessionModel.DateAdded;

                // check if user edit the name
                if (model.Name.ToLower() != sessionModel.Name.ToLower())
                {
                    // check if name exist
                    if (_dataHelper.CheckForDuplicate(ServiceType.Product, DataType.Name, model.Name))
                    {
                        ModelState.AddModelError(string.Empty, "Product name already exist");
                        hasError = true;
                    }
                }

                // create seo friendly url if the user didn't provide
                if (string.IsNullOrEmpty(model.SeoUrl))
                {
                    model.SeoUrl = _dataHelper.GenerateSeoFriendlyUrl(ServiceType.Product, model.Name);
                }
                else
                {
                    // check if user change seo url
                    if (model.SeoUrl.ToLower() != sessionModel.SeoUrl.ToLower())
                    {
                        // check if seo already exist
                        if (_dataHelper.CheckForDuplicate(ServiceType.Product, DataType.Seo, model.SeoUrl))
                        {
                            ModelState.AddModelError(string.Empty, "SEO Url already exist");
                            hasError = true;
                        }
                    }
                }

                // if everything is valid
                if (!hasError)
                {
                    // map view model to entity
                    var productEntity = _mapper.Map <ProductCreateOrUpdateModel, Product>(model);
                    productEntity.DateModified = DateTime.Now;

                    // save to database
                    _productService.UpdateProduct(productEntity);
                    SaveCategoryMappings(model);
                    SaveManufacturerMappings(model);
                    SaveImageMappings(model);
                    SaveSpecificationMappings(model);

                    if (continueEditing)
                    {
                        return(RedirectToAction("Edit", new { id = productEntity.Id, ActiveTab = model.ActiveTab }));
                    }

                    return(RedirectToAction("List"));
                }
            }

            // something went wrong, redisplay form
            model.CategorySelectList         = _viewHelper.GetCategorySelectList();
            model.ManufacturerSelectList     = _viewHelper.GetManufacturerSelectList();
            model.SpecificationKeySelectList = _viewHelper.GetSpecificationKeySelectList();
            return(View(model));
        }