Exemplo n.º 1
0
        /// <summary>
        /// Updates an existing device in the DocumentDB
        /// Throws a DeviceNotRegisteredException is the device does not already exist in the DocumentDB
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <dynamic> UpdateDeviceAsync(dynamic device)
        {
            string deviceId = DeviceSchemaHelper.GetDeviceID(device);

            dynamic existingDevice = await GetDeviceAsync(deviceId);

            if (existingDevice == null)
            {
                throw new DeviceNotRegisteredException(deviceId);
            }

            string incomingRid = DeviceSchemaHelper.GetDocDbRid(device);

            if (string.IsNullOrWhiteSpace(incomingRid))
            {
                // copy the existing _rid onto the incoming data if needed
                var existingRid = DeviceSchemaHelper.GetDocDbRid(existingDevice);
                if (string.IsNullOrWhiteSpace(existingRid))
                {
                    throw new InvalidOperationException("Could not find _rid property on existing device");
                }
                device._rid = existingRid;
            }

            string incomingId = DeviceSchemaHelper.GetDocDbId(device);

            if (string.IsNullOrWhiteSpace(incomingId))
            {
                // copy the existing id onto the incoming data if needed
                var existingId = DeviceSchemaHelper.GetDocDbId(existingDevice);
                if (string.IsNullOrWhiteSpace(existingId))
                {
                    throw new InvalidOperationException("Could not find id property on existing device");
                }
                device.id = existingId;
            }

            DeviceSchemaHelper.UpdateUpdatedTime(device);

            device = await _docDbRestUtil.UpdateDocumentAsync(device);

            return(device);
        }