public async Task <ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model) { bool isModelValid = ModelState.IsValid; bool onlyValidating = (button != null && button.ToLower().Trim() == "check"); if (object.ReferenceEquals(null, model) || (model.GetType() == typeof(object))) { model = new UnregisteredDeviceModel(); } //reset flag model.IsDeviceIdUnique = false; if (model.IsDeviceIdSystemGenerated) { //clear the model state of errors prior to modifying the model ModelState.Clear(); //assign a system generated device Id model.DeviceId = Guid.NewGuid().ToString(); //validate the model isModelValid = TryValidateModel(model); } if (isModelValid) { bool deviceExists = await GetDeviceExistsAsync(model.DeviceId); model.IsDeviceIdUnique = !deviceExists; if (model.IsDeviceIdUnique) { if (!onlyValidating) { return(await Add(model)); } } else { ModelState.AddModelError("DeviceId", Strings.DeviceIdInUse); } } return(PartialView("_AddDeviceCreate", model)); }
public async Task <ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model) { bool isModelValid = ModelState.IsValid; bool onlyValidating = (button != null && button.ToLower().Trim() == "check"); if (ReferenceEquals(null, model) || (model.GetType() == typeof(object))) { model = new UnregisteredDeviceModel(); } if (_apiRegistrationRepository.IsApiRegisteredInAzure()) { try { List <DeviceModel> devices = await GetDevices(); ViewBag.AvailableIccids = _cellularExtensions.GetListOfAvailableIccids(devices); ViewBag.CanHaveIccid = true; } catch (CellularConnectivityException) { ViewBag.CanHaveIccid = false; } } else { ViewBag.CanHaveIccid = false; } //reset flag model.IsDeviceIdUnique = false; if (model.IsDeviceIdSystemGenerated) { //clear the model state of errors prior to modifying the model ModelState.Clear(); //assign a system generated device Id while (true) { var random = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0); string deviceId = $"CoolingSampleDevice_{random % 10000:d4}"; if (!await GetDeviceExistsAsync(deviceId)) { model.DeviceId = deviceId; break; } } //validate the model isModelValid = TryValidateModel(model); } if (isModelValid) { bool deviceExists = !model.IsDeviceIdSystemGenerated && await GetDeviceExistsAsync(model.DeviceId); model.IsDeviceIdUnique = !deviceExists; if (model.IsDeviceIdUnique) { if (!onlyValidating) { return(await Add(model)); } } else { ModelState.AddModelError("DeviceId", Strings.DeviceIdInUse); } } return(PartialView("_AddDeviceCreate", model)); }
public async Task <ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model) { bool isModelValid = this.ModelState.IsValid; bool onlyValidating = (button != null && button.ToLower().Trim() == "check"); if (ReferenceEquals(null, model) || (model.GetType() == typeof(object))) { model = new UnregisteredDeviceModel(); } if (this._apiRegistrationRepository.IsApiRegisteredInAzure()) { try { var devices = await this.GetDevices(); this.ViewBag.AvailableIccids = this._cellularService.GetListOfAvailableIccids(devices); this.ViewBag.CanHaveIccid = true; } catch (CellularConnectivityException) { this.ViewBag.CanHaveIccid = false; } } else { this.ViewBag.CanHaveIccid = false; } //reset flag model.IsDeviceIdUnique = false; if (model.IsDeviceIdSystemGenerated) { //clear the model state of errors prior to modifying the model this.ModelState.Clear(); //assign a system generated device Id model.DeviceId = Guid.NewGuid().ToString(); //validate the model isModelValid = this.TryValidateModel(model); } if (isModelValid) { bool deviceExists = await this.GetDeviceExistsAsync(model.DeviceId); model.IsDeviceIdUnique = !deviceExists; if (model.IsDeviceIdUnique) { if (!onlyValidating) { return(await this.Add(model)); } } else { this.ModelState.AddModelError("DeviceId", Strings.DeviceIdInUse); } } return(this.PartialView("_AddDeviceCreate", model)); }