Update EventOccurrence parsing to use EventOccurrenceParseGroup for improved data structure

This commit refactors the EventOccurrence parsing logic to utilize the EventOccurrenceParseGroup class, enhancing the organization of parsed occurrences by grouping them based on event definitions and optional section levels. The changes include updates to the EventOccurrenceParseResult, EventOccurrenceParser, and EventOccurrenceParserService to accommodate the new grouping structure. Additionally, tests are modified to reflect these changes, ensuring that the parsing functionality remains intact and accurate. This refactor improves data handling and aligns with the overall architecture of the application.
This commit is contained in:
2026-04-04 21:55:44 -04:00
parent 4dcd9e5aab
commit f400813667
39 changed files with 1896 additions and 93 deletions
@@ -0,0 +1,91 @@
using System.Text;
using Core.Entities;
using Core.Models;
namespace GoogleSheetsScheduleImport;
/// <summary>
/// Emits PDF-style section headers <c>Event Name - MS</c> / <c>Event Name - HS</c> before occurrence lines,
/// matching competition schedule imports.
/// </summary>
public static class GroupedImportTextEmitter
{
private readonly struct SectionKey(int eventDefinitionId, SchoolLevel level) : IEquatable<SectionKey>
{
public int EventDefinitionId { get; } = eventDefinitionId;
public SchoolLevel Level { get; } = level;
public bool Equals(SectionKey other) =>
EventDefinitionId == other.EventDefinitionId && Level == other.Level;
public override bool Equals(object? obj) => obj is SectionKey other && Equals(other);
public override int GetHashCode() => HashCode.Combine(EventDefinitionId, Level);
}
public static string Build(
IReadOnlyList<(string SheetTitle, string SectionHeader, List<ParsedOccurrenceLine> Lines)> sheets,
IReadOnlyList<EventDefinition> matchableEvents,
int year)
{
var idToDef = matchableEvents.Where(e => e.Id != 0).ToDictionary(e => e.Id);
var sb = new StringBuilder();
foreach (var (sheetTitle, _, lines) in sheets)
{
sb.AppendLine($"# {sheetTitle}");
var general = new List<ParsedOccurrenceLine>();
var bySection = new Dictionary<SectionKey, List<ParsedOccurrenceLine>>();
foreach (var line in lines)
{
if (!OccurrenceEventMatcher.TryMatch(line.Name, matchableEvents, out var evt, out var lvl)
|| evt == null
|| !lvl.HasValue)
{
general.Add(line);
continue;
}
var key = new SectionKey(evt.Id, lvl.Value);
if (!bySection.TryGetValue(key, out var list))
{
list = [];
bySection[key] = list;
}
list.Add(line);
}
foreach (var key in bySection.Keys.OrderBy(k => HeaderSortKey(idToDef, k), StringComparer.OrdinalIgnoreCase))
{
if (!idToDef.TryGetValue(key.EventDefinitionId, out var def))
continue;
sb.AppendLine($"{def.Name} - {SchoolLevelPrefixParser.ToSectionSuffix(key.Level)}");
foreach (var line in OccurrenceChronologicalSort.Sort(bySection[key], year))
{
var displayName = OccurrenceDisplayNameReducer.ReduceForSection(line.Name, def, key.Level);
sb.AppendLine(ImportLineFormatter.FormatOccurrenceLine(line with { Name = displayName }));
}
}
if (general.Count > 0)
{
sb.AppendLine("General Schedule");
foreach (var line in OccurrenceChronologicalSort.Sort(general, year))
sb.AppendLine(ImportLineFormatter.FormatOccurrenceLine(line));
}
sb.AppendLine();
}
return sb.ToString().TrimEnd();
}
private static string HeaderSortKey(Dictionary<int, EventDefinition> idToDef, SectionKey key)
{
if (!idToDef.TryGetValue(key.EventDefinitionId, out var def))
return $"{key.EventDefinitionId} - {key.Level}";
return $"{def.Name} - {SchoolLevelPrefixParser.ToSectionSuffix(key.Level)}";
}
}