Example #1
0
        private void SynchronizeCreations()
        {
            //set date to the past in case of empty table
            LastUpdate = new DateTime(1983, 11, 20);
            //get latest Date when the list was synchronized
            if (dataHandler.QueryCreations().Count > 0)
            {
                LastUpdate = dataHandler.QueryCreations().OrderByDescending(i => i.Modified).Select(j => j.Modified.GetValueOrDefault()).First();
            }
            List <Chocolate> ServerChocolate = serviceHandler.CallService <List <Chocolate> >(@"QueryChocolatesWithIngredients");

            if (ServerChocolate != null)
            {
                //save all which are new or have been updated to new List
                List <Chocolate> newChocolate = ServerChocolate.Where(i => i.Modified > LastUpdate).Select(j => j).ToList();

                foreach (var item in newChocolate)
                {
                    //in case of new
                    if (dataHandler.QueryCreations().Where(p => p.ChocolateId.Equals(item.ChocolateId)).Count() == 0)
                    {
                        if (dataHandler.InsertChocolate(item))
                        {
                            CreationInformer.Invoke();
                        }
                    }
                    //in case an existing has been updated
                    else
                    {
                        if (dataHandler.UpdateChocolate(item))
                        {
                            CreationInformer.Invoke();
                        }
                    }
                }
            }
        }