public async Task CreateOrUpdateDeviceSubscription()

in DeviceBridge/Providers/StorageProvider.cs [189:222]


        public async Task<DeviceSubscription> CreateOrUpdateDeviceSubscription(Logger logger, string deviceId, DeviceSubscriptionType subscriptionType, string callbackUrl, CancellationToken cancellationToken)
        {
            try
            {
                logger.Info("Creating or updating {subscriptionType} subscription for device {deviceId}", subscriptionType, deviceId);

                using SqlConnection connection = new SqlConnection(_connectionString);
                using SqlCommand command = new SqlCommand("upsertDeviceSubscription", connection)
                {
                    CommandType = CommandType.StoredProcedure,
                };

                command.Parameters.Add(new SqlParameter("@DeviceId", deviceId));
                command.Parameters.Add(new SqlParameter("@SubscriptionType", subscriptionType.ToString()));
                command.Parameters.Add(new SqlParameter("@CallbackUrl", await _encryptionService.Encrypt(logger, callbackUrl)));
                command.Parameters.Add(new SqlParameter("@CreatedAt", SqlDbType.DateTime)).Direction = ParameterDirection.Output;

                await connection.OpenAsync(cancellationToken);
                await command.ExecuteNonQueryAsync(cancellationToken);
                logger.Info("Created or updated {subscriptionType} subscription for device {deviceId}", subscriptionType, deviceId);

                return new DeviceSubscription()
                {
                    DeviceId = deviceId,
                    SubscriptionType = subscriptionType,
                    CallbackUrl = callbackUrl,
                    CreatedAt = (DateTime)command.Parameters["@CreatedAt"].Value,
                };
            }
            catch (Exception e)
            {
                throw TranslateSqlException(e);
            }
        }