Example #1
0
        public ActionResult SaveProduct(Product model, HttpPostedFileBase[] images, string deletedImages)
        {
            if (!IsAuthentificated)
            {
                return RedirectToAction("Register");
            }

            // Проверяем что у нас - создание или сохранение
            Product product = null;
            if (model.Id <= 0)
            {
                product = model;
                product.Date = DateTime.Now;
                product.User = CurrentUser;
                CurrentUser.Products.Add(product);
            }
            else
            {
                product = Locator.GetService<IProductsRepository>().Load(model.Id);
                product.Title = model.Title;
                product.Keywords = model.Keywords;
                product.CategoryId = model.CategoryId;
                product.UserCategoryId = model.UserCategoryId;
                product.User = CurrentUser;
                product.Field1 = model.Field1;
                product.Field3 = model.Field3;
                product.Field4 = model.Field4;
                product.Field5 = model.Field5;
                product.Field6 = model.Field6;
                product.Field8 = model.Field8;
                product.Field9 = model.Field9;
                product.Description = model.Description;
                product.Price = model.Price;
                product.Currency = model.Currency;
                product.Measure = model.Measure;
                product.MinimunLotSize = model.MinimunLotSize;
                product.MinimumLotMeasure = model.MinimumLotMeasure;
                product.VendorCountry = model.VendorCountry;
                product.DeliveryTime = model.DeliveryTime;
                product.DeliveryPossibilityDay = model.DeliveryPossibilityDay;
                product.DeliveryPossibilityMeasure = model.DeliveryPossibilityMeasure;
                product.DeliveryPossibilityTime = model.DeliveryPossibilityTime;
                product.ProductCode = model.ProductCode;
                product.ProductBox = model.ProductBox;

                // Информация о горячем товаре
                if (product.HotProducts == null)
                {
                    product.HotProducts = new HotProduct()
                        {
                            Clicks = 0,
                            EnableHotProduct = false,
                            PayedViews = 0,
                            Product = product,
                            Views = 0
                        };
                }
                product.HotProducts.EnableHotProduct = model.HotProducts.EnableHotProduct;
            }

            // Сохраняем изменения
            UsersRepository.SubmitChanges();

            // Сохраняем фотографию
            var productImageFile = Request.Files["ProductImage"];
            if (productImageFile != null && productImageFile.ContentLength > 0 && productImageFile.ContentType.Contains("image"))
            {
                var fileName = String.Format("prod-{0}-{1}{2}", product.Id,
                                             new Random(System.Environment.TickCount).Next(Int32.MaxValue),
                                             Path.GetExtension(productImageFile.FileName));
                FileUtils.SavePostedFile(productImageFile,"prodimage",fileName);
                product.Img = fileName;
                UsersRepository.SubmitChanges();
            }

            var imagesRepository = Locator.GetService<IProductImagesRepository>();

            if (images.Any())
            {

                foreach (var image in images)
                {
                    if (image != null && image.ContentLength > 0 && image.ContentType.Contains("image"))
                    {
                        var name = String.Format("prod-{0}-{1}{2}", product.Id,
                                             new Random(Environment.TickCount).Next(Int32.MaxValue),
                                             Path.GetExtension(image.FileName));

                        FileUtils.SavePostedFile(image, "prodimage", name);

                        imagesRepository.Add(new ProductImage
                            {
                                Image = name,
                                Product = product,
                                ProductId = product.Id,
                            });
                    }
                }

                imagesRepository.SubmitChanges();
            }

            if (!string.IsNullOrEmpty(deletedImages))
            {
                int[] imageIds = deletedImages.Split(',').Select(n => Convert.ToInt32(n)).ToArray();

                if (imageIds.Any())
                {
                    foreach (var imageId in imageIds)
                    {
                        var image = imagesRepository.Find(f => f.Id == imageId);
                        if (image != null)
                        {
                            imagesRepository.Delete(image);
                        }
                    }

                    imagesRepository.SubmitChanges();
                }
            }

            // Перенаправляемся на список продуктов
            return RedirectToAction("CategoryProducts",new {id = product.UserCategoryId});
        }