Ejemplo n.º 1
0
        public static Message ParseShippingRateFile(string filePath, List <ShippingRateDB> shippingRates, bool hasHeader)
        {
            var message = new Message();

            try
            {
                using (var reader = new StreamReader(filePath))
                    using (var csvReader = new CsvReader(reader))
                    {
                        csvReader.Configuration.HasHeaderRecord = hasHeader;
                        while (csvReader.Read())
                        {
                            var shippingRate = new ShippingRateDB();

                            int ID;
                            csvReader.TryGetField <int>("ID", out ID);
                            shippingRate.Id = ID;

                            int?WeightFrom = null;
                            csvReader.TryGetField <int?>("WeightFrom", out WeightFrom);
                            shippingRate.WeightFrom = WeightFrom;

                            int?WeightTo;
                            csvReader.TryGetField <int?>("WeightTo", out WeightTo);
                            shippingRate.WeightTo = WeightTo;

                            string Unit = null;
                            csvReader.TryGetField <string>("Unit", out Unit);
                            shippingRate.Unit = Unit;

                            decimal?Rate = null;
                            csvReader.TryGetField <decimal?>("Rate", out Rate);
                            shippingRate.Rate = Rate;


                            shippingRates.Add(shippingRate);
                        }
                    }
                message.IsSucess = true;
            }
            catch (Exception ex)
            {
                message.SetMessage(false, string.Format("Shipping Rates file upload failed: {0}", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
            }
            return(message);
        }
Ejemplo n.º 2
0
        public int DoUpadateOrInsertShippingRate(ShippingRateDB model, bool isCreate)
        {
            using (var context = new EisInventoryContext())
            {
                // get the exising product from db
                var shippingRate = context.shippingrates.FirstOrDefault(x => x.Id == model.Id);

                if (shippingRate != null)
                {
                    // let's update its data except for Products.Name
                    shippingRate.Rate       = model.Rate;
                    shippingRate.Unit       = model.Unit;
                    shippingRate.WeightFrom = model.WeightFrom;
                    shippingRate.WeightTo   = model.WeightTo;
                }
                else
                {
                    if (!isCreate)
                    {
                        return(0);
                    }
                    // add first the product item
                    context.shippingrates.Add(new shippingrate
                    {
                        Rate       = model.Rate,
                        Unit       = model.Unit,
                        WeightFrom = model.WeightFrom,
                        WeightTo   = model.WeightTo,
                    });
                }
                // save the change first to the product
                context.SaveChanges();

                return(1);
            }
        }