Project

General

Profile

Actions

InitializeSubscriber NET C#

/// <summary>
/// Reinitialize subscriber
/// </summary>
/// <param name="subscriberId">identifier of subscriber</param>
public void ReinitializeDatabase(string subscriberId)
{
    //getting data from server
    var request = new RestRequest("InitializeSubscriber/{subscriberUUID}", Method.GET);
    request.AddUrlSegment("subscriberUUID", subscriberId);
    request.AddHeader("Accept", "*/*");
    IRestResponse response = wsClient.Execute(request);
    Dictionary<string, string> dbSchema = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content);

    //we need to sort by key
    var tmp = dbSchema.OrderBy(key => key.Key);
    var dbSchemaSorted = tmp.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);

    using (SQLiteConnection conn = new SQLiteConnection(this.connString))
    {
        using (SQLiteCommand cmd = new SQLiteCommand())
        {
            cmd.Connection = conn;
            conn.Open();

            SQLiteHelper sh = new SQLiteHelper(cmd);

            sh.BeginTransaction();

            try
            {
                foreach (KeyValuePair<string, string> entry in dbSchemaSorted)
                    if (!entry.Key.StartsWith("00000"))
                    {
                        sh.Execute(entry.Value);
                    }
                sh.Commit();
            }
            catch (Exception ex)
            {
                sh.Rollback();
                throw ex;
            }

            conn.Close();
        }
    }
}

Updated by Tomek Dziemidowicz over 5 years ago · 1 revisions

Go to top