Implement duplicate occurrence check in calendar import process
This commit enhances the calendar import functionality by adding a check for duplicate event occurrences before saving them to the database. If duplicates are detected, they are skipped, and the user is informed of the number of occurrences saved and duplicates skipped. This improvement ensures data integrity and provides better feedback to users during the import process.
This commit is contained in:
@@ -330,10 +330,20 @@
|
||||
try
|
||||
{
|
||||
var savedCount = 0;
|
||||
var skippedCount = 0;
|
||||
|
||||
foreach (var kvp in _parseResult.Occurrences)
|
||||
{
|
||||
foreach (var occurrence in kvp.Value)
|
||||
{
|
||||
// Check for duplicates before adding
|
||||
var isDuplicate = await IsDuplicate(occurrence);
|
||||
if (isDuplicate)
|
||||
{
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add each occurrence to the database
|
||||
await Context.EventOccurrences.AddAsync(occurrence);
|
||||
savedCount++;
|
||||
@@ -341,7 +351,13 @@
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync();
|
||||
Snackbar.Add($"Successfully saved {savedCount} occurrence(s) to database", Severity.Success);
|
||||
|
||||
var message = $"Successfully saved {savedCount} occurrence(s) to database";
|
||||
if (skippedCount > 0)
|
||||
{
|
||||
message += $" ({skippedCount} duplicate(s) skipped)";
|
||||
}
|
||||
Snackbar.Add(message, Severity.Success);
|
||||
|
||||
// Clear input and output instead of navigating
|
||||
_inputText = string.Empty;
|
||||
@@ -357,6 +373,33 @@
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> IsDuplicate(EventOccurrence occurrence)
|
||||
{
|
||||
// Check if an occurrence with the same name, date, time, location, and event definition already exists
|
||||
var query = Context.EventOccurrences
|
||||
.Where(eo => eo.Name == occurrence.Name &&
|
||||
eo.StartTime.Date == occurrence.StartTime.Date &&
|
||||
eo.Time == occurrence.Time &&
|
||||
eo.Location == occurrence.Location);
|
||||
|
||||
// Match by EventDefinitionId if it exists, otherwise match by SpecialEventType
|
||||
if (occurrence.EventDefinitionId.HasValue)
|
||||
{
|
||||
query = query.Where(eo => eo.EventDefinitionId == occurrence.EventDefinitionId);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(occurrence.SpecialEventType))
|
||||
{
|
||||
query = query.Where(eo => eo.SpecialEventType == occurrence.SpecialEventType);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If neither EventDefinitionId nor SpecialEventType is set, match by both being null/empty
|
||||
query = query.Where(eo => eo.EventDefinitionId == null && string.IsNullOrEmpty(eo.SpecialEventType));
|
||||
}
|
||||
|
||||
return await query.AnyAsync();
|
||||
}
|
||||
|
||||
private string GetEventName(EventDefinition eventDefinition)
|
||||
{
|
||||
if (eventDefinition == EventDefinition.GeneralSchedule)
|
||||
|
||||
Reference in New Issue
Block a user