Example #1
0
        public JsonResult UploadProduct(HttpPostedFileBase file)
        {
            try
            {
                if (file != null)
                {
                    var    fileStream        = file.InputStream;
                    var    excelContent      = ExcelImporter.ParseExcel(new MemoryStream(ExcelImporter.ReadFully(fileStream)));
                    string json              = JsonConvert.SerializeObject(excelContent);
                    var    importList        = JsonConvert.DeserializeObject <List <ProductImportScheme> >(json);
                    var    addProductRequest = new List <AddProductRequest>();

                    int row = 1;
                    foreach (var item in importList)
                    {
                        if (string.IsNullOrEmpty(item.Barcode))
                        {
                            throw new Exception($"Row = {row}, Barcode can not be empty");
                        }
                        else if (string.IsNullOrEmpty(item.Description))
                        {
                            throw new Exception($"Row = {row}, Description can not be empty");
                        }
                        else if (string.IsNullOrEmpty(item.ProductCode))
                        {
                            throw new Exception($"Row = {row}, ProductCode can not be empty");
                        }
                        else if (string.IsNullOrEmpty(item.ProductName))
                        {
                            throw new Exception($"Row = {row}, ProductName can not be empty");
                        }
                        else if (item.RetailPrice < 0)
                        {
                            throw new Exception($"Row = {row}, Price can not be lower than 0");
                        }
                        else if (item.Quantity < 0)
                        {
                            throw new Exception($"Row = {row}, Quantity can not be lower than 0");
                        }

                        addProductRequest.Add(new AddProductRequest()
                        {
                            Barcode      = item.Barcode,
                            CreatedBy    = "admin",
                            CreatedDate  = DateTime.Now,
                            Description  = item.Description,
                            ModifiedBy   = "admin",
                            ModifiedDate = DateTime.Now,
                            ProductCode  = item.ProductCode,
                            ProductName  = item.ProductName,
                            Quantity     = (int)item.Quantity,
                            RetailPrice  = item.RetailPrice
                        });

                        row++;
                    }

                    WCF.Service1Client client = new WCF.Service1Client();

                    var response = client.AddProducts(addProductRequest.ToArray());

                    return(Json(new { Success = true }));
                }
                else
                {
                    return(Json(new { Success = false, ErrorMessage = "Please choose file" }));
                }
            }
            catch (Exception _ex)
            {
                return(Json(new { Success = false, ErrorMessage = _ex.Message }));
            }
        }