Exemplo n.º 1
0
        private void Button_Import_Clicked(object sender, EventArgs e)
        {
            if (_isBusy)
            {
                return;
            }
            _isBusy = true;

            Label_Results.Text = "Importing data...";

            List <EventTeamMatch> matches = (List <EventTeamMatch>)SqlDataEventTeamMatches.GetItemsAsync().Result;

            string myDocumentsPath = App.GetMyDocumentsPath();

            string[] filenames = Directory.GetFiles(myDocumentsPath, $"{App.AppYear}_{App.currFRCEventKey}*.json");

            foreach (string path in filenames)
            {
                Label_Results.Text += $"\n\n{path}";

                string allMatchData = File.ReadAllText(path);

                JArray matchJsonData = JArray.Parse(allMatchData);

                Label_Results.Text += $"\n\nMatches found: {matchJsonData.Count}";

                int addedCount      = 0;
                int updatedCount    = 0;
                int notChangedCount = 0;

                foreach (JObject obj in matchJsonData)
                {
                    EventTeamMatch item    = EventTeamMatch.Parse(obj.ToString());
                    EventTeamMatch oldItem = matches.FirstOrDefault(p => p.Uuid == item.Uuid);

                    if (oldItem == null)
                    {
                        item.Changed = 0; // downloaded records are excluded from uploading
                        SqlDataEventTeamMatches.AddItemAsync(item);
                        addedCount++;
                    }
                    else if (oldItem.Changed > 0 && oldItem.Changed < item.Changed)
                    {
                        SqlDataEventTeamMatches.UpdateItemAsync(item);
                        updatedCount++;
                    }
                    else
                    {
                        notChangedCount++;
                    }
                }

                Label_Results.Text += $"\n\nAdded: {addedCount} - Updated: {updatedCount} - Not Changed: {notChangedCount}";
            }

            Label_Results.Text += "\n\nImport complete";

            _isBusy = false;
        }
Exemplo n.º 2
0
        private void Button_Download_Clicked(object sender, EventArgs e)
        {
            if (_isBusy)
            {
                return;
            }
            _isBusy = true;

            PrepareSync();

            int addedCount      = 0;
            int updatedCount    = 0;
            int notChangedCount = 0;
            int lastId          = 0;

            Label_Results.Text = "Downloading data...";

            List <EventTeamMatch> matches = (List <EventTeamMatch>)SqlDataEventTeamMatches.GetItemsAsync().Result;

            try
            {
                do
                {
                    addedCount      = 0;
                    updatedCount    = 0;
                    notChangedCount = 0;
                    string batchInfo             = $"{App.currFRCEventKey}|{lastId}|10";
                    HttpResponseMessage response = App.client.GetAsync($"api/EventTeamMatches?batchInfo={batchInfo}").Result;
                    response.EnsureSuccessStatusCode();
                    if (response.IsSuccessStatusCode)
                    {
                        string result  = response.Content.ReadAsStringAsync().Result;
                        JArray results = JArray.Parse(result);
                        foreach (JObject obj in results)
                        {
                            EventTeamMatch item = EventTeamMatch.Parse(obj.ToString());

                            if (lastId < item.Id.Value)
                            {
                                lastId = item.Id.Value;
                            }

                            EventTeamMatch oldItem = matches.FirstOrDefault(p => p.Uuid == item.Uuid);

                            if (oldItem == null)
                            {
                                item.Changed = 0; // downloaded records are excluded from sending
                                SqlDataEventTeamMatches.AddItemAsync(item);
                                addedCount++;
                            }
                            else if (oldItem.Changed > 0 && oldItem.Changed < item.Changed)
                            {
                                SqlDataEventTeamMatches.UpdateItemAsync(item);
                                updatedCount++;
                            }
                            else
                            {
                                notChangedCount++;
                            }
                        }
                    }

                    if (addedCount + updatedCount + notChangedCount > 0)
                    {
                        Label_Results.Text += $"\n\nAdded: {addedCount} - Updated: {updatedCount} - Not Changed: {notChangedCount}";
                    }
                }while (addedCount + updatedCount + notChangedCount > 0);

                Label_Results.Text += "\n\nDownload complete";
            }
            catch (Exception ex)
            {
                Label_Results.Text += $"\n\nError during transmission\n\n{ex.Message}\n\n{ex.InnerException}";
                _isBusy             = false;
                return;
            }

            _isBusy = false;
        }