private async Task AddProductAsync(ProductViewModel productVM, string newFileName)
        {
            using (var dbContextTransaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    Product  product  = new Product();
                    Category category = await _unitOfWork.Categories.GetAll().AsNoTracking().SingleOrDefaultAsync(c => c.Name == productVM.CategoryName);

                    product.CategoryId    = category.Id;
                    product.Name          = productVM.Name;
                    product.Description   = productVM.Description;
                    product.Price         = PriceHelper.DecimalToInt(productVM.Price);
                    product.SellerPrice   = product.Price;
                    product.ImageFileName = FileHelper.FilterImageName(newFileName);
                    product.Bidder        = 0;
                    await _unitOfWork.Products.CreateAsync(product);

                    await _unitOfWork.SaveAsync();

                    dbContextTransaction.Commit();
                }
                catch (Exception e)
                {
                    dbContextTransaction.Rollback(); // Rollbacking DB
                    throw new Exception("DB Transaction Failed. " + e.Message);
                }
            }
        }
        private async Task AddProductAsync(ProductViewModel productVM, string newFileName)
        {
            using (var dbContextTransaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    Product  product  = new Product();
                    Category category = await _unitOfWork.Categories.FindAsync(c => c.Name == productVM.CategoryName);

                    product.CategoryId    = category.Id;
                    product.Name          = productVM.Name;
                    product.Description   = productVM.Description;
                    product.Price         = PriceHelper.DecimalToInt(productVM.Price);
                    product.SellerPrice   = product.Price;
                    product.ImageFileName = FileHelper.FilterImageName(newFileName);
                    product.Bidder        = 0;
                    await _unitOfWork.Products.CreateAsync(product);

                    await _unitOfWork.SaveAsync();

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback(); // Rollbacking DB
                    ExceptionDispatchInfo.Capture(ex).Throw();
                }
            }
        }
        public async Task EditAsync(ProductViewModel productVM)
        {
            string newFileName = FileHelper.GetUniqueFileName(productVM.ImageFile?.FileName);
            string oldFileName = await UpdateProductAsync(productVM, newFileName);

            await FileHelper.UpdateImageAsync(productVM.ImageFile, oldFileName, newFileName);

            await _auctionService.StartSaleAsync(productVM.Id, PriceHelper.DecimalToInt(productVM.Price)); // Statring sale
        }
        public async Task <IActionResult> Edit(ProductViewModel productVM)
        {
            if (!ModelState.IsValid)
            {
                InitCategorySelector();
                return(View(productVM)); // Validation failed. Return to edit form
            }

            string newFileName = FileHelper.GetUniqueFileName(productVM.ImageFile?.FileName);
            string oldFileName = await UpdateProductAsync(productVM, newFileName);

            await FileHelper.UpdateImageAsync(productVM.ImageFile, oldFileName, newFileName);

            await _auctionService.StartSaleAsync(productVM.Id, PriceHelper.DecimalToInt(productVM.Price)); // Statring sale

            return(Redirect("~/Product/Index"));
        }
        private async Task <string> UpdateProductAsync(ProductViewModel productVM, string newFileName)
        {
            string oldFileName = null;

            // Update product from DB
            using (var dbContextTransaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    Product product = await _unitOfWork.Products.GetAll().AsNoTracking().SingleOrDefaultAsync(p => p.Id == productVM.Id);

                    oldFileName = product.ImageFileName;
                    product.Id  = productVM.Id;
                    Category category = await _unitOfWork.Categories.GetAll().AsNoTracking().SingleOrDefaultAsync(c => c.Name == productVM.CategoryName);

                    product.CategoryId  = category.Id;
                    product.Name        = productVM.Name;
                    product.Description = productVM.Description;
                    product.Price       = PriceHelper.DecimalToInt(productVM.Price);
                    product.SellerPrice = product.Price;

                    if (!string.IsNullOrEmpty(newFileName))
                    {
                        product.ImageFileName = newFileName;
                    }

                    product.Bidder = 0;
                    _unitOfWork.Products.Update(product);

                    await _unitOfWork.SaveAsync();

                    dbContextTransaction.Commit();
                }
                catch (Exception e)
                {
                    dbContextTransaction.Rollback(); // Rollbacking DB
                    throw new Exception("DB Transaction Failed. " + e.Message);
                }
            }

            return(oldFileName);
        }
        private async Task <string> UpdateProductAsync(ProductViewModel productVM, string newFileName)
        {
            string oldFileName = null;

            // Update product from DB
            using (var dbContextTransaction = _unitOfWork.BeginTransaction())
            {
                try
                {
                    Product product = await _unitOfWork.Products.FindAsync(productVM.Id);

                    oldFileName = product.ImageFileName;
                    product.Id  = productVM.Id;
                    Category category = await _unitOfWork.Categories.FindAsync(c => c.Name == productVM.CategoryName);

                    product.CategoryId  = category.Id;
                    product.Name        = productVM.Name;
                    product.Description = productVM.Description;
                    product.Price       = PriceHelper.DecimalToInt(productVM.Price);
                    product.SellerPrice = product.Price;

                    if (!string.IsNullOrEmpty(newFileName))
                    {
                        product.ImageFileName = newFileName;
                    }

                    product.Bidder = 0;
                    _unitOfWork.Products.Update(product);

                    await _unitOfWork.SaveAsync();

                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback(); // Rollbacking DB
                    ExceptionDispatchInfo.Capture(ex).Throw();
                }
            }

            return(oldFileName);
        }