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.
83 lines
2.3 KiB
C#
83 lines
2.3 KiB
C#
using System.Text;
|
|
using Core.Entities;
|
|
|
|
namespace GoogleSheetsScheduleImport;
|
|
|
|
/// <summary>
|
|
/// Loads minimal <see cref="EventDefinition"/> rows from Tests-style CSV (column "Event").
|
|
/// </summary>
|
|
public static class EventDefinitionsCsvLoader
|
|
{
|
|
public static List<EventDefinition> Load(string path)
|
|
{
|
|
var list = new List<EventDefinition>();
|
|
var lines = File.ReadAllLines(path);
|
|
if (lines.Length < 2)
|
|
return list;
|
|
|
|
var header = ParseCsvLine(lines[0]);
|
|
var eventIdx = header.FindIndex(h => h.Equals("Event", StringComparison.OrdinalIgnoreCase));
|
|
if (eventIdx < 0)
|
|
throw new InvalidOperationException($"CSV '{path}' must contain an 'Event' column header.");
|
|
|
|
for (var i = 1; i < lines.Length; i++)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(lines[i]))
|
|
continue;
|
|
var cols = ParseCsvLine(lines[i]);
|
|
if (eventIdx >= cols.Count)
|
|
continue;
|
|
var name = cols[eventIdx].Trim();
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
continue;
|
|
list.Add(new EventDefinition
|
|
{
|
|
Id = i,
|
|
Name = name,
|
|
ShortName = name,
|
|
Eligibility = "",
|
|
EventFormat = EventFormat.Team
|
|
});
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
private static List<string> ParseCsvLine(string line)
|
|
{
|
|
var result = new List<string>();
|
|
var cur = new StringBuilder();
|
|
var inQuotes = false;
|
|
for (var i = 0; i < line.Length; i++)
|
|
{
|
|
var ch = line[i];
|
|
if (inQuotes)
|
|
{
|
|
if (ch == '"')
|
|
{
|
|
if (i + 1 < line.Length && line[i + 1] == '"')
|
|
{
|
|
cur.Append('"');
|
|
i++;
|
|
}
|
|
else inQuotes = false;
|
|
}
|
|
else cur.Append(ch);
|
|
}
|
|
else
|
|
{
|
|
if (ch == '"') inQuotes = true;
|
|
else if (ch == ',')
|
|
{
|
|
result.Add(cur.ToString());
|
|
cur.Clear();
|
|
}
|
|
else cur.Append(ch);
|
|
}
|
|
}
|
|
|
|
result.Add(cur.ToString());
|
|
return result;
|
|
}
|
|
}
|