예제 #1
0
        private string GenerateTrackingCode(PackagesCreateViewModel viewModel)
        {
            try
            {
                string isoCode        = GetIso(viewModel.Location);
                string zipCode        = viewModel.ZipCode.Contains("-") ? viewModel.ZipCode.Replace("-", "") : viewModel.ZipCode;
                string identification = Guid.NewGuid().ToString().Split('-').LastOrDefault().Substring(0, 6);
                string date           = viewModel.ReceiveDate.Value.ToString("dd/MM/yy");

                string[] dateSplit       = date.Split('/');
                int      day             = int.Parse(dateSplit[0].ElementAt(0).ToString());
                int      month           = int.Parse(dateSplit[1].ElementAt(1).ToString());
                int      controlDigitOne = (day + month + 20);
                while (controlDigitOne > 10)
                {
                    controlDigitOne = controlDigitOne.ToString().Select(digit => int.Parse(digit.ToString())).ToList().Aggregate((a, b) => a + b);
                }

                int isoNumericCode  = GetIsoNumericCode(viewModel.Location);
                int controlDigitTWo = (isoNumericCode + int.Parse(dateSplit[2].ElementAt(1).ToString())).ToString().Select(digit => int.Parse(digit.ToString())).ToList().Aggregate((a, b) => a + b);
                return($"{isoCode}-{zipCode}-{identification}-{date.Replace("/", "")}-{controlDigitOne}{controlDigitTWo}");
            }
            catch
            {
                return("");
            }
        }
예제 #2
0
        public IActionResult Create(PackagesCreateViewModel model)
        {
            int recipientId = this.context.Users
                .FirstOrDefault(u => u.Username == model.Recipient)
                .Id;

            var package = new Package
            {
                Description = model.Description,
                Weight = model.Weight,
                ShippingAddress = model.ShippingAddress,
                RecipientId = recipientId
            };

            this.context.Packages.Add(package);
            this.context.SaveChanges();

            return this.RedirectToAction("/");
        }
 public void AddMultipleProducts()
 {
     for (int i = 0; i < 10000; i++)
     {
         PackagesController      controller = new PackagesController();
         Random                  rnd        = new Random();
         PackagesCreateViewModel viewModel  = new PackagesCreateViewModel()
         {
             ReceiveDate   = RandomDay(),
             PacketSize    = PacketSize.XS,
             ZipCode       = "2345-230",
             Location      = "Portugal",
             Weight        = rnd.Next(1, 100).ToString(),
             HasValueToPay = false,
             IsFragile     = false
         };
         controller.Create(viewModel);
     }
 }
예제 #4
0
        public static Validator Create(PackagesCreateViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(new Validator(false, "Erro"));
            }

            if (viewModel.ReceiveDate == null)
            {
                return(new Validator(false, "Please add the package receive date"));
            }

            if (viewModel.PacketSize == null)
            {
                return(new Validator(false, "Please select a packet size"));
            }

            if (viewModel.Weight == null)
            {
                return(new Validator(false, "Please add the weight of the package"));
            }

            if (viewModel.Address == null)
            {
                return(new Validator(false, "Please add the address"));
            }

            if (viewModel.ZipCode == null)
            {
                return(new Validator(false, "Please add the zipcode of the address"));
            }

            if (viewModel.Location == null)
            {
                return(new Validator(false, "Please add the location"));
            }

            return(new Validator(true));
        }
예제 #5
0
        public async Task <IHttpActionResult> Create(PackagesCreateViewModel viewModel)
        {
            try
            {
                //Validation
                Validator result = PackageValidator.Create(viewModel);

                if (!result.Success)
                {
                    return(BadRequest(result.ErrorMessage));
                }
                //Create Package
                string  trackingCode = GenerateTrackingCode(viewModel);
                Package package      = viewModel.ToPackage(trackingCode);
                Db.Packages.Add(package);
                await Db.SaveChangesAsync();

                return(Ok("Package created successfully"));
            }
            catch
            {
                return(BadRequest("An error has occurred"));
            }
        }
예제 #6
0
        public async Task MultipleCreate(HttpPostedFileBase file)
        {
            string CurrentFilePath;

            if (File.Exists(System.Web.Hosting.HostingEnvironment.MapPath("~/File/" + file.FileName)))
            {
                Random rnd    = new Random();
                int    random = rnd.Next();
                CurrentFilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/File/" + file.FileName + random);
                file.SaveAs(CurrentFilePath);
            }
            else
            {
                CurrentFilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/File/" + file.FileName);
                file.SaveAs(CurrentFilePath);
            }
            ExcelFile excel = new ExcelFile(CurrentFilePath, 1);

            for (int i = 2; i < excel.Range.Rows.Count; i++)
            {
                //Ex. Iterate through the row's data and put in a string array
                String[] rowData = new String[excel.Range.Rows[i].Columns.Count];
                for (int j = 0; j < excel.Range.Rows[i].Columns.Count; j++)
                {
                    rowData[j] = Convert.ToString(excel.Range.Rows[i].Cells[1, j + 1].Value2);
                }

                if (String.IsNullOrWhiteSpace(rowData[0]))
                {
                    continue;
                }

                double   d    = double.Parse(rowData[0]);
                DateTime date = DateTime.FromOADate(d);
                Enum.TryParse(rowData[1], out PacketSize packet);
                string weight   = rowData[2];
                string zipCode  = rowData[3];
                string location = rowData[4];
                bool.TryParse(rowData[5], out bool isFragile);
                bool.TryParse(rowData[6], out bool hasValueToPay);

                PackagesCreateViewModel viewModel = new PackagesCreateViewModel(zipCode, location, date);

                string trackingCode = GenerateTrackingCode(viewModel);

                if (string.IsNullOrWhiteSpace(trackingCode))
                {
                    continue;
                }

                Package package = new Package
                {
                    ReceiveDate   = viewModel.ReceiveDate.Value,
                    PacketSize    = packet,
                    Weight        = weight,
                    IsFragile     = isFragile,
                    HasValueToPay = hasValueToPay,
                    TrackingCode  = trackingCode,
                    Status        = Status.Received
                };
                Db.Packages.Add(package);
            }
            await Db.SaveChangesAsync();

            excel.Wb.Close(0);

            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Equals("EXCEL"))  //Process Excel?
                {
                    clsProcess.Kill();
                }
            }

            if (File.Exists(CurrentFilePath))
            {
                File.Delete(CurrentFilePath);
            }
        }