Exemplo n.º 1
0
        public async Task <IActionResult> AddProduct(AddProductModel addProductModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    BindDropdownForAddProduct(ref addProductModel);
                    return(View(addProductModel));
                }
                long customerId           = DBHelper.ParseInt64(Get(Constants.CustomerId));
                var  productModel         = ProductHelper.BindProductModel(addProductModel, customerId);
                var  productSizeModel     = ProductHelper.BindProductSizeModel(addProductModel);
                var  productColorModel    = ProductHelper.BindProductColorModel(addProductModel);
                var  productCategoryModel = ProductHelper.BindProductCategoryModel(addProductModel);
                var  productImageModel    = ProductHelper.BindProductImageModel(addProductModel);

                if (!addProductModel.ProductId.IsGreaterThenZero())
                {
                    long productId = iProduct.AddProduct(productModel, productCategoryModel, productColorModel, productImageModel, productSizeModel);
                    if (productId == RepositoryReturnCode.DataNotSaved.GetHashCode())
                    {
                        BindDropdownForAddProduct(ref addProductModel);
                        TempData[MessageConstant.ErrorMessage] = MessageConstant.ProductNotAdded;
                        return(View(addProductModel));
                    }
                    else if (productId.IsGreaterThenZero())
                    {
                        TempData[MessageConstant.SuccessMessage] = MessageConstant.ProductAdded;
                    }
                }
                else
                {
                    long productId = iProduct.UpdateProduct(productModel, productCategoryModel, productColorModel, productImageModel, productSizeModel);
                    if (productId == RepositoryReturnCode.DataNotSaved.GetHashCode())
                    {
                        BindDropdownForAddProduct(ref addProductModel);
                        TempData[MessageConstant.ErrorMessage] = MessageConstant.ProductNotUpdated;
                        return(View(addProductModel));
                    }
                    else if (productId.IsGreaterThenZero())
                    {
                        TempData[MessageConstant.SuccessMessage] = MessageConstant.ProductUpdated;
                    }
                }

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                LogHelper.ExceptionLog(ex.Message + Constants.ExceptionSeprator + ex.StackTrace);
                throw ex;
            }
        }