blazor-calendar/BlazorApp/Data/Event.cs

59 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace BlazorApp.Data
{
public class Event
{
public string Uid { get; set; }
public string Summary { get; set; }
public DateTime? DtStart { get; set; }
public DateTime? DtEnd { get; set; }
public string Description { get; set; }
public TimeSpan? Duration { get; set; }
public List<(string, string, string)> RawProperties { get; } = new();
public string ETag { get; set; }
public string Href { get; set; }
public DateTime? CalculatedEnd => DtEnd ?? DtStart + Duration;
private static string _escape(string s)
{
return s
.Replace(@"\", @"\\")
.Replace(",", @"\,")
.Replace(";", @"\;")
.Replace("\n", @"\n");
}
public string ToICal()
{
var sb = new StringBuilder();
sb.Append("BEGIN:VCALENDAR\r\n");
sb.Append("VERSION:2.0\r\n");
sb.Append("PRODID:-//Sijmen//blazor-calendar//EN\r\n");
sb.Append("BEGIN:VEVENT\r\n");
foreach (var (name, parameters, oldValue) in RawProperties)
{
sb.Append(name);
if (parameters != null)
sb.Append($";{parameters}");
var value = name switch
{
"SUMMARY" => _escape(Summary),
_ => oldValue
};
sb.Append($":{value}\r\n");
}
sb.Append("END:VEVENT\r\n");
sb.Append("END:VCALENDAR\r\n");
return sb.ToString();
}
}
}