InitializeSubscriber NET C# » History » Version 1
Tomek Dziemidowicz, 2019-07-04 09:01 PM
| 1 | 1 | Tomek Dziemidowicz | h1. InitializeSubscriber NET C# |
|---|---|---|---|
| 2 | |||
| 3 | <pre><code class="csharp"> |
||
| 4 | /// <summary> |
||
| 5 | /// Reinitialize subscriber |
||
| 6 | /// </summary> |
||
| 7 | /// <param name="subscriberId">identifier of subscriber</param> |
||
| 8 | public void ReinitializeDatabase(string subscriberId) |
||
| 9 | { |
||
| 10 | //getting data from server |
||
| 11 | var request = new RestRequest("InitializeSubscriber/{subscriberUUID}", Method.GET); |
||
| 12 | request.AddUrlSegment("subscriberUUID", subscriberId); |
||
| 13 | request.AddHeader("Accept", "*/*"); |
||
| 14 | IRestResponse response = wsClient.Execute(request); |
||
| 15 | Dictionary<string, string> dbSchema = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content); |
||
| 16 | |||
| 17 | //we need to sort by key |
||
| 18 | var tmp = dbSchema.OrderBy(key => key.Key); |
||
| 19 | var dbSchemaSorted = tmp.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); |
||
| 20 | |||
| 21 | using (SQLiteConnection conn = new SQLiteConnection(this.connString)) |
||
| 22 | { |
||
| 23 | using (SQLiteCommand cmd = new SQLiteCommand()) |
||
| 24 | { |
||
| 25 | cmd.Connection = conn; |
||
| 26 | conn.Open(); |
||
| 27 | |||
| 28 | SQLiteHelper sh = new SQLiteHelper(cmd); |
||
| 29 | |||
| 30 | sh.BeginTransaction(); |
||
| 31 | |||
| 32 | try |
||
| 33 | { |
||
| 34 | foreach (KeyValuePair<string, string> entry in dbSchemaSorted) |
||
| 35 | if (!entry.Key.StartsWith("00000")) |
||
| 36 | { |
||
| 37 | sh.Execute(entry.Value); |
||
| 38 | } |
||
| 39 | sh.Commit(); |
||
| 40 | } |
||
| 41 | catch (Exception ex) |
||
| 42 | { |
||
| 43 | sh.Rollback(); |
||
| 44 | throw ex; |
||
| 45 | } |
||
| 46 | |||
| 47 | conn.Close(); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | </code></pre> |