public async Task <ActionResult> Update(LockPackageViewModel lockPackageViewModel) { int counter = 0; if (lockPackageViewModel != null && lockPackageViewModel.PackageLockStates != null) { var packageIdsFromRequest = lockPackageViewModel.PackageLockStates.Select(x => x.Id).ToList(); var packageRegistrationsFromDb = GetPackageRegistrationsForIds(packageIdsFromRequest); var packageStatesFromRequestDictionary = lockPackageViewModel.PackageLockStates.ToDictionary(x => x.Id); foreach (var packageRegistration in packageRegistrationsFromDb) { if (packageStatesFromRequestDictionary.TryGetValue(packageRegistration.Id, out var packageStateRequest)) { if (packageRegistration.IsLocked != packageStateRequest.IsLocked) { packageRegistration.IsLocked = packageStateRequest.IsLocked; counter++; } } } if (counter > 0) { await _packageRegistrationRepository.CommitChangesAsync(); } } TempData["Message"] = string.Format(CultureInfo.InvariantCulture, $"Lock state was updated for {counter} packages."); return(View(nameof(Index), lockPackageViewModel)); }
public async Task UpdateAppliesChangesToTheCorrectPackages() { // Arrange var packageRegistrationsInDb = Enumerable.Range(1, 10).Select(i => new PackageRegistration() { Id = "Test" + i, IsLocked = false, Key = i }).ToList(); var packageRegistrationsRepository = new Mock <IEntityRepository <PackageRegistration> >(); packageRegistrationsRepository.Setup(x => x.GetAll()).Returns(packageRegistrationsInDb.AsQueryable()); var controller = new LockPackageController(packageRegistrationsRepository.Object); var viewModel = new LockPackageViewModel() { PackageLockStates = new List <PackageLockState>() { new PackageLockState() { Id = "Test1", IsLocked = true }, new PackageLockState() { Id = "Test5", IsLocked = true } } }; // Act var result = await controller.Update(viewModel); // Assert Assert.Equal(2, packageRegistrationsInDb.Count(x => x.IsLocked)); Assert.True(packageRegistrationsInDb.First(x => x.Id == "Test1").IsLocked); Assert.True(packageRegistrationsInDb.First(x => x.Id == "Test5").IsLocked); var viewResult = ResultAssert.IsView <LockPackageViewModel>(result, "Index"); Assert.Equal(2, viewResult.PackageLockStates.Count(x => x.IsLocked)); Assert.True(viewResult.PackageLockStates.First(x => x.Id == "Test1").IsLocked); Assert.True(viewResult.PackageLockStates.First(x => x.Id == "Test5").IsLocked); }
public virtual ActionResult Index() { var model = new LockPackageViewModel(); return(View(model)); }