Synchronizing two lists
I am syncing between two lists.
IList<Event> GoogleEvents
andTable<myEvent> DB.Events;
On google side I am using this
String Summary,
String Description,
EventDateTime Start,
EventDateTime End,
Event.RemindersData Reminders;
On db side like this
my code is like this
foreach (myEvent item in DB.Events)
{
if (item.GoogleID == "" || item.GoogleID == null)// i add event my db
{
//Add dbEvent to google and save id to googleid column
}
}
foreach (Event item in myGoogleCalendar.Items)
{
if (DB.Events.Where(o => o.GoogleID == item.Id).Count() == 0)// i add event google
{
//Add googleEvent to dbEvent
}
else
{
//This event also existing in two server
myEvent dbEvent = DB.Olaylar.Where(o => o.GoogleID == item.Id).First();
if(item.Updated.Value == dbEvent.UpdateTime) continue;//Same
if (item.Updated.Value > dbEvent.UpdateTime)
{
//Google event is new
}
else
{
//dbEvent is new
}
}
}
There is one big problem: I forgot to delete
Where should I add deleting part of events and how?
source to share
The problem is not to implement delete synchronization, but to recognize if an event was deleted in one list or added to another.
I recommend not deleting events from the DB.Events list, but marking them as deleted.
So you need a flag
bool IsDeleted = false;
in your type myEvent
.
On the google list (assuming you are using https://developers.google.com/google-apps/calendar/v3/reference/events ), you don't have such a flag. Therefore, your algorithm should be a little tricky.
So, you need the first sync from the google list
foreach (Event item in myGoogleCalendar.Items)
{
if (DB.Events.Where(o => o.GoogleID == item.Id).Count() == 0)
{
// create new DB event from Google event, because since it is
// not in the DB.Events list, it has not been deleted there earlier,
// else there would be an event with IsDeleted=true
}
else
{
//This event exists in both
myEvent dbEvent = DB.Events.Where(o => o.GoogleID == item.Id).First();
if (dbEvent.IsDeleted)
{
// delete Google event, since the flag in DB.Events list shows that
// it existed earlier and has been deleted in DB.Events list.
}
else
{
if(item.Updated.Value == dbEvent.UpdateTime) continue;//Same
if (item.Updated.Value > dbEvent.UpdateTime)
{
// update DB event
}
else
{
// update Google event
}
}
}
}
Then your sync from the DB.Events list
foreach (myEvent item in DB.Events)
{
if (!item.IsDeleted && string.IsNullOrEmpty(item.GoogleID))
{
// Create new Google Event from DB event
}
else if (!item.IsDeleted && !string.IsNullOrEmpty(item.GoogleID))
{
// Mark DB event as deleted
item.IsDeleted = true;
}
}
This works in scripts
- You create an event in the DB
- You are creating an event on Google
- You are deleting an event in the DB
- You delete an event on Google
source to share
I created a Gist: https://gist.github.com/ricjac/947991ba96488c640bfe
// Prepare DTO to delta
var mergeDict = DeltaExtensions.MergeDictionary(
sourceIEnum, //Source
destinationIEnum, //Destination
s => s.SomeIdKey,
d => d.SomeIdKey);
// Get the delta between the two
var mergeDelta = DeltaExtensions.GetMergeDelta(mergeDict);
// You now have the delta of the two:
mergeDelta.InsertedOnly
mergeDelta.DeletedOnly
mergeDelta.Inserted
mergeDelta.Updated
mergeDelta.Deleted
source to share