public async Task <IActionResult> Edit(Device device)
        {
            Device newDevice = new Device(device.Name, device.Lake, device.Location, device.Depth);
            await _dbContext.Devices.AddAsync(newDevice);

            List <AppUserDevice> newAppUserAssociations = new List <AppUserDevice>();

            foreach (AppUser user in _identityDbContext.Users)
            {
                if (await _deviceStorage.IsDeviceAccessibleForUserAsync(user, device))
                {
                    newAppUserAssociations.Add(new AppUserDevice(user.Id, newDevice.Id));
                }
            }
            await _dbContext.AppUserDeviceAssociation.AddRangeAsync(newAppUserAssociations);

            await _dbContext.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
示例#2
0
        public async Task <IActionResult> EditDevices([Required] string id)
        {
            AppUser user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                ModelState.AddModelError("", "The user no longer exists.");
                return(View(nameof(Index)));
            }
            List <Device> accessibleDevices    = new List <Device>();
            List <Device> notAccessibleDevices = new List <Device>();

            foreach (Device device in _dbContext.Devices)
            {
                List <Device> list = await _deviceStorage.IsDeviceAccessibleForUserAsync(user, device) ? accessibleDevices : notAccessibleDevices;

                list.Add(device);
            }

            return(View(new AppUserDevicesEditViewModel(user, accessibleDevices, notAccessibleDevices)));
        }