示例#1
0
        public async Task <IHttpActionResult> Update(PackagesEditViewModel viewModel)
        {
            try
            {
                //Validation
                Package   package = Db.Packages.FirstOrDefault(p => p.Id == viewModel.Id);
                Validator result  = PackageValidator.Edit(viewModel, package);

                if (!result.Success)
                {
                    return(BadRequest(result.ErrorMessage));
                }

                viewModel.UpdatePackage(package);

                await Db.SaveChangesAsync();

                return(Ok("Package updated successfully"));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(BadRequest("Package was changed by another user"));
            }
            catch
            {
                return(BadRequest("An error has occurred"));
            }
        }
 public void EditMultipleProducts()
 {
     for (int i = 0; i < 10000; i++)
     {
         PackagesController    controller = new PackagesController();
         Random                rnd        = new Random();
         PackagesEditViewModel viewModel  = new PackagesEditViewModel()
         {
             Id            = rnd.Next(1, 100),
             ReceiveDate   = RandomDay(),
             PacketSize    = PacketSize.XS,
             ZipCode       = "2345-230",
             Location      = "Portugal",
             Weight        = rnd.Next(1, 100).ToString(),
             HasValueToPay = false,
             IsFragile     = false
         };
         controller.Update(viewModel);
     }
 }
示例#3
0
        public static Validator Edit(PackagesEditViewModel viewModel, Package package)
        {
            if (viewModel == null)
            {
                return(new Validator(false, "Erro"));
            }

            if (package == null)
            {
                return(new Validator(false, "Invalid package"));
            }

            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));
        }