// After first login, get full table "user_prompts" from remote database and insert it in local database public async Task <string> SyncAllTableUserPrompts(string userId) { IEnumerable <UsersPromptsModel> userPromptsToSync; // Firstly, check if there are any data in "users_prompts" table for the current user // If there are no any data (could be user just registered or logged in from another device), send request "sync_all" to server to check if there are any saved prompts // If server contains any prompts, get them var data = DisplayUserPrompts(userId); if (data.Any()) { TruncateTableUsersPrompts(); } //return; // If local table "user-prompts" is empty, send request "sync_all" to server userPromptsToSync = new UsersPromptsModel[] { new UsersPromptsModel() { UserId = userId, IsSync = "sync_all" } }; // Send Json and get response from remote server var jsonResponse = await RestService(userPromptsToSync); // If there was an error in PostJsonDataAsync class, display message if (jsonResponse == null) { Message = restService.Message; Console.WriteLine(Message); return(Message); } // Insert prompts from server into table "users_prompts" foreach (var item in jsonResponse) { if (item.PromptId == 0) { Message = "There is no data on the server to sync"; Console.WriteLine(Message); return(Message); } int res = InsertCurrentPromptIntoTable(new UsersPromptsModel() { UserId = item.UserId, PromptId = item.PromptId, CreatedAt = item.CreatedAt, IsDone = item.IsDone, IsSync = "True" // Change attrubute "is_sync=true" for the new users promts received from the remote server }); if (res == 0) { Message = "There was a problem when inserting prompt id=" + item.UserPromptId; Console.WriteLine(Message); return(Message); } } return(Message = "Prompts have been synchronised successfully"); }
// Update user prompt public void UpdateUserPrompt(UsersPromptsModel userPrompt) { try { database.UpdateAsync(userPrompt); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
// Insert current prompt and get number of added rows public int InsertCurrentPromptIntoTable(UsersPromptsModel userPrompt) { try { string sqlGetLastId = "SELECT * FROM users_prompts ORDER BY userpromptid DESC LIMIT 1"; int lastId = database.ExecuteScalarAsync <int>(sqlGetLastId).Result; if (lastId == 0) { userPrompt.UserPromptId = 1; } else { userPrompt.UserPromptId = lastId + 1; } return(database.InsertAsync(userPrompt).Result); } catch (SQLiteException ex) { Console.WriteLine(ex.Message); return(0); } }