Example #1
0
        /// <summary>
        /// SQL for deleting a preference key
        /// </summary>
        private async Task <int> DeletePreferenceKey(PrefKeyDataModel prefKey, PrefKeyDataModel existing)
        {
            Log.LogDebug($"PreferenceRepository/DeletePreferenceKey: user preference={JsonConvert.SerializeObject(prefKey)}))')");

            var upsertedCount = 0;

            if (existing != null)
            {
                const string delete =
                    @"DELETE FROM PreferenceKey
              WHERE PreferenceKeyID = @PreferenceKeyID";
                upsertedCount = await ExecuteWithAsyncPolicy(delete, prefKey);

                Log.LogDebug($"PreferenceRepository/DeletePreferenceKey: deleted {upsertedCount} rows");
            }
            else
            {
                Log.LogDebug($"PreferenceRepository/DeletePreferenceKey: No preference key found to delete");
            }

            return(upsertedCount);
        }
Example #2
0
        /// <summary>
        /// SQL for updating a preference key
        /// </summary>
        private async Task <int> UpdatePreferenceKey(PrefKeyDataModel prefKey, PrefKeyDataModel existing)
        {
            Log.LogDebug($"PreferenceRepository/UpdatePreferenceKey: preference key={JsonConvert.SerializeObject(prefKey)}))')");

            var upsertedCount = 0;

            if (existing != null)
            {
                var update = $@"UPDATE PreferenceKey
              SET KeyName = @KeyName
              WHERE PreferenceKeyID = @PreferenceKeyID";

                upsertedCount = await ExecuteWithAsyncPolicy(update, prefKey);

                Log.LogDebug($"PreferenceRepository/UpdatePreferenceKey: upserted {upsertedCount} rows");
            }
            else
            {
                Log.LogDebug($"PreferenceRepository/UpdatePreferenceKey: No preference key found to update");
            }

            return(upsertedCount);
        }
Example #3
0
        /// <summary>
        /// Create, update and delete of a preference key
        /// </summary>
        private async Task <int> UpsertPreferenceKey(PreferenceEventType eventType, Guid?prefKeyUID, string prefKeyName)
        {
            if (eventType == PreferenceEventType.CreatePreferenceKeyEvent || eventType == PreferenceEventType.UpdatePreferenceKeyEvent)
            {
                //Check name doesn't already exist as it must be unique
                if (await GetPreferenceKey(prefKeyName: prefKeyName) != null)
                {
                    Log.LogDebug($"PreferenceRepository/StorePreferenceKey: No action as preference key name already exists");
                    return(0);
                }
            }
            var upsertedCount = 0;
            var existing      = await GetPreferenceKey(prefKeyUID);

            var prefKey = new PrefKeyDataModel
            {
                KeyName          = prefKeyName,
                PreferenceKeyUID = prefKeyUID?.ToString(),
                PreferenceKeyID  = existing?.PreferenceKeyID ?? 0
            };

            if (eventType == PreferenceEventType.CreatePreferenceKeyEvent)
            {
                upsertedCount = await CreatePreferenceKey(prefKey, existing);
            }

            if (eventType == PreferenceEventType.UpdatePreferenceKeyEvent)
            {
                upsertedCount = await UpdatePreferenceKey(prefKey, existing);
            }

            if (eventType == PreferenceEventType.DeletePreferenceKeyEvent)
            {
                upsertedCount = await DeletePreferenceKey(prefKey, existing);
            }
            return(upsertedCount);
        }
Example #4
0
        /// <summary>
        /// SQL for creating a preference key
        /// </summary>
        private async Task <int> CreatePreferenceKey(PrefKeyDataModel prefKey, PrefKeyDataModel existing)
        {
            Log.LogDebug($"PreferenceRepository/CreatePreferenceKey: preference key={JsonConvert.SerializeObject(prefKey)}))')");

            var upsertedCount = 0;

            if (existing == null)
            {
                var insert = @"INSERT PreferenceKey
              (PreferenceKeyUID, KeyName)
                VALUES
              (@PreferenceKeyUID, @KeyName)";

                upsertedCount = await ExecuteWithAsyncPolicy(insert, prefKey);

                Log.LogDebug($"PreferenceRepository/CreatePreferenceKey: inserted {upsertedCount} rows");
            }
            else
            {
                Log.LogDebug("PreferenceRepository/CreatePreferenceKey: No action as preference key already exists.");
            }

            return(upsertedCount);
        }