public override async Task RunAsync()
        {
            if (this.IsShuttingDown || this.Pause)
            {
                return;
            }

            LuceneIndex.ClearLuceneIndex();

            var productService = IoC.Container.GetInstance <IProductService>();
            var postService    = IoC.Container.GetInstance <IPostService>();

            foreach (var product in await productService.GetAllForLuceneIndex())
            {
                LuceneIndex.ClearLuceneIndexRecord(product.Id);
                LuceneIndex.AddUpdateLuceneIndex(new LuceneSearchModel
                {
                    ProductId     = product.Id,
                    Title         = product.Title,
                    Image         = product.Image,
                    Description   = product.Description.RemoveHtmlTags(),
                    Category      = "کالا‌ها",
                    SlugUrl       = product.SlugUrl,
                    Price         = product.Price.ToString(CultureInfo.InvariantCulture),
                    ProductStatus = product.ProductStatus.ToString()
                });
            }

            foreach (var post in await postService.GetAllForLuceneIndex())
            {
                LuceneIndex.ClearLucenePostIndexRecord(post.Id);
                LuceneIndex.AddUpdateLuceneIndex(new LuceneSearchModel
                {
                    PostId      = post.Id,
                    Title       = post.Title,
                    Image       = post.Image,
                    Description = post.Description.RemoveHtmlTags(),
                    Category    = post.Category,
                    SlugUrl     = post.SlugUrl
                });
            }
        }
예제 #2
0
        public virtual async Task <ActionResult> DeleteProduct(int id)
        {
            _productService.DeleteProduct(id);

            var deletedImages = await _productService.GetProductImages(id);

            await _unitOfWork.SaveAllChangesAsync();

            LuceneIndex.ClearLuceneIndexRecord(id);

            foreach (var productImage in deletedImages)
            {
                var path      = Server.MapPath("~/UploadedFiles/ProductImages/" + productImage);
                var thumbPath = Server.MapPath("~/UploadedFiles/ProductImages/Thumbs/" + productImage);
                System.IO.File.Delete(path);
                System.IO.File.Delete(thumbPath);
            }

            return(Json(true));
        }
        public virtual async Task <ActionResult> ReIndex()
        {
            LuceneIndex.ClearLuceneIndex();

            var productService = IoC.Container.GetInstance <IProductService>();
            var postService    = IoC.Container.GetInstance <IPostService>();

            foreach (var product in await productService.GetAllForLuceneIndex())
            {
                LuceneIndex.ClearLuceneIndexRecord(product.Id);
                LuceneIndex.AddUpdateLuceneIndex(new LuceneSearchModel
                {
                    ProductId     = product.Id,
                    Title         = product.Title,
                    Image         = product.Image,
                    Description   = product.Description.RemoveHtmlTags(),
                    Category      = "کالا‌ها",
                    SlugUrl       = product.SlugUrl,
                    Price         = product.Price.ToString(CultureInfo.InvariantCulture),
                    ProductStatus = product.ProductStatus.ToString()
                });
            }

            foreach (var post in await postService.GetAllForLuceneIndex())
            {
                LuceneIndex.ClearLucenePostIndexRecord(post.Id);
                LuceneIndex.AddUpdateLuceneIndex(new LuceneSearchModel
                {
                    PostId      = post.Id,
                    Title       = post.Title,
                    Image       = post.Image,
                    Description = post.Description.RemoveHtmlTags(),
                    Category    = post.Category,
                    SlugUrl     = post.SlugUrl
                });
            }
            return(Content("ReIndexing Complete."));
        }
예제 #4
0
        public virtual async Task <ActionResult> AddProduct(AddProductViewModel productModel)
        {
            if (!ModelState.IsValid)
            {
                ViewData["CategoriesSelectList"] = new MultiSelectList(
                    (await _categoryService.GetAll())
                    .Union(productModel.Categories.Select(x => new Category {
                    Name = x
                }),
                           new ProductCategoryComparer()), "Name", "Name",
                    productModel.Categories);

                return(View(productModel));
            }

            if (productModel.Count <= 0)
            {
                productModel.Count         = 0;
                productModel.ProductStatus = ProductStatus.NotAvailable;;
            }

            var addedProductImages = productModel.Images
                                     .Where(image => image.Id == null)
                                     .Select(image => new { image.Url, image.Name, image.ThumbnailUrl })
                                     .ToList();

            var deletedProductImages = new List <ProductImage>();

            var product = new DomainClasses.Product();

            foreach (var productImage in productModel.Images.Where(image => image.Id == null))
            {
                productImage.Url          = Url.Content("~/UploadedFiles/ProductImages/" + Path.GetFileName(getFilePath(productImage.Name, Server.MapPath("~/UploadedFiles/ProductImages"))));
                productImage.ThumbnailUrl = Url.Content("~/UploadedFiles/ProductImages/Thumbs/" + Path.GetFileName(getFilePath(productImage.Name, Server.MapPath("~/UploadedFiles/ProductImages/Thumbs"))));
                productImage.DeleteUrl    = "";
            }

            _mappingEngine.Map(productModel, product);

            if (productModel.Id.HasValue)
            {
                deletedProductImages = (await _productService.EditProduct(product)).ToList();
                TempData["message"]  = "کالای مورد نظر با موفقیت ویرایش شد";
            }
            else
            {
                product.Title          = productModel.Name;
                product.PostedDate     = DateTime.Now;
                product.PostedByUserId = 1;
                await _productService.AddProduct(product);

                TempData["message"] = "کالای جدید با موفقیت ثبت شد";
            }

            await _unitOfWork.SaveAllChangesAsync();

            foreach (var productImage in addedProductImages)
            {
                var path      = getFilePath(productImage.Name, Server.MapPath("~/UploadedFiles/ProductImages"));
                var thumbPath = getFilePath(productImage.Name, Server.MapPath("~/UploadedFiles/ProductImages/Thumbs"));

                System.IO.File.Move(Server.MapPath("~/Content/tmp/" + Path.GetFileName(productImage.Url)), path);
                System.IO.File.Move(Server.MapPath("~/Content/tmp/" + Path.GetFileName(productImage.ThumbnailUrl)), thumbPath);
            }

            foreach (var productImage in deletedProductImages)
            {
                var path      = Server.MapPath("~/UploadedFiles/ProductImages/" + productImage.Name);
                var thumbPath = Server.MapPath("~/UploadedFiles/ProductImages/Thumbs/" + productImage.Name);
                System.IO.File.Delete(path);
                System.IO.File.Delete(thumbPath);
            }

            if (productModel.Id.HasValue)
            {
                LuceneIndex.ClearLuceneIndexRecord(productModel.Id.Value);
            }

            //Index the new product lucene.NET
            LuceneIndex.AddUpdateLuceneIndex(new LuceneSearchModel
            {
                ProductId     = product.Id,
                Description   = product.Body.RemoveHtmlTags(),
                Title         = product.Title,
                ProductStatus = product.ProductStatus.ToString(),
                Price         = product.Prices
                                .OrderByDescending(productPrice => productPrice.Date)
                                .Select(productPrice => productPrice.Price).
                                FirstOrDefault().
                                ToString(CultureInfo.InvariantCulture),
                Image = product.Images.OrderBy(image => image.Order)
                        .Select(image => image.ThumbnailUrl)
                        .FirstOrDefault(),
                SlugUrl  = product.SlugUrl,
                Category = "کالا‌ها"
            });


            return(RedirectToAction(MVC.Product.Admin.ActionNames.Index));
        }