-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarSynchronizer.cs
105 lines (88 loc) · 3.05 KB
/
CalendarSynchronizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Collections.Async;
using System.Linq;
using System.Threading.Tasks;
using SyncWhole.Common;
using SyncWhole.Logging;
namespace SyncWhole
{
public sealed class CalendarSynchronizer
{
private readonly IAppointmentSourceFactory _sourceFactory;
private readonly IAppointmentDestinationFactory _destinationFactory;
public CalendarSynchronizer(IAppointmentSourceFactory sourceFactory,
IAppointmentDestinationFactory destinationFactory)
{
_sourceFactory = sourceFactory;
_destinationFactory = destinationFactory;
}
public string SourceName => _sourceFactory.ToString();
public string DestinationName => _destinationFactory.ToString();
public async Task<Statistics> SynchronizeAsync(bool force)
{
using (Logger.Scope($"CalendarSynchronizer.Synchronize({(force ? "force" : null)})"))
{
using (var source = await _sourceFactory.ConnectSourceAsync().ConfigureAwait(false))
{
using (var destination = await _destinationFactory.ConnectDestinationAsync().ConfigureAwait(false))
{
return await SynchronizeInternalAsync(source, destination, force).ConfigureAwait(false);
}
}
}
}
private async Task<Statistics> SynchronizeInternalAsync(IAppointmentSource source, IAppointmentDestination destination, bool force)
{
var sourceAppointments = await source
.LoadAllAppointments()
.ToArrayAsync()
.ConfigureAwait(false);
var destAppointments = await destination
.LoadAllAppointments()
.ToArrayAsync()
.ConfigureAwait(false);
var newAppointments = sourceAppointments.Except(destAppointments, LoadedAppointmentIdComparer.Default);
var deletedAppointments = destAppointments.Except(sourceAppointments, LoadedAppointmentIdComparer.Default);
var modifiedAppointments = sourceAppointments.SmartZip(destAppointments, LoadedAppointmentIdComparer.Default,
(src, dest) => new { Source = src, Destination = dest});
if (!force)
{
modifiedAppointments = modifiedAppointments.Where(pair => pair.Source.LastModifiedDateTime > pair.Destination.LastModifiedDateTime);
}
int created = 0, deleted = 0, updated = 0;
foreach (var appointment in newAppointments)
{
await destination
.CreateAppointmentAsync(appointment.UniqueId, appointment)
.ConfigureAwait(false);
created++;
}
foreach (var appointment in deletedAppointments)
{
await destination
.DeleteAppointmentAsync(appointment)
.ConfigureAwait(false);
deleted++;
}
foreach (var pair in modifiedAppointments)
{
await destination
.UpdateAppointmentAsync(pair.Destination, pair.Source, force)
.ConfigureAwait(false);
updated++;
}
return new Statistics(created, deleted, updated);
}
public struct Statistics
{
public int Created { get; }
public int Deleted { get; }
public int Updated { get; }
public Statistics(int created, int deleted, int updated)
{
Created = created;
Deleted = deleted;
Updated = updated;
}
}
}
}