Store leftover CSV columns on hidden student notes, move catalog import to /events/import, and persist Students index columns from Chapter Settings. Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using Core.Notes;
|
|
|
|
namespace Core.Parsers;
|
|
|
|
/// <summary>
|
|
/// Builds a starter CSV for <c>/students/import</c> with roster columns plus leftover note fields.
|
|
/// </summary>
|
|
public static class StudentImportCsvTemplate
|
|
{
|
|
public static readonly string[] RosterHeaders =
|
|
[
|
|
"Student Name",
|
|
"Grade",
|
|
"TSA year",
|
|
"State ID",
|
|
"Regional ID",
|
|
"National ID"
|
|
];
|
|
|
|
/// <summary>
|
|
/// Returns a CSV with a header row and one example data row.
|
|
/// </summary>
|
|
public static string Build(IEnumerable<string>? leftoverFieldNames = null)
|
|
{
|
|
List<string> leftovers = leftoverFieldNames?
|
|
.Where(name => !string.IsNullOrWhiteSpace(name))
|
|
.Select(name => name.Trim())
|
|
.Where(name => !StudentNotesFieldParser.IsReservedHeader(name))
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
.ToList() ?? [];
|
|
|
|
if (leftovers.Count == 0)
|
|
leftovers = [.. StudentNoteFieldDefaults.IndexColumns];
|
|
|
|
var headers = RosterHeaders.Concat(leftovers).ToArray();
|
|
var values = headers.Select(ExampleValue).ToArray();
|
|
return $"{ToCsvRow(headers)}{Environment.NewLine}{ToCsvRow(values)}{Environment.NewLine}";
|
|
}
|
|
|
|
private static string ExampleValue(string header) => header switch
|
|
{
|
|
"Student Name" => "Last, First",
|
|
"Grade" => "9",
|
|
"TSA year" => "1st",
|
|
"Interview Time" => "3:20-3:35",
|
|
_ when ContainsIgnoreCase(header, "Application")
|
|
|| ContainsIgnoreCase(header, "Permission") => "x",
|
|
_ => string.Empty
|
|
};
|
|
|
|
private static bool ContainsIgnoreCase(string value, string part) =>
|
|
value.Contains(part, StringComparison.OrdinalIgnoreCase);
|
|
|
|
private static string ToCsvRow(IEnumerable<string> cells) =>
|
|
string.Join(",", cells.Select(EscapeCsv));
|
|
|
|
private static string EscapeCsv(string value)
|
|
{
|
|
if (value.Contains(',') || value.Contains('"') || value.Contains('\n'))
|
|
return $"\"{value.Replace("\"", "\"\"")}\"";
|
|
return value;
|
|
}
|
|
}
|