Пример #1
0
        public async static Task <bool> GetContactsFromWeb(bool isUpdated)
        {
            try
            {
                //Если необходимо ошибку вызвать
                //throw new HttpListenerException();
                string json1;
                string json2;
                string json3;
                using (WebClient client = new WebClient())
                {
                    var baseAddress = "https://raw.githubusercontent.com/Newbilius/ElbaMobileXamarinDeveloperTest/master/json/";
                    json1 = client.DownloadString($"{baseAddress}generated-01.json");
                    json2 = client.DownloadString($"{baseAddress}generated-02.json");
                    json3 = client.DownloadString($"{baseAddress}generated-03.json");
                }

                // using newtonsoft json.net - use http://json2csharp.com/ to verfiy
                // that your C# model class actually matches your json
                var data = JsonConvert.DeserializeObject <List <Contact> >(json1);
                data.AddRange(JsonConvert.DeserializeObject <List <Contact> >(json2));
                data.AddRange(JsonConvert.DeserializeObject <List <Contact> >(json3));
                _db.ClearDbAsync();
                foreach (Contact contact in data)
                {
                    var eduId = Guid.NewGuid().ToString();
                    contact.EducationPeriodId  = eduId;
                    contact.EducationPeriod    = contact.EducationPeriod;
                    contact.EducationPeriod.Id = eduId;
                }
                await _db.SaveAllItemsAsync(data.Select(x => x.EducationPeriod), isUpdated);

                await _db.SaveAllItemsAsync(data, isUpdated);

                ContactList = data.OrderBy(x => x.Name).ToList();
                return(true);
            }
            catch
            {
                _db.ClearDbAsync();
                foreach (Contact contact in ContactList)
                {
                    var eduId = Guid.NewGuid().ToString();
                    contact.EducationPeriodId  = eduId;
                    contact.EducationPeriod    = contact.EducationPeriod;
                    contact.EducationPeriod.Id = eduId;
                }
                await _db.SaveAllItemsAsync(ContactList.Select(x => x.EducationPeriod), isUpdated);

                await _db.SaveAllItemsAsync(ContactList, isUpdated);

                return(false);
            }
        }