diff --git a/WebApp/Components/Features/Calendar/Import.razor b/WebApp/Components/Features/Calendar/Import.razor index c7f8b32..d69f037 100644 --- a/WebApp/Components/Features/Calendar/Import.razor +++ b/WebApp/Components/Features/Calendar/Import.razor @@ -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 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)