Add EventOccurrenceParserService and update service registrations
Registered the new EventOccurrenceParserService in Program.cs to handle event occurrence parsing. Updated the _Imports.razor file to reflect the renaming of the EventCalendar component to Calendar. Removed the obsolete EventCalendar component to streamline the codebase.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Result of parsing event occurrence text data.
|
||||
/// Contains parsed occurrences, errors, and warnings.
|
||||
/// </summary>
|
||||
public class EventOccurrenceParseResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Dictionary of parsed event occurrences, keyed by EventDefinition.
|
||||
/// For special events (GeneralSchedule/VotingDelegates), the EventDefinition key will be the static instance.
|
||||
/// </summary>
|
||||
public IDictionary<EventDefinition, List<EventOccurrence>> Occurrences { get; set; } = new Dictionary<EventDefinition, List<EventOccurrence>>();
|
||||
|
||||
/// <summary>
|
||||
/// List of parsing errors (critical issues that prevented parsing).
|
||||
/// </summary>
|
||||
public List<string> Errors { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// List of parsing warnings (non-critical issues that occurred during parsing).
|
||||
/// </summary>
|
||||
public List<string> Warnings { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Total number of event occurrences successfully parsed.
|
||||
/// </summary>
|
||||
public int TotalParsed => Occurrences.Values.Sum(list => list.Count);
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether parsing was successful (no errors).
|
||||
/// </summary>
|
||||
public bool IsSuccess => Errors.Count == 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Text;
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
using Core.Parsers;
|
||||
|
||||
namespace Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service implementation for parsing event occurrence text data.
|
||||
/// Wraps EventOccurrenceParser to support text input and error collection.
|
||||
/// </summary>
|
||||
public class EventOccurrenceParserService : IEventOccurrenceParserService
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public EventOccurrenceParseResult ParseFromText(string text, ICollection<EventDefinition> events)
|
||||
{
|
||||
var result = new EventOccurrenceParseResult();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
result.Errors.Add("Input text is empty or whitespace.");
|
||||
return result;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Create a temporary file from the text content
|
||||
var tempFile = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
File.WriteAllText(tempFile, text, Encoding.UTF8);
|
||||
var fileInfo = new FileInfo(tempFile);
|
||||
|
||||
// Use the existing EventOccurrenceParser
|
||||
var parser = new EventOccurrenceParser(fileInfo, events);
|
||||
var parsedOccurrences = parser.Parse();
|
||||
|
||||
// Convert parsed occurrences to result format, handling special event types
|
||||
foreach (var kvp in parsedOccurrences)
|
||||
{
|
||||
var eventDefinition = kvp.Key;
|
||||
var occurrences = kvp.Value;
|
||||
|
||||
// Check if this is a special event type (GeneralSchedule or VotingDelegates)
|
||||
if (eventDefinition == EventDefinition.GeneralSchedule ||
|
||||
eventDefinition == EventDefinition.VotingDelegates)
|
||||
{
|
||||
// For special events, set EventDefinitionId to null and set SpecialEventType
|
||||
foreach (var occurrence in occurrences)
|
||||
{
|
||||
occurrence.EventDefinitionId = null;
|
||||
occurrence.SpecialEventType = eventDefinition == EventDefinition.GeneralSchedule
|
||||
? "GeneralSchedule"
|
||||
: "VotingDelegates";
|
||||
}
|
||||
|
||||
// Add to result with the special EventDefinition as key
|
||||
result.Occurrences[eventDefinition] = occurrences;
|
||||
}
|
||||
else
|
||||
{
|
||||
// For regular events, set EventDefinitionId and ensure SpecialEventType is null
|
||||
foreach (var occurrence in occurrences)
|
||||
{
|
||||
occurrence.EventDefinitionId = eventDefinition.Id;
|
||||
occurrence.SpecialEventType = null;
|
||||
}
|
||||
|
||||
result.Occurrences[eventDefinition] = occurrences;
|
||||
}
|
||||
}
|
||||
|
||||
// Track any occurrences without a matching EventDefinition
|
||||
// (This would be detected if parser.Parse() returns occurrences with null EventDefinition keys,
|
||||
// but the current parser implementation doesn't do this - all occurrences have a currentEventDefinition)
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Clean up temporary file
|
||||
try
|
||||
{
|
||||
if (File.Exists(tempFile))
|
||||
{
|
||||
File.Delete(tempFile);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Errors.Add($"Error parsing text: {ex.Message}");
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
result.Errors.Add($"Inner exception: {ex.InnerException.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
|
||||
namespace Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service interface for parsing event occurrence text data.
|
||||
/// </summary>
|
||||
public interface IEventOccurrenceParserService
|
||||
{
|
||||
/// <summary>
|
||||
/// Parses event occurrence text data and returns parse results with occurrences, errors, and warnings.
|
||||
/// </summary>
|
||||
/// <param name="text">The text content to parse (typically multi-line text from paste/import)</param>
|
||||
/// <param name="events">Collection of EventDefinitions to match against during parsing</param>
|
||||
/// <returns>ParseResult containing parsed occurrences, errors, and warnings</returns>
|
||||
EventOccurrenceParseResult ParseFromText(string text, ICollection<EventDefinition> events);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user