Compare commits
6
Commits
4c91db37c2
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29101e2ead | ||
|
|
c03ffc0833 | ||
|
|
1337d9833d | ||
|
|
3712dba974 | ||
|
|
3f50d6e635 | ||
|
|
4cfd85b902 |
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Core.Printing;
|
||||
|
||||
namespace Core.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Saved page-printer recipe: template markdown, entity type, and filters. Merged output is not stored.
|
||||
/// </summary>
|
||||
public class PrintPreset
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
public string TemplateMarkdown { get; set; } = string.Empty;
|
||||
|
||||
public PrintEntityType EntityType { get; set; }
|
||||
|
||||
public string FiltersJson { get; set; } = "{}";
|
||||
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
}
|
||||
@@ -17,6 +17,10 @@ public class Student : IEquatable<Student>
|
||||
[Display(Name = "Last Name")]
|
||||
public string LastName { get; set; } = null!;
|
||||
|
||||
[StringLength(50)]
|
||||
[Display(Name = "Nickname")]
|
||||
public string? Nickname { get; set; }
|
||||
|
||||
[Range(5,12)]
|
||||
[Display(Name = "Grade")]
|
||||
public int Grade { get; set; }
|
||||
@@ -55,6 +59,17 @@ public class Student : IEquatable<Student>
|
||||
|
||||
public string FirstNameLastName => $"{FirstName} {LastName}";
|
||||
|
||||
/// <summary>
|
||||
/// Nickname when set, otherwise <see cref="FirstName"/>.
|
||||
/// </summary>
|
||||
public string DisplayFirstName =>
|
||||
string.IsNullOrWhiteSpace(Nickname) ? FirstName : Nickname.Trim();
|
||||
|
||||
public void NormalizeNickname()
|
||||
{
|
||||
Nickname = string.IsNullOrWhiteSpace(Nickname) ? null : Nickname.Trim();
|
||||
}
|
||||
|
||||
public static Tuple<string, string> ParseNameParts(string fullName)
|
||||
{
|
||||
var match = Match(fullName, @"(.*),\s*(.*)");
|
||||
@@ -67,10 +82,7 @@ public class Student : IEquatable<Student>
|
||||
: new Tuple<string, string>(fullName, string.Empty);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return FirstName;
|
||||
}
|
||||
public override string ToString() => DisplayFirstName;
|
||||
|
||||
public bool VotingDelegate => OfficerRole is Entities.OfficerRole.President or Entities.OfficerRole.VicePresident;
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using Core.Entities;
|
||||
using Core.Notes;
|
||||
|
||||
namespace Core.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Result of parsing a student notes field CSV.
|
||||
/// </summary>
|
||||
public class StudentNotesImportResult
|
||||
{
|
||||
public List<StudentNotesImportMatch> Matches { get; set; } = [];
|
||||
|
||||
public List<StudentNotesImportIssue> Issues { get; set; } = [];
|
||||
|
||||
public List<string> Errors { get; set; } = [];
|
||||
|
||||
public List<string> Warnings { get; set; } = [];
|
||||
|
||||
public IReadOnlyList<string> FieldNames { get; set; } = [];
|
||||
|
||||
public bool IsSuccess => Errors.Count == 0;
|
||||
|
||||
public int StudentsWithChanges => Matches.Count(m => m.Merge.Changed);
|
||||
}
|
||||
|
||||
public class StudentNotesImportMatch
|
||||
{
|
||||
public required Student Student { get; set; }
|
||||
|
||||
public string RawStudentName { get; set; } = string.Empty;
|
||||
|
||||
public int RowNumber { get; set; }
|
||||
|
||||
public int StudentScore { get; set; }
|
||||
|
||||
public List<ImportedField> IncomingFields { get; set; } = [];
|
||||
|
||||
public required ImportedFieldsMergeResult Merge { get; set; }
|
||||
}
|
||||
|
||||
public class StudentNotesImportIssue
|
||||
{
|
||||
public int RowNumber { get; set; }
|
||||
|
||||
public string RawStudentName { get; set; } = string.Empty;
|
||||
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Core.Notes;
|
||||
|
||||
/// <summary>
|
||||
/// Parses and upserts a generic Field/Value markdown table under a stable heading.
|
||||
/// </summary>
|
||||
public static class ImportedFieldsTable
|
||||
{
|
||||
public const string Heading = "## Additional fields";
|
||||
|
||||
public static string NormalizeValue(string? raw)
|
||||
{
|
||||
var value = (raw ?? string.Empty).Trim();
|
||||
if (value.Equals("x", StringComparison.OrdinalIgnoreCase))
|
||||
return "Yes";
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads Field/Value rows from the Additional fields section.
|
||||
/// </summary>
|
||||
public static List<ImportedField> ParseFields(string? markdown)
|
||||
{
|
||||
var section = ExtractSection(markdown);
|
||||
if (string.IsNullOrWhiteSpace(section))
|
||||
return [];
|
||||
|
||||
List<ImportedField> fields = [];
|
||||
foreach (var rawLine in section.Split('\n'))
|
||||
{
|
||||
var line = rawLine.Trim();
|
||||
if (!line.StartsWith('|') || line.Contains("---", StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
var cells = SplitTableCells(line);
|
||||
if (cells.Length < 2)
|
||||
continue;
|
||||
|
||||
var field = UnescapeCell(cells[0]);
|
||||
var value = UnescapeCell(cells[1]);
|
||||
if (field.Equals("Field", StringComparison.OrdinalIgnoreCase)
|
||||
&& value.Equals("Value", StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
fields.Add(new ImportedField(field, value));
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upserts incoming fields into the Additional fields section. Incoming values win.
|
||||
/// Fields not in <paramref name="incoming"/> are kept. Identical values are not changes.
|
||||
/// </summary>
|
||||
public static ImportedFieldsMergeResult Merge(string? existingMarkdown, IReadOnlyList<ImportedField> incoming)
|
||||
{
|
||||
var existing = ParseFields(existingMarkdown);
|
||||
var merged = existing.ToList();
|
||||
List<ImportedFieldChange> changes = [];
|
||||
|
||||
foreach (var incomingField in incoming)
|
||||
{
|
||||
var name = incomingField.Name.Trim();
|
||||
if (string.IsNullOrEmpty(name))
|
||||
continue;
|
||||
|
||||
var value = NormalizeValue(incomingField.Value);
|
||||
var index = merged.FindIndex(f => f.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
|
||||
if (index < 0)
|
||||
{
|
||||
merged.Add(new ImportedField(name, value));
|
||||
changes.Add(new ImportedFieldChange
|
||||
{
|
||||
Field = name,
|
||||
PreviousValue = null,
|
||||
NewValue = value,
|
||||
IsNew = true
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
var current = merged[index];
|
||||
if (string.Equals(current.Value, value, StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
merged[index] = new ImportedField(current.Name, value);
|
||||
changes.Add(new ImportedFieldChange
|
||||
{
|
||||
Field = current.Name,
|
||||
PreviousValue = current.Value,
|
||||
NewValue = value,
|
||||
IsNew = false
|
||||
});
|
||||
}
|
||||
|
||||
var section = FormatSection(merged);
|
||||
var markdown = ReplaceSection(existingMarkdown, section);
|
||||
|
||||
return new ImportedFieldsMergeResult
|
||||
{
|
||||
Markdown = markdown,
|
||||
Changed = changes.Count > 0,
|
||||
Changes = changes
|
||||
};
|
||||
}
|
||||
|
||||
public static string FormatSection(IReadOnlyList<ImportedField> fields)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine(Heading);
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("| Field | Value |");
|
||||
builder.AppendLine("| --- | --- |");
|
||||
foreach (var field in fields)
|
||||
builder.AppendLine($"| {EscapeCell(field.Name)} | {EscapeCell(field.Value)} |");
|
||||
|
||||
return builder.ToString().TrimEnd() + Environment.NewLine;
|
||||
}
|
||||
|
||||
public static string? GetFieldValue(string? markdown, string fieldName)
|
||||
{
|
||||
var field = ParseFields(markdown)
|
||||
.FirstOrDefault(f => f.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase));
|
||||
return field?.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unique additional-field names across notes, first-seen casing, sorted A–Z.
|
||||
/// </summary>
|
||||
public static List<string> DistinctFieldNames(IEnumerable<string?> markdowns)
|
||||
{
|
||||
Dictionary<string, string> names = new(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var markdown in markdowns)
|
||||
{
|
||||
foreach (var field in ParseFields(markdown))
|
||||
{
|
||||
var name = field.Name.Trim();
|
||||
if (name.Length == 0)
|
||||
continue;
|
||||
names.TryAdd(name, name);
|
||||
}
|
||||
}
|
||||
|
||||
return [.. names.Values.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)];
|
||||
}
|
||||
|
||||
private static string? ExtractSection(string? markdown)
|
||||
{
|
||||
if (string.IsNullOrEmpty(markdown))
|
||||
return null;
|
||||
|
||||
if (!TryFindHeading(markdown, out var start, out var headingLength))
|
||||
return null;
|
||||
|
||||
var afterHeading = start + headingLength;
|
||||
var nextHeading = FindNextHeading(markdown, afterHeading);
|
||||
return nextHeading < 0 ? markdown[start..] : markdown[start..nextHeading];
|
||||
}
|
||||
|
||||
private static string ReplaceSection(string? existingMarkdown, string section)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(existingMarkdown))
|
||||
return section.TrimEnd() + Environment.NewLine;
|
||||
|
||||
if (!TryFindHeading(existingMarkdown, out var start, out var headingLength))
|
||||
{
|
||||
var prefix = existingMarkdown.TrimEnd();
|
||||
return string.IsNullOrEmpty(prefix)
|
||||
? section
|
||||
: prefix + Environment.NewLine + Environment.NewLine + section;
|
||||
}
|
||||
|
||||
var afterHeading = start + headingLength;
|
||||
var nextHeading = FindNextHeading(existingMarkdown, afterHeading);
|
||||
var before = existingMarkdown[..start].TrimEnd();
|
||||
var after = nextHeading < 0 ? string.Empty : existingMarkdown[nextHeading..].TrimStart();
|
||||
|
||||
var builder = new StringBuilder();
|
||||
if (!string.IsNullOrEmpty(before))
|
||||
{
|
||||
builder.Append(before);
|
||||
builder.AppendLine();
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
builder.Append(section.TrimEnd());
|
||||
builder.AppendLine();
|
||||
|
||||
if (!string.IsNullOrEmpty(after))
|
||||
{
|
||||
builder.AppendLine();
|
||||
builder.Append(after.TrimEnd());
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// First <c>## … fields</c> heading (any prefix). New sections are written as <see cref="Heading"/>.
|
||||
/// </summary>
|
||||
private static bool TryFindHeading(string markdown, out int start, out int headingLength)
|
||||
{
|
||||
var index = 0;
|
||||
while (index < markdown.Length)
|
||||
{
|
||||
var lineEnd = markdown.IndexOf('\n', index);
|
||||
var end = lineEnd < 0 ? markdown.Length : lineEnd;
|
||||
var line = markdown[index..end].TrimEnd('\r');
|
||||
if (IsFieldsHeading(line))
|
||||
{
|
||||
start = index;
|
||||
headingLength = line.Length;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (lineEnd < 0)
|
||||
break;
|
||||
index = lineEnd + 1;
|
||||
}
|
||||
|
||||
start = -1;
|
||||
headingLength = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsFieldsHeading(string line)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
if (!trimmed.StartsWith("## ", StringComparison.Ordinal)
|
||||
|| trimmed.StartsWith("###", StringComparison.Ordinal))
|
||||
return false;
|
||||
|
||||
var title = trimmed[3..].Trim();
|
||||
return title.EndsWith(" fields", StringComparison.OrdinalIgnoreCase)
|
||||
|| title.Equals("fields", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static int FindNextHeading(string markdown, int startIndex)
|
||||
{
|
||||
var index = startIndex;
|
||||
while (index < markdown.Length)
|
||||
{
|
||||
var lineStart = markdown.IndexOf('\n', index);
|
||||
if (lineStart < 0)
|
||||
return -1;
|
||||
|
||||
lineStart++;
|
||||
if (lineStart < markdown.Length && markdown[lineStart] == '#' &&
|
||||
lineStart + 2 < markdown.Length && markdown[lineStart + 1] == '#' &&
|
||||
markdown[lineStart + 2] == ' ')
|
||||
return lineStart;
|
||||
|
||||
index = lineStart;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits a markdown table row into cells. Leading/trailing pipes are ignored so a blank
|
||||
/// value (e.g. <c>| Teacher Rec 3 | |</c>) is kept instead of dropped.
|
||||
/// </summary>
|
||||
private static string[] SplitTableCells(string line)
|
||||
{
|
||||
var parts = line.Split('|', StringSplitOptions.TrimEntries);
|
||||
var start = 0;
|
||||
var length = parts.Length;
|
||||
if (length > 0 && parts[0].Length == 0)
|
||||
{
|
||||
start = 1;
|
||||
length--;
|
||||
}
|
||||
|
||||
if (length > 0 && parts[start + length - 1].Length == 0)
|
||||
length--;
|
||||
|
||||
return length <= 0 ? [] : parts[start..(start + length)];
|
||||
}
|
||||
|
||||
private static string EscapeCell(string value) => value.Replace("|", "\\|");
|
||||
|
||||
private static string UnescapeCell(string value) => value.Replace("\\|", "|");
|
||||
}
|
||||
|
||||
public record ImportedField(string Name, string Value);
|
||||
|
||||
public class ImportedFieldsMergeResult
|
||||
{
|
||||
public required string Markdown { get; init; }
|
||||
|
||||
public bool Changed { get; init; }
|
||||
|
||||
public List<ImportedFieldChange> Changes { get; init; } = [];
|
||||
}
|
||||
|
||||
public class ImportedFieldChange
|
||||
{
|
||||
public required string Field { get; init; }
|
||||
|
||||
public string? PreviousValue { get; init; }
|
||||
|
||||
public required string NewValue { get; init; }
|
||||
|
||||
public bool IsNew { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace Core.Notes;
|
||||
|
||||
/// <summary>
|
||||
/// Default imported-field names used for Students index columns and the CSV template.
|
||||
/// </summary>
|
||||
public static class StudentNoteFieldDefaults
|
||||
{
|
||||
public static readonly string[] IndexColumns =
|
||||
[
|
||||
"Interview Time",
|
||||
"Application",
|
||||
"Club Permission Slip"
|
||||
];
|
||||
}
|
||||
@@ -9,6 +9,10 @@ public class AssignmentRequirementParser : CsvParserBase
|
||||
{
|
||||
}
|
||||
|
||||
public AssignmentRequirementParser(StreamReader reader, bool ignoreBlankLines = true) : base(reader, ignoreBlankLines)
|
||||
{
|
||||
}
|
||||
|
||||
public AssignmentRequirement[] Parse(ICollection<EventDefinition> events, ICollection<Student> students)
|
||||
{
|
||||
var assumptions = new List<AssignmentRequirement>();
|
||||
@@ -20,7 +24,11 @@ public class AssignmentRequirementParser : CsvParserBase
|
||||
|
||||
var studentArray =
|
||||
studentColumns
|
||||
.Select(c => students.FirstOrDefault(s => s.FirstName == c)).ToArray();
|
||||
.Select(c => students.FirstOrDefault(s =>
|
||||
string.Equals(s.DisplayFirstName, c, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(s.Nickname, c, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(s.FirstName, c, StringComparison.OrdinalIgnoreCase)))
|
||||
.ToArray();
|
||||
|
||||
while (CsvReader.Read())
|
||||
{
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
using Core.Entities;
|
||||
using FuzzySharp;
|
||||
|
||||
namespace Core.Parsers;
|
||||
|
||||
/// <summary>
|
||||
/// Fuzzy-matches a CSV or pasted name to existing students.
|
||||
/// </summary>
|
||||
public static class FuzzyStudentMatcher
|
||||
{
|
||||
public const int MatchThreshold = 90;
|
||||
|
||||
public static (Student Student, int Score)? Find(ICollection<Student> students, string name)
|
||||
{
|
||||
var ranked = students
|
||||
.Select(s => (Student: s, Score: Score(s, name)))
|
||||
.Where(x => x.Score >= MatchThreshold)
|
||||
.OrderByDescending(x => x.Score)
|
||||
.ToList();
|
||||
|
||||
return ranked.Count == 0 ? null : ranked[0];
|
||||
}
|
||||
|
||||
public static int Score(Student student, string name)
|
||||
{
|
||||
var candidates = new[]
|
||||
{
|
||||
student.Name,
|
||||
student.FirstNameLastName,
|
||||
student.LastNameFirstName,
|
||||
student.DisplayFirstName,
|
||||
student.Nickname
|
||||
}.Where(c => !string.IsNullOrWhiteSpace(c));
|
||||
return candidates.Max(candidate => Math.Max(Fuzz.Ratio(candidate, name), Fuzz.TokenSetRatio(candidate, name)));
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ namespace Core.Parsers;
|
||||
|
||||
public class StudentEventRankingParser : CsvParserBase
|
||||
{
|
||||
public const int StudentMatchThreshold = 90;
|
||||
public const int EventMatchThreshold = 70;
|
||||
public const int EventAmbiguityGap = 8;
|
||||
|
||||
@@ -60,7 +59,7 @@ public class StudentEventRankingParser : CsvParserBase
|
||||
if (string.IsNullOrEmpty(name))
|
||||
continue;
|
||||
|
||||
var studentMatch = FindStudent(students, name);
|
||||
var studentMatch = FuzzyStudentMatcher.Find(students, name);
|
||||
if (studentMatch is null)
|
||||
{
|
||||
result.Issues.Add(new StudentEventRankingIssue
|
||||
@@ -172,23 +171,6 @@ public class StudentEventRankingParser : CsvParserBase
|
||||
return result;
|
||||
}
|
||||
|
||||
private static (Student Student, int Score)? FindStudent(ICollection<Student> students, string name)
|
||||
{
|
||||
var ranked = students
|
||||
.Select(s => (Student: s, Score: ScoreStudent(s, name)))
|
||||
.Where(x => x.Score >= StudentMatchThreshold)
|
||||
.OrderByDescending(x => x.Score)
|
||||
.ToList();
|
||||
|
||||
return ranked.Count == 0 ? null : ranked[0];
|
||||
}
|
||||
|
||||
private static int ScoreStudent(Student student, string name)
|
||||
{
|
||||
var candidates = new[] { student.Name, student.FirstNameLastName, student.LastNameFirstName };
|
||||
return candidates.Max(candidate => Math.Max(Fuzz.Ratio(candidate, name), Fuzz.TokenSetRatio(candidate, name)));
|
||||
}
|
||||
|
||||
private static EventResolution ResolveEvent(ICollection<EventDefinition> events, string eventName)
|
||||
{
|
||||
var scored = events
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
using Core.Notes;
|
||||
|
||||
namespace Core.Parsers;
|
||||
|
||||
/// <summary>
|
||||
/// Parses leftover CSV columns (not roster or ranking) into student note field merges.
|
||||
/// </summary>
|
||||
public class StudentNotesFieldParser : CsvParserBase
|
||||
{
|
||||
private static readonly HashSet<string> ReservedHeaders = new(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"Student Name",
|
||||
"Grade",
|
||||
"TSA year",
|
||||
"State ID",
|
||||
"Regional ID",
|
||||
"National ID",
|
||||
"Officer",
|
||||
"TOTAL # OF EVENTS"
|
||||
};
|
||||
|
||||
public StudentNotesFieldParser(FileSystemInfo csvFile, bool ignoreBlankLines = true) : base(csvFile, ignoreBlankLines)
|
||||
{
|
||||
}
|
||||
|
||||
public StudentNotesFieldParser(StreamReader reader, bool ignoreBlankLines = true) : base(reader, ignoreBlankLines)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Roster and ranking columns that must not become imported note fields.
|
||||
/// </summary>
|
||||
public static bool IsReservedHeader(string? header)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(header))
|
||||
return true;
|
||||
|
||||
var trimmed = header.Trim();
|
||||
if (ReservedHeaders.Contains(trimmed))
|
||||
return true;
|
||||
|
||||
return int.TryParse(trimmed, out var rank)
|
||||
&& rank >= 1
|
||||
&& rank <= StudentEventRanking.MaxRank;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Leftover field names from a header row after reserved columns are removed.
|
||||
/// </summary>
|
||||
public static List<string> GetLeftoverFieldNames(IEnumerable<string?> headers) =>
|
||||
headers
|
||||
.Where(h => !IsReservedHeader(h))
|
||||
.Select(h => h!.Trim())
|
||||
.ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Reads the header row and returns leftover field names without processing data rows.
|
||||
/// </summary>
|
||||
public List<string> PeekLeftoverFieldNames()
|
||||
{
|
||||
CsvReader.Read();
|
||||
CsvReader.ReadHeader();
|
||||
return GetLeftoverFieldNames(CsvReader.HeaderRecord ?? []);
|
||||
}
|
||||
|
||||
public StudentNotesImportResult Parse(
|
||||
ICollection<Student> students,
|
||||
IReadOnlyDictionary<int, string?> existingNotesByStudentId)
|
||||
{
|
||||
var result = new StudentNotesImportResult();
|
||||
|
||||
CsvReader.Read();
|
||||
CsvReader.ReadHeader();
|
||||
|
||||
if (CsvReader.HeaderRecord is null ||
|
||||
!CsvReader.HeaderRecord.Contains("Student Name", StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
result.Errors.Add("CSV must include a 'Student Name' column.");
|
||||
return result;
|
||||
}
|
||||
|
||||
var fieldNames = GetLeftoverFieldNames(CsvReader.HeaderRecord);
|
||||
result.FieldNames = fieldNames;
|
||||
|
||||
if (fieldNames.Count == 0)
|
||||
result.Warnings.Add("No leftover field columns were found besides roster and ranking columns.");
|
||||
|
||||
Dictionary<int, PendingStudentFields> pendingByStudentId = [];
|
||||
|
||||
while (CsvReader.Read())
|
||||
{
|
||||
var rowNumber = CsvReader.Context.Parser?.Row ?? 0;
|
||||
var name = CsvReader.GetField("Student Name")?.Trim();
|
||||
if (string.IsNullOrEmpty(name))
|
||||
continue;
|
||||
|
||||
var studentMatch = FuzzyStudentMatcher.Find(students, name);
|
||||
if (studentMatch is null)
|
||||
{
|
||||
result.Issues.Add(new StudentNotesImportIssue
|
||||
{
|
||||
RowNumber = rowNumber,
|
||||
RawStudentName = name,
|
||||
Message = $"No student matched '{name}'."
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
var (student, score) = studentMatch.Value;
|
||||
if (!pendingByStudentId.TryGetValue(student.Id, out var pending))
|
||||
{
|
||||
pending = new PendingStudentFields(student);
|
||||
pendingByStudentId[student.Id] = pending;
|
||||
}
|
||||
|
||||
pending.RawStudentName = name;
|
||||
pending.RowNumber = rowNumber;
|
||||
pending.StudentScore = score;
|
||||
|
||||
foreach (var fieldName in fieldNames)
|
||||
{
|
||||
var raw = CsvReader.GetField(fieldName);
|
||||
pending.Fields[fieldName.Trim()] = raw ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var pending in pendingByStudentId.Values)
|
||||
{
|
||||
List<ImportedField> incoming = [.. pending.Fields.Select(pair => new ImportedField(pair.Key, pair.Value))];
|
||||
existingNotesByStudentId.TryGetValue(pending.Student.Id, out var existingMarkdown);
|
||||
var merge = ImportedFieldsTable.Merge(existingMarkdown, incoming);
|
||||
|
||||
result.Matches.Add(new StudentNotesImportMatch
|
||||
{
|
||||
Student = pending.Student,
|
||||
RawStudentName = pending.RawStudentName,
|
||||
RowNumber = pending.RowNumber,
|
||||
StudentScore = pending.StudentScore,
|
||||
IncomingFields = incoming,
|
||||
Merge = merge
|
||||
});
|
||||
}
|
||||
|
||||
if (result.Matches.Count == 0 && result.Errors.Count == 0)
|
||||
result.Warnings.Add("No students were matched from the CSV.");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private sealed class PendingStudentFields(Student student)
|
||||
{
|
||||
public Student Student { get; } = student;
|
||||
|
||||
public string RawStudentName { get; set; } = string.Empty;
|
||||
|
||||
public int RowNumber { get; set; }
|
||||
|
||||
public int StudentScore { get; set; }
|
||||
|
||||
public Dictionary<string, string> Fields { get; } = new(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,10 @@ namespace Core.Parsers
|
||||
{
|
||||
}
|
||||
|
||||
public TeamParser(StreamReader reader, bool ignoreBlankLines = true) : base(reader, ignoreBlankLines)
|
||||
{
|
||||
}
|
||||
|
||||
public Team[] Parse(ICollection<EventDefinition> events, ICollection<Student> students)
|
||||
{
|
||||
var teams = new List<Team>();
|
||||
@@ -118,7 +122,9 @@ namespace Core.Parsers
|
||||
{
|
||||
Fuzz.Ratio(s.Name, studentName),
|
||||
Fuzz.Ratio(s.FirstNameLastName, studentName),
|
||||
Fuzz.Ratio(s.FirstName, studentName)
|
||||
Fuzz.Ratio(s.FirstName, studentName),
|
||||
Fuzz.Ratio(s.DisplayFirstName, studentName),
|
||||
string.IsNullOrWhiteSpace(s.Nickname) ? 0 : Fuzz.Ratio(s.Nickname, studentName)
|
||||
}.Max()
|
||||
where rat > 90
|
||||
orderby rat descending
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
public sealed record EventMark(
|
||||
string Symbol,
|
||||
string Label,
|
||||
string Color,
|
||||
Func<EventDefinition, bool> Applies);
|
||||
|
||||
/// <summary>
|
||||
/// Compact event-attribute marks used on the event-ranking index chip,
|
||||
/// print badges, and the shared legend.
|
||||
/// </summary>
|
||||
public static class EventAttributeMarks
|
||||
{
|
||||
public const string LevelOfEffort1 = "○";
|
||||
public const string LevelOfEffort2 = "◐";
|
||||
public const string LevelOfEffort3 = "⬤";
|
||||
public const string Individual = "ⓘ";
|
||||
public const string OnSite = "ⓐ";
|
||||
public const string Regional = "ⓡ";
|
||||
public const string Presubmission = "ⓟ";
|
||||
|
||||
public static readonly IReadOnlyList<EventMark> LegendItems =
|
||||
[
|
||||
new(LevelOfEffort1, "Level of Effort: 1", "#757575", e => e.LevelOfEffort == 1),
|
||||
new(LevelOfEffort2, "Level of Effort: 2", "#616161", e => e.LevelOfEffort == 2),
|
||||
new(LevelOfEffort3, "Level of Effort: 3", "#424242", e => e.LevelOfEffort == 3),
|
||||
new(Individual, "Individual Event", "#9c27b0", e => e.EventFormat == EventFormat.Individual),
|
||||
new(OnSite, "On-Site Activity", "#ff9800", e => e.OnSiteActivity),
|
||||
new(Regional, "Regional Event", "#2196f3", e => e.RegionalEvent),
|
||||
new(Presubmission, "Presubmission", "#4caf50", e => e.Presubmission)
|
||||
];
|
||||
|
||||
public static string For(EventDefinition? evt)
|
||||
{
|
||||
if (evt is null)
|
||||
return string.Empty;
|
||||
|
||||
return string.Join(
|
||||
" ",
|
||||
LegendItems.Where(m => m.Applies(evt)).Select(m => m.Symbol));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Rank labels and colors shared by the ranking index, print badges, and legend.
|
||||
/// </summary>
|
||||
public static class EventRankLegend
|
||||
{
|
||||
public static readonly IReadOnlyList<(int Rank, string Label)> Items =
|
||||
[
|
||||
.. Enumerable.Range(1, StudentEventRanking.MaxRank)
|
||||
.Select(rank => (rank, Ordinal(rank)))
|
||||
];
|
||||
|
||||
public static string Ordinal(int num)
|
||||
{
|
||||
if (num <= 0)
|
||||
return num.ToString();
|
||||
|
||||
switch (num % 100)
|
||||
{
|
||||
case 11:
|
||||
case 12:
|
||||
case 13:
|
||||
return num + "th";
|
||||
}
|
||||
|
||||
return (num % 10) switch
|
||||
{
|
||||
1 => num + "st",
|
||||
2 => num + "nd",
|
||||
3 => num + "rd",
|
||||
_ => num + "th"
|
||||
};
|
||||
}
|
||||
|
||||
public static string ColorHex(int rank) =>
|
||||
rank switch
|
||||
{
|
||||
1 => "#dd7e6b",
|
||||
2 => "#ea9999",
|
||||
3 => "#f9cb9c",
|
||||
4 => "#ffe599",
|
||||
5 => "#fff2cc",
|
||||
6 => "#fffaea",
|
||||
7 => "#fffefa",
|
||||
8 => "#fffefc",
|
||||
9 => "#fffffd",
|
||||
10 => "#fffffe",
|
||||
_ => "#ddd"
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Splits a markdown template that contains exactly one table so a roster can
|
||||
/// share one header and append merged body rows per record.
|
||||
/// </summary>
|
||||
public sealed class MarkdownTableStencil
|
||||
{
|
||||
public required string Prefix { get; init; }
|
||||
|
||||
public required string Header { get; init; }
|
||||
|
||||
public required string Body { get; init; }
|
||||
|
||||
public required string Suffix { get; init; }
|
||||
|
||||
public static bool TryParse(string? template, out MarkdownTableStencil? stencil)
|
||||
{
|
||||
stencil = null;
|
||||
if (string.IsNullOrWhiteSpace(template))
|
||||
return false;
|
||||
|
||||
var lines = SplitLines(template);
|
||||
if (!TryFindTable(lines, 0, out var headerIndex, out var bodyStart, out var bodyEnd))
|
||||
return false;
|
||||
|
||||
if (bodyStart >= bodyEnd)
|
||||
return false;
|
||||
|
||||
if (TryFindTable(lines, bodyEnd, out _, out _, out _))
|
||||
return false;
|
||||
|
||||
stencil = new MarkdownTableStencil
|
||||
{
|
||||
Prefix = JoinLines(lines[..headerIndex]),
|
||||
Header = JoinLines(lines[headerIndex..bodyStart]),
|
||||
Body = JoinLines(lines[bodyStart..bodyEnd]),
|
||||
Suffix = JoinLines(lines[bodyEnd..])
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
public string Stitch(string prefix, IEnumerable<string> bodies, string suffix)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(bodies);
|
||||
|
||||
List<string> parts = [];
|
||||
AppendPart(parts, prefix);
|
||||
AppendPart(parts, Header);
|
||||
foreach (var body in bodies)
|
||||
AppendPart(parts, body);
|
||||
AppendPart(parts, suffix);
|
||||
|
||||
return parts.Count == 0 ? string.Empty : string.Join('\n', parts) + "\n";
|
||||
}
|
||||
|
||||
private static void AppendPart(List<string> parts, string? part)
|
||||
{
|
||||
if (string.IsNullOrEmpty(part))
|
||||
return;
|
||||
|
||||
var trimmed = part.TrimEnd('\r', '\n');
|
||||
if (trimmed.Length > 0)
|
||||
parts.Add(trimmed);
|
||||
}
|
||||
|
||||
private static bool TryFindTable(
|
||||
string[] lines,
|
||||
int start,
|
||||
out int headerIndex,
|
||||
out int bodyStart,
|
||||
out int bodyEnd)
|
||||
{
|
||||
headerIndex = -1;
|
||||
bodyStart = -1;
|
||||
bodyEnd = -1;
|
||||
|
||||
for (var i = start; i < lines.Length - 1; i++)
|
||||
{
|
||||
if (!IsTableLine(lines[i]) || !IsSeparatorLine(lines[i + 1]))
|
||||
continue;
|
||||
|
||||
headerIndex = i;
|
||||
bodyStart = i + 2;
|
||||
bodyEnd = bodyStart;
|
||||
while (bodyEnd < lines.Length && IsTableLine(lines[bodyEnd]) && !IsSeparatorLine(lines[bodyEnd]))
|
||||
bodyEnd++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsTableLine(string line)
|
||||
{
|
||||
var trimmed = line.TrimStart();
|
||||
return trimmed.StartsWith('|');
|
||||
}
|
||||
|
||||
private static bool IsSeparatorLine(string line) =>
|
||||
IsTableLine(line) && line.Contains("---", StringComparison.Ordinal);
|
||||
|
||||
private static string[] SplitLines(string text) =>
|
||||
text.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n').Split('\n');
|
||||
|
||||
private static string JoinLines(string[] lines) =>
|
||||
lines.Length == 0 ? string.Empty : string.Join('\n', lines);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Replaces <c>{{Token}}</c> placeholders from a case-insensitive map.
|
||||
/// Unknown tokens are left unchanged. Known empty values become blank.
|
||||
/// <c>{{PageBreak}}</c> becomes a print page break after HTML conversion.
|
||||
/// <c>{{AnswerSpace}}</c> becomes ruled write-in space after HTML conversion.
|
||||
/// <c>{{RankedEvents}}</c> and <c>{{RankedStudents}}</c> become ranking-index badges.
|
||||
/// <c>{{Legend}}</c> becomes the shared attribute-mark legend.
|
||||
/// </summary>
|
||||
public static class NoteTemplateMerger
|
||||
{
|
||||
public const string PageBreakToken = "PageBreak";
|
||||
public const string PageBreakSentinel = "<!--tsa-page-break-->";
|
||||
public const string PageBreakHtml = "<div class=\"pagebreak\"></div>";
|
||||
|
||||
public const string AnswerSpaceToken = "AnswerSpace";
|
||||
public const string AnswerSpaceSentinel = "<!--tsa-answer-space-->";
|
||||
public const string AnswerSpaceHtml = "<div class=\"print-answer-space\"> </div>";
|
||||
|
||||
public const string LegendToken = "Legend";
|
||||
public const string LegendSentinel = "<!--tsa-legend-->";
|
||||
|
||||
public const string RankedEventsToken = "RankedEvents";
|
||||
public const string RankedStudentsToken = "RankedStudents";
|
||||
|
||||
public static readonly string[] HtmlFragmentTokens =
|
||||
[
|
||||
RankedEventsToken,
|
||||
RankedStudentsToken
|
||||
];
|
||||
|
||||
public static string HtmlFragmentSentinel(string name) => $"<!--tsa-html:{name}-->";
|
||||
|
||||
private readonly record struct LayoutToken(string Name, string Sentinel, string Html);
|
||||
|
||||
private static readonly LayoutToken[] LayoutTokens =
|
||||
[
|
||||
new(PageBreakToken, PageBreakSentinel, PageBreakHtml),
|
||||
new(AnswerSpaceToken, AnswerSpaceSentinel, AnswerSpaceHtml),
|
||||
new(LegendToken, LegendSentinel, PrintRankBadgeHtml.Legend())
|
||||
];
|
||||
|
||||
private static readonly Regex TokenRegex = new(@"\{\{([^}]+)\}\}", RegexOptions.Compiled);
|
||||
|
||||
public static string Merge(string? template, IReadOnlyDictionary<string, string> tokens)
|
||||
{
|
||||
if (string.IsNullOrEmpty(template))
|
||||
return string.Empty;
|
||||
|
||||
ArgumentNullException.ThrowIfNull(tokens);
|
||||
|
||||
return TokenRegex.Replace(template, match =>
|
||||
{
|
||||
var key = match.Groups[1].Value.Trim();
|
||||
if (key.Length == 0)
|
||||
return match.Value;
|
||||
|
||||
foreach (var layout in LayoutTokens)
|
||||
{
|
||||
if (key.Equals(layout.Name, StringComparison.OrdinalIgnoreCase))
|
||||
return layout.Sentinel;
|
||||
}
|
||||
|
||||
foreach (var htmlName in HtmlFragmentTokens)
|
||||
{
|
||||
if (key.Equals(htmlName, StringComparison.OrdinalIgnoreCase))
|
||||
return HtmlFragmentSentinel(htmlName);
|
||||
}
|
||||
|
||||
return tokens.TryGetValue(key, out var value)
|
||||
? value ?? string.Empty
|
||||
: match.Value;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns layout sentinels into HTML after markdown has been rendered.
|
||||
/// </summary>
|
||||
public static string ApplyLayout(
|
||||
string? html,
|
||||
IReadOnlyDictionary<string, string>? htmlFragments = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(html))
|
||||
return string.Empty;
|
||||
|
||||
foreach (var layout in LayoutTokens)
|
||||
{
|
||||
html = html
|
||||
.Replace($"<p>{layout.Sentinel}</p>", layout.Html, StringComparison.Ordinal)
|
||||
.Replace(layout.Sentinel, layout.Html, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
foreach (var name in HtmlFragmentTokens)
|
||||
{
|
||||
var sentinel = HtmlFragmentSentinel(name);
|
||||
var fragment = htmlFragments is not null
|
||||
&& htmlFragments.TryGetValue(name, out var value)
|
||||
? value ?? string.Empty
|
||||
: string.Empty;
|
||||
html = html
|
||||
.Replace($"<p>{sentinel}</p>", fragment, StringComparison.Ordinal)
|
||||
.Replace(sentinel, fragment, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
return html;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Entity a print preset merges a note onto.
|
||||
/// </summary>
|
||||
public enum PrintEntityType
|
||||
{
|
||||
Student,
|
||||
Team,
|
||||
Event
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Built-in merge token names. Imported student-note field names are supplied at runtime.
|
||||
/// </summary>
|
||||
public static class PrintFieldCatalog
|
||||
{
|
||||
public static readonly string[] Layout =
|
||||
[
|
||||
NoteTemplateMerger.PageBreakToken,
|
||||
NoteTemplateMerger.AnswerSpaceToken,
|
||||
NoteTemplateMerger.LegendToken
|
||||
];
|
||||
|
||||
public static readonly string[] Chapter =
|
||||
[
|
||||
"Chapter.Name",
|
||||
"Chapter.ShortName",
|
||||
"Chapter.CompetitionYear",
|
||||
"Chapter.YearlyTheme",
|
||||
"Chapter.StateAbbrev"
|
||||
];
|
||||
|
||||
public static readonly string[] Student =
|
||||
[
|
||||
"FirstName",
|
||||
"LastName",
|
||||
"Name",
|
||||
"LastNameFirstName",
|
||||
"Grade",
|
||||
"TsaYear",
|
||||
"Email",
|
||||
"PhoneNumber",
|
||||
"StateId",
|
||||
"RegionalId",
|
||||
"NationalId",
|
||||
"OfficerRole"
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// <c>Rank1</c>…<c>Rank10</c> and matching <c>.ShortName</c> tokens from
|
||||
/// <see cref="StudentRankTokens"/>.
|
||||
/// </summary>
|
||||
public static readonly string[] StudentRanks = [.. StudentRankTokens.AllNames];
|
||||
|
||||
public static readonly string[] StudentRanks1To6 = [.. RankTokens(1, 6)];
|
||||
|
||||
public static readonly string[] StudentRanks7To10 = [.. RankTokens(7, 10)];
|
||||
|
||||
public static IReadOnlyList<string> RankTokens(int fromRank, int toRank) =>
|
||||
[
|
||||
.. Enumerable.Range(fromRank, toRank - fromRank + 1)
|
||||
.SelectMany(rank => (string[])
|
||||
[
|
||||
StudentRankTokens.NameToken(rank),
|
||||
StudentRankTokens.ShortNameToken(rank),
|
||||
StudentRankTokens.AttributesToken(rank)
|
||||
])
|
||||
];
|
||||
|
||||
public static readonly string[] Team =
|
||||
[
|
||||
"Identifier",
|
||||
"Name",
|
||||
"EventName",
|
||||
"EventShortName",
|
||||
"EventFormat",
|
||||
"TeamSize",
|
||||
"NationalEligibility",
|
||||
"Eligibility",
|
||||
"RegionalTeamCount",
|
||||
"StateTeamCount",
|
||||
"Description",
|
||||
"Theme",
|
||||
"EventAttributes"
|
||||
];
|
||||
|
||||
public static readonly string[] Event =
|
||||
[
|
||||
"Name",
|
||||
"ShortName",
|
||||
"EventFormat",
|
||||
"TeamSize",
|
||||
"NationalEligibility",
|
||||
"Eligibility",
|
||||
"RegionalTeamCount",
|
||||
"StateTeamCount",
|
||||
"LevelOfEffort",
|
||||
"SemifinalistActivity",
|
||||
"RegionalEvent",
|
||||
"Description",
|
||||
"Theme",
|
||||
"Documentation",
|
||||
"Notes",
|
||||
"EventAttributes",
|
||||
NoteTemplateMerger.RankedStudentsToken
|
||||
];
|
||||
|
||||
public static IReadOnlyList<string> EntityTokens(PrintEntityType entityType) =>
|
||||
entityType switch
|
||||
{
|
||||
PrintEntityType.Student => Student,
|
||||
PrintEntityType.Team => Team,
|
||||
PrintEntityType.Event => Event,
|
||||
_ => []
|
||||
};
|
||||
|
||||
public static IReadOnlyList<string> BuiltInFor(PrintEntityType entityType) =>
|
||||
entityType switch
|
||||
{
|
||||
PrintEntityType.Student => [.. Chapter, .. Student, .. StudentRanks],
|
||||
_ => [.. Chapter, .. EntityTokens(entityType)]
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Filter payload stored on a <see cref="PrintPreset"/>. Unused fields stay null.
|
||||
/// </summary>
|
||||
public class PrintPresetFilters
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
Converters = { new JsonStringEnumConverter() }
|
||||
};
|
||||
|
||||
public int? Grade { get; set; }
|
||||
|
||||
public int? TsaYear { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <c>true</c> officers only, <c>false</c> non-officers only, <c>null</c> any.
|
||||
/// </summary>
|
||||
public bool? IsOfficer { get; set; }
|
||||
|
||||
public string? TeamIdentifierContains { get; set; }
|
||||
|
||||
public string? EventNameContains { get; set; }
|
||||
|
||||
public EventFormat? EventFormat { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <c>true</c> regional only, <c>false</c> non-regional only, <c>null</c> any.
|
||||
/// </summary>
|
||||
public bool? RegionalOnly { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When true (default), each merged record starts a new printed page.
|
||||
/// <c>{{PageBreak}}</c> in the template still works either way.
|
||||
/// </summary>
|
||||
public bool NewPagePerRecord { get; set; } = true;
|
||||
|
||||
public const int DefaultFontSizePt = 12;
|
||||
public const int MinFontSizePt = 9;
|
||||
public const int MaxFontSizePt = 18;
|
||||
public const int DefaultAnswerSpaceLines = 3;
|
||||
public const int MinAnswerSpaceLines = 1;
|
||||
public const int MaxAnswerSpaceLines = 8;
|
||||
|
||||
/// <summary>
|
||||
/// Body font size in points for merged pages.
|
||||
/// </summary>
|
||||
public int FontSizePt { get; set; } = DefaultFontSizePt;
|
||||
|
||||
/// <summary>
|
||||
/// Ruled write-in lines for each <c>{{AnswerSpace}}</c>.
|
||||
/// </summary>
|
||||
public int AnswerSpaceLines { get; set; } = DefaultAnswerSpaceLines;
|
||||
|
||||
public string ToJson() => JsonSerializer.Serialize(this, JsonOptions);
|
||||
|
||||
public static PrintPresetFilters FromJson(string? json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
return new PrintPresetFilters();
|
||||
|
||||
var filters = JsonSerializer.Deserialize<PrintPresetFilters>(json, JsonOptions)
|
||||
?? new PrintPresetFilters();
|
||||
filters.ClampPrintOptions();
|
||||
return filters;
|
||||
}
|
||||
|
||||
public void ClampPrintOptions()
|
||||
{
|
||||
FontSizePt = Math.Clamp(FontSizePt, MinFontSizePt, MaxFontSizePt);
|
||||
AnswerSpaceLines = Math.Clamp(AnswerSpaceLines, MinAnswerSpaceLines, MaxAnswerSpaceLines);
|
||||
}
|
||||
|
||||
public static string ToTriState(bool? value) => value switch
|
||||
{
|
||||
true => "yes",
|
||||
false => "no",
|
||||
_ => "any"
|
||||
};
|
||||
|
||||
public static bool? FromTriState(string? value) => value switch
|
||||
{
|
||||
"yes" => true,
|
||||
"no" => false,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Ranking-index style badges: colored rank dot plus a short label.
|
||||
/// Student pages list ranked events; event pages list students who ranked them.
|
||||
/// </summary>
|
||||
public static class PrintRankBadgeHtml
|
||||
{
|
||||
public static string ForStudentEvents(IEnumerable<StudentEventRanking>? rankings)
|
||||
{
|
||||
if (rankings is null)
|
||||
return string.Empty;
|
||||
|
||||
var badges = rankings
|
||||
.Where(r => r.Rank is >= 1 and <= StudentEventRanking.MaxRank)
|
||||
.OrderBy(r => r.Rank)
|
||||
.Select(EventBadge)
|
||||
.ToList();
|
||||
|
||||
return Wrap(badges);
|
||||
}
|
||||
|
||||
public static string ForEventStudents(IEnumerable<StudentEventRanking>? rankings)
|
||||
{
|
||||
if (rankings is null)
|
||||
return string.Empty;
|
||||
|
||||
var badges = rankings
|
||||
.Where(r => r.Student is not null && r.Rank is >= 1 and <= StudentEventRanking.MaxRank)
|
||||
.OrderBy(r => r.Rank)
|
||||
.ThenByDescending(r => r.Student.Grade + r.Student.TsaYear)
|
||||
.Select(r => StudentBadge(r.Student.FirstName, r.Rank))
|
||||
.ToList();
|
||||
|
||||
return Wrap(badges);
|
||||
}
|
||||
|
||||
internal static string EventBadge(StudentEventRanking ranking)
|
||||
{
|
||||
var evt = ranking.EventDefinition;
|
||||
var label = !string.IsNullOrWhiteSpace(evt?.ShortName)
|
||||
? evt.ShortName
|
||||
: evt?.Name;
|
||||
var attributes = EventAttributeMarks.For(evt);
|
||||
var attrsHtml = string.IsNullOrEmpty(attributes)
|
||||
? string.Empty
|
||||
: $"<span class=\"print-event-attrs\">{PrintTokenMap.EscapeHtml(attributes)}</span>";
|
||||
|
||||
return Badge(ranking.Rank, PrintTokenMap.EscapeHtml(label), attrsHtml);
|
||||
}
|
||||
|
||||
internal static string StudentBadge(string? firstName, int rank) =>
|
||||
Badge(rank, PrintTokenMap.EscapeHtml(firstName), string.Empty);
|
||||
|
||||
private static string Badge(int rank, string labelHtml, string extraHtml)
|
||||
{
|
||||
var extra = string.IsNullOrEmpty(extraHtml) ? string.Empty : $" {extraHtml}";
|
||||
return
|
||||
$"<span class=\"print-rank-badge\">" +
|
||||
$"<span class=\"print-rank-dot event-rank-{rank}\"></span> " +
|
||||
$"{labelHtml}{extra}" +
|
||||
"</span>";
|
||||
}
|
||||
|
||||
public static string Legend()
|
||||
{
|
||||
var marks = EventAttributeMarks.LegendItems
|
||||
.Select(mark =>
|
||||
"<span class=\"print-legend-mark\">" +
|
||||
$"<span class=\"print-event-attrs\">{PrintTokenMap.EscapeHtml(mark.Symbol)}</span> " +
|
||||
PrintTokenMap.EscapeHtml(mark.Label) +
|
||||
"</span>");
|
||||
|
||||
return
|
||||
"<div class=\"print-badge-legend\">" +
|
||||
$"<div class=\"print-attr-legend\">{string.Join(" · ", marks)}</div>" +
|
||||
"</div>";
|
||||
}
|
||||
|
||||
private static string Wrap(IReadOnlyList<string> badges, string separator = " ")
|
||||
{
|
||||
if (badges.Count == 0)
|
||||
return string.Empty;
|
||||
|
||||
return $"<div class=\"print-rank-badges\">{string.Join(separator, badges)}</div>";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Builds a case-insensitive token map. Fill order is imported, then entity, then chapter
|
||||
/// so built-in names win over an imported column with the same name.
|
||||
/// </summary>
|
||||
public static class PrintTokenMap
|
||||
{
|
||||
public static Dictionary<string, string> Create() =>
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static Dictionary<string, string> Build(
|
||||
IReadOnlyDictionary<string, string?>? imported,
|
||||
IReadOnlyDictionary<string, string?>? entity,
|
||||
IReadOnlyDictionary<string, string?>? chapter)
|
||||
{
|
||||
var map = Create();
|
||||
Apply(map, imported);
|
||||
Apply(map, entity);
|
||||
Apply(map, chapter);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void Apply(IDictionary<string, string> map, IReadOnlyDictionary<string, string?>? values)
|
||||
{
|
||||
if (values is null)
|
||||
return;
|
||||
|
||||
foreach (var (key, value) in values)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
continue;
|
||||
map[key] = Escape(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Treats substituted values as plain text so they cannot change markdown or inject HTML.
|
||||
/// Line breaks are flattened so a value cannot end a markdown table row.
|
||||
/// </summary>
|
||||
public static string Escape(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return string.Empty;
|
||||
|
||||
return FlattenLines(value)
|
||||
.Replace("\\", "\\\\", StringComparison.Ordinal)
|
||||
.Replace("|", "\\|", StringComparison.Ordinal)
|
||||
.Replace("*", "\\*", StringComparison.Ordinal)
|
||||
.Replace("_", "\\_", StringComparison.Ordinal)
|
||||
.Replace("`", "\\`", StringComparison.Ordinal)
|
||||
.Replace("[", "\\[", StringComparison.Ordinal)
|
||||
.Replace("]", "\\]", StringComparison.Ordinal)
|
||||
.Replace("<", "<", StringComparison.Ordinal)
|
||||
.Replace(">", ">", StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Escapes a value for insertion into generated print HTML (badge labels).
|
||||
/// </summary>
|
||||
public static string EscapeHtml(string? value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return string.Empty;
|
||||
|
||||
return FlattenLines(value)
|
||||
.Replace("&", "&", StringComparison.Ordinal)
|
||||
.Replace("<", "<", StringComparison.Ordinal)
|
||||
.Replace(">", ">", StringComparison.Ordinal)
|
||||
.Replace("\"", """, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static string FlattenLines(string value) =>
|
||||
string.Join(' ',
|
||||
value.Split(['\r', '\n', '\u2028', '\u2029'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace Core.Printing;
|
||||
|
||||
/// <summary>
|
||||
/// Per-student event-rank merge tokens. <c>Rank1</c> is the official event name
|
||||
/// at rank 1; <c>Rank1.ShortName</c> is the catalog short name. Every rank
|
||||
/// through <see cref="StudentEventRanking.MaxRank"/> is always a map key so a
|
||||
/// missing preference prints blank instead of leaving <c>{{Rank5}}</c> visible.
|
||||
/// </summary>
|
||||
public static class StudentRankTokens
|
||||
{
|
||||
public static string NameToken(int rank) => $"Rank{rank}";
|
||||
|
||||
public static string ShortNameToken(int rank) => $"Rank{rank}.ShortName";
|
||||
|
||||
public static string AttributesToken(int rank) => $"Rank{rank}.Attributes";
|
||||
|
||||
public static IReadOnlyList<string> AllNames { get; } =
|
||||
[
|
||||
NoteTemplateMerger.RankedEventsToken,
|
||||
.. Enumerable.Range(1, StudentEventRanking.MaxRank)
|
||||
.SelectMany(rank => (string[])
|
||||
[
|
||||
NameToken(rank),
|
||||
ShortNameToken(rank),
|
||||
AttributesToken(rank)
|
||||
])
|
||||
];
|
||||
|
||||
public static Dictionary<string, string?> FromRankings(IEnumerable<StudentEventRanking>? rankings)
|
||||
{
|
||||
var map = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
for (var rank = 1; rank <= StudentEventRanking.MaxRank; rank++)
|
||||
{
|
||||
map[NameToken(rank)] = null;
|
||||
map[ShortNameToken(rank)] = null;
|
||||
map[AttributesToken(rank)] = null;
|
||||
}
|
||||
|
||||
if (rankings is null)
|
||||
return map;
|
||||
|
||||
foreach (var ranking in rankings)
|
||||
{
|
||||
if (ranking.Rank < 1 || ranking.Rank > StudentEventRanking.MaxRank)
|
||||
continue;
|
||||
|
||||
var evt = ranking.EventDefinition;
|
||||
map[NameToken(ranking.Rank)] = evt?.Name;
|
||||
map[ShortNameToken(ranking.Rank)] = evt?.ShortName;
|
||||
map[AttributesToken(ranking.Rank)] = EventAttributeMarks.For(evt);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,19 @@ public interface INoteNamingService
|
||||
/// <param name="noteTitle">The note title to check</param>
|
||||
/// <returns>True if the note is a meeting note, false otherwise</returns>
|
||||
bool IsMeetingNote(string noteTitle);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the title for a student note. Format: "#Student:{id}"
|
||||
/// </summary>
|
||||
string GetStudentNoteTitle(int studentId);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a note title is a student note.
|
||||
/// </summary>
|
||||
bool IsStudentNote(string? noteTitle);
|
||||
|
||||
/// <summary>
|
||||
/// Parses the student id from a student note title.
|
||||
/// </summary>
|
||||
bool TryParseStudentNoteId(string? noteTitle, out int studentId);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
|
||||
namespace Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Parses student field CSVs and merges them into markdown notes.
|
||||
/// </summary>
|
||||
public interface IStudentNotesImportService
|
||||
{
|
||||
StudentNotesImportResult Parse(
|
||||
Stream stream,
|
||||
ICollection<Student> students,
|
||||
IReadOnlyDictionary<int, string?> existingNotesByStudentId);
|
||||
}
|
||||
@@ -8,6 +8,7 @@ public class NoteNamingService : INoteNamingService
|
||||
{
|
||||
private const string PageNotePrefix = "#";
|
||||
private const string MeetingNotePrefix = "#Meeting Notes";
|
||||
private const string StudentNotePrefix = "#Student:";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GetMeetingNoteTitle(DateTime meetingDate)
|
||||
@@ -47,4 +48,26 @@ public class NoteNamingService : INoteNamingService
|
||||
|
||||
return noteTitle.StartsWith(MeetingNotePrefix, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GetStudentNoteTitle(int studentId) => $"{StudentNotePrefix}{studentId}";
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsStudentNote(string? noteTitle)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(noteTitle))
|
||||
return false;
|
||||
|
||||
return noteTitle.StartsWith(StudentNotePrefix, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool TryParseStudentNoteId(string? noteTitle, out int studentId)
|
||||
{
|
||||
studentId = 0;
|
||||
if (!IsStudentNote(noteTitle))
|
||||
return false;
|
||||
|
||||
return int.TryParse(noteTitle.AsSpan(StudentNotePrefix.Length), out studentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using Core.Models;
|
||||
|
||||
namespace Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Decides which parsed student notes should be created or updated.
|
||||
/// Unchanged merges are omitted so a re-import does not write history.
|
||||
/// </summary>
|
||||
public static class StudentNotesImportPlan
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds persist actions from a parse result. Only matches with field changes are included.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<StudentNotePersistAction> Create(
|
||||
StudentNotesImportResult parseResult,
|
||||
IReadOnlySet<int> studentIdsWithExistingNotes)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parseResult);
|
||||
ArgumentNullException.ThrowIfNull(studentIdsWithExistingNotes);
|
||||
|
||||
List<StudentNotePersistAction> actions = [];
|
||||
foreach (var match in parseResult.Matches)
|
||||
{
|
||||
if (!match.Merge.Changed)
|
||||
continue;
|
||||
|
||||
actions.Add(new StudentNotePersistAction
|
||||
{
|
||||
StudentId = match.Student.Id,
|
||||
Markdown = match.Merge.Markdown,
|
||||
Kind = studentIdsWithExistingNotes.Contains(match.Student.Id)
|
||||
? StudentNotePersistKind.Update
|
||||
: StudentNotePersistKind.Create
|
||||
});
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
|
||||
public enum StudentNotePersistKind
|
||||
{
|
||||
Create,
|
||||
Update
|
||||
}
|
||||
|
||||
public class StudentNotePersistAction
|
||||
{
|
||||
public required int StudentId { get; init; }
|
||||
|
||||
public required string Markdown { get; init; }
|
||||
|
||||
public required StudentNotePersistKind Kind { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
using Core.Parsers;
|
||||
|
||||
namespace Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Wraps <see cref="StudentNotesFieldParser"/> for stream-based import.
|
||||
/// </summary>
|
||||
public class StudentNotesImportService : IStudentNotesImportService
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public StudentNotesImportResult Parse(
|
||||
Stream stream,
|
||||
ICollection<Student> students,
|
||||
IReadOnlyDictionary<int, string?> existingNotesByStudentId)
|
||||
{
|
||||
var reader = new StreamReader(stream, leaveOpen: true);
|
||||
using var parser = new StudentNotesFieldParser(reader);
|
||||
return parser.Parse(students, existingNotesByStudentId);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public static class StudentNameFormatter
|
||||
if (student == null)
|
||||
return string.Empty;
|
||||
|
||||
var name = student.FirstName;
|
||||
var name = student.DisplayFirstName;
|
||||
|
||||
// Add overlap marker
|
||||
if (options.HasOverlap)
|
||||
|
||||
@@ -107,7 +107,7 @@ public static class TeamStudentNameFormatter
|
||||
if (student == null)
|
||||
return string.Empty;
|
||||
|
||||
var name = student.FirstName;
|
||||
var name = student.DisplayFirstName;
|
||||
|
||||
// Add captain indicator (before overlap/absent markers)
|
||||
if (options.CaptainIndicator != CaptainIndicatorStyle.None && team != null && team.Captain != null && team.Captain.Equals(student))
|
||||
@@ -137,7 +137,7 @@ public static class TeamStudentNameFormatter
|
||||
|
||||
// Get the suffix from StudentNameFormatter (overlap/absent markers)
|
||||
var baseFormatted = StudentNameFormatter.FormatStudentName(student, studentNameOptions);
|
||||
var suffix = baseFormatted.Substring(student.FirstName.Length);
|
||||
var suffix = baseFormatted.Substring(student.DisplayFirstName.Length);
|
||||
|
||||
return name + suffix;
|
||||
}
|
||||
@@ -214,10 +214,10 @@ public static class TeamStudentNameFormatter
|
||||
{
|
||||
OrderingStyle.CaptainFirst => studentsWithCaptainInfo
|
||||
.OrderBy(x => !x.IsCaptain)
|
||||
.ThenBy(x => x.Student.FirstName)
|
||||
.ThenBy(x => x.Student.DisplayFirstName)
|
||||
.Select(x => x.Student),
|
||||
OrderingStyle.Alphabetical => studentsWithCaptainInfo
|
||||
.OrderBy(x => x.Student.FirstName)
|
||||
.OrderBy(x => x.Student.DisplayFirstName)
|
||||
.Select(x => x.Student),
|
||||
OrderingStyle.GradeDescending => studentsWithCaptainInfo
|
||||
.OrderByDescending(x => x.Student.Grade + x.Student.TsaYear)
|
||||
@@ -252,8 +252,8 @@ public static class TeamStudentNameFormatter
|
||||
{
|
||||
OrderingStyle.CaptainFirst => students
|
||||
.OrderBy(s => team.Captain == null || !team.Captain.Equals(s))
|
||||
.ThenBy(s => s.FirstName),
|
||||
OrderingStyle.Alphabetical => students.OrderBy(s => s.FirstName),
|
||||
.ThenBy(s => s.DisplayFirstName),
|
||||
OrderingStyle.Alphabetical => students.OrderBy(s => s.DisplayFirstName),
|
||||
OrderingStyle.GradeDescending => students.OrderByDescending(s => s.Grade + s.TsaYear),
|
||||
_ => students
|
||||
};
|
||||
|
||||
@@ -242,7 +242,9 @@ public static class YearTransitionPlanner
|
||||
// Exact full-name matches first
|
||||
var fullMatches = students.Where(s =>
|
||||
comparer.Equals(s.FirstNameLastName, line) ||
|
||||
comparer.Equals(s.LastNameFirstName, line)).ToList();
|
||||
comparer.Equals(s.LastNameFirstName, line) ||
|
||||
comparer.Equals($"{s.DisplayFirstName} {s.LastName}", line) ||
|
||||
comparer.Equals($"{s.LastName}, {s.DisplayFirstName}", line)).ToList();
|
||||
if (fullMatches.Count > 0)
|
||||
return fullMatches;
|
||||
|
||||
@@ -251,8 +253,10 @@ public static class YearTransitionPlanner
|
||||
return [];
|
||||
|
||||
return students.Where(s =>
|
||||
comparer.Equals(s.FirstName.Trim(), first) &&
|
||||
comparer.Equals(s.LastName.Trim(), last));
|
||||
comparer.Equals(s.LastName.Trim(), last) &&
|
||||
(comparer.Equals(s.FirstName.Trim(), first)
|
||||
|| comparer.Equals(s.DisplayFirstName, first)
|
||||
|| comparer.Equals(s.Nickname?.Trim(), first)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace Data
|
||||
public DbSet<Note> Notes { get; set; }
|
||||
public DbSet<NoteHistory> NoteHistories { get; set; }
|
||||
public DbSet<TeamMeetingHistory> TeamMeetingHistories { get; set; }
|
||||
public DbSet<PrintPreset> PrintPresets { get; set; }
|
||||
|
||||
public AppDbContext()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using Core.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Data.Configurations;
|
||||
|
||||
public class PrintPresetConfiguration : IEntityTypeConfiguration<PrintPreset>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PrintPreset> builder)
|
||||
{
|
||||
builder.HasKey(p => p.Id);
|
||||
|
||||
builder.Property(p => p.Name)
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.HasIndex(p => p.Name)
|
||||
.IsUnique();
|
||||
|
||||
builder.Property(p => p.EntityType)
|
||||
.HasConversion<string>()
|
||||
.HasMaxLength(32)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(p => p.FiltersJson)
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
builder.Property(p => p.TemplateMarkdown)
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,9 @@ namespace Data.Configurations
|
||||
.IsRequired()
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(s => s.Nickname)
|
||||
.HasMaxLength(100);
|
||||
|
||||
builder.Property(s => s.Email)
|
||||
.HasMaxLength(255);
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20260830040000_AddPrintPresets")]
|
||||
public partial class AddPrintPresets : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "PrintPresets",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "INTEGER", nullable: false)
|
||||
.Annotation("Sqlite:Autoincrement", true),
|
||||
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
|
||||
NoteId = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
EntityType = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
|
||||
FiltersJson = table.Column<string>(type: "TEXT", nullable: false),
|
||||
UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_PrintPresets", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_PrintPresets_Notes_NoteId",
|
||||
column: x => x.NoteId,
|
||||
principalTable: "Notes",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_PrintPresets_Name",
|
||||
table: "PrintPresets",
|
||||
column: "Name",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_PrintPresets_NoteId",
|
||||
table: "PrintPresets",
|
||||
column: "NoteId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "PrintPresets");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20260903010000_PrintPresetTemplateMarkdown")]
|
||||
public partial class PrintPresetTemplateMarkdown : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "TemplateMarkdown",
|
||||
table: "PrintPresets",
|
||||
type: "TEXT",
|
||||
nullable: false,
|
||||
defaultValue: "");
|
||||
|
||||
migrationBuilder.Sql(
|
||||
"""
|
||||
UPDATE PrintPresets
|
||||
SET TemplateMarkdown = COALESCE(
|
||||
(SELECT Content FROM Notes WHERE Notes.Id = PrintPresets.NoteId),
|
||||
''
|
||||
);
|
||||
""");
|
||||
|
||||
migrationBuilder.Sql(
|
||||
"""
|
||||
CREATE TABLE "PrintPresets_new" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_PrintPresets" PRIMARY KEY AUTOINCREMENT,
|
||||
"Name" TEXT NOT NULL,
|
||||
"TemplateMarkdown" TEXT NOT NULL,
|
||||
"EntityType" TEXT NOT NULL,
|
||||
"FiltersJson" TEXT NOT NULL,
|
||||
"UpdatedAt" TEXT NOT NULL
|
||||
);
|
||||
""");
|
||||
|
||||
migrationBuilder.Sql(
|
||||
"""
|
||||
INSERT INTO "PrintPresets_new" ("Id", "Name", "TemplateMarkdown", "EntityType", "FiltersJson", "UpdatedAt")
|
||||
SELECT "Id", "Name", "TemplateMarkdown", "EntityType", "FiltersJson", "UpdatedAt"
|
||||
FROM "PrintPresets";
|
||||
""");
|
||||
|
||||
migrationBuilder.Sql("""DROP TABLE "PrintPresets";""");
|
||||
migrationBuilder.Sql("""ALTER TABLE "PrintPresets_new" RENAME TO "PrintPresets";""");
|
||||
migrationBuilder.Sql(
|
||||
"""CREATE UNIQUE INDEX "IX_PrintPresets_Name" ON "PrintPresets" ("Name");""");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(
|
||||
"""
|
||||
CREATE TABLE "PrintPresets_old" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_PrintPresets" PRIMARY KEY AUTOINCREMENT,
|
||||
"Name" TEXT NOT NULL,
|
||||
"NoteId" INTEGER NOT NULL,
|
||||
"EntityType" TEXT NOT NULL,
|
||||
"FiltersJson" TEXT NOT NULL,
|
||||
"UpdatedAt" TEXT NOT NULL
|
||||
);
|
||||
""");
|
||||
|
||||
migrationBuilder.Sql(
|
||||
"""
|
||||
INSERT INTO "PrintPresets_old" ("Id", "Name", "NoteId", "EntityType", "FiltersJson", "UpdatedAt")
|
||||
SELECT "Id", "Name", 0, "EntityType", "FiltersJson", "UpdatedAt"
|
||||
FROM "PrintPresets";
|
||||
""");
|
||||
|
||||
migrationBuilder.Sql("""DROP TABLE "PrintPresets";""");
|
||||
migrationBuilder.Sql("""ALTER TABLE "PrintPresets_old" RENAME TO "PrintPresets";""");
|
||||
migrationBuilder.Sql(
|
||||
"""CREATE UNIQUE INDEX "IX_PrintPresets_Name" ON "PrintPresets" ("Name");""");
|
||||
migrationBuilder.Sql(
|
||||
"""CREATE INDEX "IX_PrintPresets_NoteId" ON "PrintPresets" ("NoteId");""");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Data;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20260911190000_AddStudentNickname")]
|
||||
public partial class AddStudentNickname : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "Nickname",
|
||||
table: "Students",
|
||||
type: "TEXT",
|
||||
maxLength: 100,
|
||||
nullable: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "Nickname",
|
||||
table: "Students");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,6 +177,41 @@ namespace Data.Migrations
|
||||
b.ToTable("EventOccurrences");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Core.Entities.PrintPreset", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("EntityType")
|
||||
.IsRequired()
|
||||
.HasMaxLength(32)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("FiltersJson")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("TemplateMarkdown")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("UpdatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("PrintPresets");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Core.Entities.Note", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@@ -287,6 +322,10 @@ namespace Data.Migrations
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Nickname")
|
||||
.HasMaxLength(100)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("NationalId")
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
@@ -12,6 +12,7 @@ public class StudentBuilder
|
||||
private int _id = _idCounter++;
|
||||
private string _firstName = "Test";
|
||||
private string _lastName = "Student";
|
||||
private string? _nickname = null;
|
||||
private int _grade = 9;
|
||||
private string? _email = null;
|
||||
private string? _phoneNumber = null;
|
||||
@@ -41,6 +42,12 @@ public class StudentBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
public StudentBuilder WithNickname(string? nickname)
|
||||
{
|
||||
_nickname = nickname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StudentBuilder WithGrade(int grade)
|
||||
{
|
||||
_grade = grade;
|
||||
@@ -126,6 +133,7 @@ public class StudentBuilder
|
||||
Id = _id,
|
||||
FirstName = _firstName,
|
||||
LastName = _lastName,
|
||||
Nickname = _nickname,
|
||||
Grade = _grade,
|
||||
Email = _email,
|
||||
PhoneNumber = _phoneNumber,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Entities;
|
||||
|
||||
[TestFixture]
|
||||
public class Student_Tests
|
||||
{
|
||||
[Test]
|
||||
public void DisplayFirstName_FallsBackToFirstName()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Brown").Build();
|
||||
|
||||
Assert.That(student.DisplayFirstName, Is.EqualTo("Josiah"));
|
||||
Assert.That(student.ToString(), Is.EqualTo("Josiah"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisplayFirstName_UsesTrimmedNickname()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Brown").WithNickname(" Jo ").Build();
|
||||
|
||||
Assert.That(student.DisplayFirstName, Is.EqualTo("Jo"));
|
||||
Assert.That(student.ToString(), Is.EqualTo("Jo"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LegalNameProperties_IgnoreNickname()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
|
||||
Assert.That(student.FirstName, Is.EqualTo("Josiah"));
|
||||
Assert.That(student.LastName, Is.EqualTo("Brown"));
|
||||
Assert.That(student.Name, Is.EqualTo("Josiah Brown"));
|
||||
Assert.That(student.FirstNameLastName, Is.EqualTo("Josiah Brown"));
|
||||
Assert.That(student.LastNameFirstName, Is.EqualTo("Brown, Josiah"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisplayFirstName_TreatsWhitespaceAsUnset()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Brown").WithNickname(" ").Build();
|
||||
|
||||
Assert.That(student.DisplayFirstName, Is.EqualTo("Josiah"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NormalizeNickname_ClearsBlankAndTrims()
|
||||
{
|
||||
var blank = StudentBuilder.Create("Josiah", "Brown").WithNickname(" ").Build();
|
||||
blank.NormalizeNickname();
|
||||
Assert.That(blank.Nickname, Is.Null);
|
||||
|
||||
var nick = StudentBuilder.Create("Josiah", "Brown").WithNickname(" Jo ").Build();
|
||||
nick.NormalizeNickname();
|
||||
Assert.That(nick.Nickname, Is.EqualTo("Jo"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
using Core.Notes;
|
||||
|
||||
namespace Tests.Notes;
|
||||
|
||||
[TestFixture]
|
||||
public class ImportedFieldsTable_Tests
|
||||
{
|
||||
[Test]
|
||||
public void NormalizeValue_X_BecomesYes()
|
||||
{
|
||||
Assert.That(ImportedFieldsTable.NormalizeValue("x"), Is.EqualTo("Yes"));
|
||||
Assert.That(ImportedFieldsTable.NormalizeValue("X"), Is.EqualTo("Yes"));
|
||||
Assert.That(ImportedFieldsTable.NormalizeValue(" x "), Is.EqualTo("Yes"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NormalizeValue_Blank_StaysEmpty()
|
||||
{
|
||||
Assert.That(ImportedFieldsTable.NormalizeValue(null), Is.EqualTo(string.Empty));
|
||||
Assert.That(ImportedFieldsTable.NormalizeValue(" "), Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_IncomingWins_AndAppendsNewFields()
|
||||
{
|
||||
var existing = """
|
||||
Freeform note
|
||||
|
||||
## Imported fields
|
||||
|
||||
| Field | Value |
|
||||
| --- | --- |
|
||||
| Interview Time | 1:00-1:15 |
|
||||
| Application | Yes |
|
||||
""";
|
||||
|
||||
var result = ImportedFieldsTable.Merge(existing,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Teacher Rec 1", "Fuqua")
|
||||
]);
|
||||
|
||||
Assert.That(result.Changed, Is.True);
|
||||
Assert.That(result.Changes, Has.Count.EqualTo(2));
|
||||
var fields = ImportedFieldsTable.ParseFields(result.Markdown);
|
||||
Assert.That(fields.Single(f => f.Name == "Interview Time").Value, Is.EqualTo("3:20-3:35"));
|
||||
Assert.That(fields.Single(f => f.Name == "Application").Value, Is.EqualTo("Yes"));
|
||||
Assert.That(fields.Single(f => f.Name == "Teacher Rec 1").Value, Is.EqualTo("Fuqua"));
|
||||
Assert.That(result.Markdown, Does.Contain("Freeform note"));
|
||||
Assert.That(result.Markdown, Does.Contain(ImportedFieldsTable.Heading));
|
||||
Assert.That(result.Markdown, Does.Not.Contain("## Imported fields"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseFields_ReadsAnyFieldsHeading()
|
||||
{
|
||||
foreach (var heading in new[] { "## Additional fields", "## Imported fields", "## Extra fields" })
|
||||
{
|
||||
var markdown = $"""
|
||||
{heading}
|
||||
|
||||
| Field | Value |
|
||||
| --- | --- |
|
||||
| Allergies | peanuts |
|
||||
""";
|
||||
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Allergies"), Is.EqualTo("peanuts"), heading);
|
||||
}
|
||||
|
||||
var otherHeading = """
|
||||
## Advisor comments
|
||||
|
||||
| Field | Value |
|
||||
| --- | --- |
|
||||
| Allergies | peanuts |
|
||||
""";
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(otherHeading, "Allergies"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_SameDataTwice_IsNoOp()
|
||||
{
|
||||
var first = ImportedFieldsTable.Merge(null,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Application", "x")
|
||||
]);
|
||||
|
||||
Assert.That(first.Changed, Is.True);
|
||||
Assert.That(ImportedFieldsTable.ParseFields(first.Markdown).Single(f => f.Name == "Application").Value, Is.EqualTo("Yes"));
|
||||
|
||||
var second = ImportedFieldsTable.Merge(first.Markdown,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Application", "x")
|
||||
]);
|
||||
|
||||
Assert.That(second.Changed, Is.False);
|
||||
Assert.That(second.Changes, Is.Empty);
|
||||
Assert.That(ImportedFieldsTable.ParseFields(second.Markdown), Is.EqualTo(ImportedFieldsTable.ParseFields(first.Markdown)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetFieldValue_ReadsImportedTable()
|
||||
{
|
||||
var markdown = ImportedFieldsTable.FormatSection(
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35")
|
||||
]);
|
||||
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Interview Time"), Is.EqualTo("3:20-3:35"));
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Application"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_PreservesTextAfterSection()
|
||||
{
|
||||
var existing = """
|
||||
## Imported fields
|
||||
|
||||
| Field | Value |
|
||||
| --- | --- |
|
||||
| Application | Yes |
|
||||
|
||||
## Advisor comments
|
||||
|
||||
Great interview.
|
||||
""";
|
||||
|
||||
var result = ImportedFieldsTable.Merge(existing, [new ImportedField("Application", "Yes")]);
|
||||
Assert.That(result.Changed, Is.False);
|
||||
Assert.That(result.Markdown, Does.Contain("## Advisor comments"));
|
||||
Assert.That(result.Markdown, Does.Contain("Great interview."));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseFields_KeepsBlankValues()
|
||||
{
|
||||
var markdown = ImportedFieldsTable.FormatSection(
|
||||
[
|
||||
new ImportedField("Application", "Yes"),
|
||||
new ImportedField("Teacher Rec 3", "")
|
||||
]);
|
||||
|
||||
var fields = ImportedFieldsTable.ParseFields(markdown);
|
||||
Assert.That(fields.Single(f => f.Name == "Teacher Rec 3").Value, Is.EqualTo(string.Empty));
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Teacher Rec 3"), Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_BlankFields_SameDataTwice_IsNoOp()
|
||||
{
|
||||
var first = ImportedFieldsTable.Merge(null,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Teacher Rec 3", "")
|
||||
]);
|
||||
|
||||
Assert.That(first.Changed, Is.True);
|
||||
Assert.That(ImportedFieldsTable.ParseFields(first.Markdown), Has.Count.EqualTo(2));
|
||||
|
||||
var second = ImportedFieldsTable.Merge(first.Markdown,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Teacher Rec 3", "")
|
||||
]);
|
||||
|
||||
Assert.That(second.Changed, Is.False);
|
||||
Assert.That(second.Changes, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_IncomingBlank_ClearsExistingValue()
|
||||
{
|
||||
var existing = ImportedFieldsTable.Merge(null, [new ImportedField("Teacher Rec 1", "Fuqua")]).Markdown;
|
||||
var result = ImportedFieldsTable.Merge(existing, [new ImportedField("Teacher Rec 1", "")]);
|
||||
|
||||
Assert.That(result.Changed, Is.True);
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(result.Markdown, "Teacher Rec 1"), Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetFieldValue_IsCaseInsensitive()
|
||||
{
|
||||
var markdown = ImportedFieldsTable.FormatSection([new ImportedField("Interview Time", "3:20-3:35")]);
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "interview time"), Is.EqualTo("3:20-3:35"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DistinctFieldNames_UnionsNotes_AndKeepsFirstCasing()
|
||||
{
|
||||
var first = ImportedFieldsTable.FormatSection(
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Application", "Yes")
|
||||
]);
|
||||
var second = ImportedFieldsTable.FormatSection(
|
||||
[
|
||||
new ImportedField("application", "Yes"),
|
||||
new ImportedField("Teacher Rec 1", "Fuqua")
|
||||
]);
|
||||
|
||||
var names = ImportedFieldsTable.DistinctFieldNames([first, second, null, ""]);
|
||||
|
||||
Assert.That(names, Is.EqualTo(new[] { "Application", "Interview Time", "Teacher Rec 1" }));
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,9 @@
|
||||
using System.Text;
|
||||
using Core.Entities;
|
||||
using Core.Models;
|
||||
using Core.Parsers;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Parsers;
|
||||
|
||||
public class AssignmentRequirement_Tests
|
||||
@@ -16,4 +22,53 @@ public class AssignmentRequirement_Tests
|
||||
|
||||
Assert.Pass();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_MatchesNicknameColumnsForTwoJosiahs()
|
||||
{
|
||||
StudentBuilder.ResetIdCounter();
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var josiahB = StudentBuilder.Create("Josiah", "Green").WithNickname("Josiah B").Build();
|
||||
var coding = EventDefinitionBuilder.Individual("Coding").WithShortName("Coding").Build();
|
||||
|
||||
const string csv = """
|
||||
,Jo,josiah b
|
||||
Coding,i,x
|
||||
""";
|
||||
|
||||
var requirements = ParseCsv(csv, [coding], [jo, josiahB]);
|
||||
|
||||
Assert.That(requirements, Has.Length.EqualTo(2));
|
||||
Assert.That(requirements.Single(r => r.Student.Id == jo.Id).Requirement, Is.EqualTo(Requirement.Include));
|
||||
Assert.That(requirements.Single(r => r.Student.Id == josiahB.Id).Requirement, Is.EqualTo(Requirement.Exclude));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_SharedLegalFirstName_TakesFirstStudent()
|
||||
{
|
||||
StudentBuilder.ResetIdCounter();
|
||||
var first = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var second = StudentBuilder.Create("Josiah", "Green").WithNickname("Josiah B").Build();
|
||||
var coding = EventDefinitionBuilder.Individual("Coding").WithShortName("Coding").Build();
|
||||
|
||||
const string csv = """
|
||||
,Josiah
|
||||
Coding,i
|
||||
""";
|
||||
|
||||
var requirements = ParseCsv(csv, [coding], [first, second]);
|
||||
|
||||
Assert.That(requirements, Has.Length.EqualTo(1));
|
||||
Assert.That(requirements[0].Student.Id, Is.EqualTo(first.Id));
|
||||
}
|
||||
|
||||
private static AssignmentRequirement[] ParseCsv(
|
||||
string csv,
|
||||
ICollection<EventDefinition> events,
|
||||
ICollection<Student> students)
|
||||
{
|
||||
using var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csv)));
|
||||
using var parser = new AssignmentRequirementParser(reader);
|
||||
return parser.Parse(events, students);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Core.Parsers;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Parsers;
|
||||
|
||||
[TestFixture]
|
||||
public class FuzzyStudentMatcher_Tests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp() => StudentBuilder.ResetIdCounter();
|
||||
|
||||
[Test]
|
||||
public void Find_MatchesNicknameAmongTwoJosiahs()
|
||||
{
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var josiahB = StudentBuilder.Create("Josiah", "Green").WithNickname("Josiah B").Build();
|
||||
|
||||
var match = FuzzyStudentMatcher.Find([jo, josiahB], "Jo");
|
||||
|
||||
Assert.That(match, Is.Not.Null);
|
||||
Assert.That(match!.Value.Student.Id, Is.EqualTo(jo.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Score_LegalNameStillMatchesWhenNicknameSet()
|
||||
{
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
|
||||
Assert.That(FuzzyStudentMatcher.Score(jo, "Josiah Brown"), Is.GreaterThanOrEqualTo(FuzzyStudentMatcher.MatchThreshold));
|
||||
Assert.That(FuzzyStudentMatcher.Score(jo, "Brown, Josiah"), Is.GreaterThanOrEqualTo(FuzzyStudentMatcher.MatchThreshold));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Score_NullNicknameDoesNotThrow()
|
||||
{
|
||||
var student = StudentBuilder.Create("Aria", "Cole").Build();
|
||||
|
||||
Assert.That(FuzzyStudentMatcher.Score(student, "Aria Cole"), Is.GreaterThanOrEqualTo(FuzzyStudentMatcher.MatchThreshold));
|
||||
Assert.That(FuzzyStudentMatcher.Find([student], "Nobody"), Is.Null);
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,21 @@ public class StudentEventRankingParser_Tests
|
||||
Assert.That(result.Matches[0].Ranking.Student, Is.SameAs(_aria));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_NicknamePlusLastName_MatchesStudent()
|
||||
{
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
|
||||
var result = ParseCsv("""
|
||||
Student Name,1
|
||||
Jo Brown,Coding
|
||||
""", [jo], _events);
|
||||
|
||||
Assert.That(result.IsSuccess, Is.True);
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Matches[0].Ranking.Student, Is.SameAs(jo));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ShortName_MatchesEvent()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using Core.Parsers;
|
||||
|
||||
namespace Tests.Parsers;
|
||||
|
||||
[TestFixture]
|
||||
public class StudentImportCsvTemplate_Tests
|
||||
{
|
||||
[Test]
|
||||
public void Build_IncludesRosterAndDefaultLeftoverColumns()
|
||||
{
|
||||
var csv = StudentImportCsvTemplate.Build();
|
||||
var header = csv.Split('\n')[0].TrimEnd('\r');
|
||||
|
||||
Assert.That(header, Does.StartWith("Student Name,Grade,TSA year,State ID,Regional ID,National ID"));
|
||||
Assert.That(header, Does.Contain("Interview Time"));
|
||||
Assert.That(header, Does.Contain("Application"));
|
||||
Assert.That(csv, Does.Contain("\"Last, First\""));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Build_UsesConfiguredLeftoverFields_AndSkipsReserved()
|
||||
{
|
||||
var csv = StudentImportCsvTemplate.Build(["Teacher Rec 1", "Grade", " Application "]);
|
||||
var header = csv.Split('\n')[0].TrimEnd('\r');
|
||||
|
||||
Assert.That(header, Does.Contain("Teacher Rec 1"));
|
||||
Assert.That(header, Does.Contain("Application"));
|
||||
Assert.That(header.Split(',').Count(c => c.Equals("Grade", StringComparison.OrdinalIgnoreCase)), Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
using System.Text;
|
||||
using Core.Notes;
|
||||
using Core.Parsers;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Parsers;
|
||||
|
||||
[TestFixture]
|
||||
public class StudentNotesFieldParser_Tests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
BuilderExtensions.ResetAllBuilders();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_MatchesStudentAndReportsUnmatched()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name,Interview Time,Application
|
||||
"Chittenden, Aria",3:20-3:35,x
|
||||
Nobody Here,1:00-1:15,x
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
|
||||
Assert.That(result.IsSuccess, Is.True);
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Matches[0].Student, Is.SameAs(aria));
|
||||
Assert.That(result.Matches[0].Merge.Changed, Is.True);
|
||||
Assert.That(result.Issues, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Issues[0].RawStudentName, Is.EqualTo("Nobody Here"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_MatchesNicknamePlusLastName()
|
||||
{
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
const string csv = """
|
||||
Student Name,Interview Time
|
||||
Jo Brown,3:20-3:35
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [jo]);
|
||||
|
||||
Assert.That(result.IsSuccess, Is.True);
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Matches[0].Student, Is.SameAs(jo));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_SameExistingNote_HasNoChanges()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
var existing = ImportedFieldsTable.Merge(null,
|
||||
[
|
||||
new ImportedField("Interview Time", "3:20-3:35"),
|
||||
new ImportedField("Application", "Yes")
|
||||
]).Markdown;
|
||||
|
||||
const string csv = """
|
||||
Student Name,Interview Time,Application
|
||||
Aria Chittenden,3:20-3:35,x
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria], new Dictionary<int, string?> { [aria.Id] = existing });
|
||||
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Matches[0].Merge.Changed, Is.False);
|
||||
Assert.That(result.StudentsWithChanges, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_MissingStudentNameColumn_IsError()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Name,Interview Time
|
||||
Aria Chittenden,3:20-3:35
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
|
||||
Assert.That(result.IsSuccess, Is.False);
|
||||
Assert.That(result.Errors, Has.Some.Contains("Student Name"));
|
||||
Assert.That(result.Matches, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_PreservesFieldsNotInCsv()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
var existing = ImportedFieldsTable.Merge(null,
|
||||
[
|
||||
new ImportedField("Interview Time", "1:00-1:15"),
|
||||
new ImportedField("Teacher Rec 1", "Fuqua")
|
||||
]).Markdown;
|
||||
|
||||
const string csv = """
|
||||
Student Name,Interview Time
|
||||
Aria Chittenden,3:20-3:35
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria], new Dictionary<int, string?> { [aria.Id] = existing });
|
||||
var fields = ImportedFieldsTable.ParseFields(result.Matches[0].Merge.Markdown);
|
||||
|
||||
Assert.That(result.Matches[0].Merge.Changed, Is.True);
|
||||
Assert.That(fields.Single(f => f.Name == "Interview Time").Value, Is.EqualTo("3:20-3:35"));
|
||||
Assert.That(fields.Single(f => f.Name == "Teacher Rec 1").Value, Is.EqualTo("Fuqua"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_X_BecomesYes_AndBlankFieldsRoundTrip()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name,Application,Teacher Rec 3
|
||||
Aria Chittenden,x,
|
||||
""";
|
||||
|
||||
var first = Parse(csv, [aria]);
|
||||
var markdown = first.Matches[0].Merge.Markdown;
|
||||
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Application"), Is.EqualTo("Yes"));
|
||||
Assert.That(ImportedFieldsTable.GetFieldValue(markdown, "Teacher Rec 3"), Is.EqualTo(string.Empty));
|
||||
|
||||
var second = Parse(csv, [aria], new Dictionary<int, string?> { [aria.Id] = markdown });
|
||||
Assert.That(second.Matches[0].Merge.Changed, Is.False);
|
||||
Assert.That(second.StudentsWithChanges, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_DuplicateStudentRows_LastValueWins_OneMatch()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name,Interview Time,Teacher Rec 1
|
||||
Aria Chittenden,1:00-1:15,Fuqua
|
||||
"Chittenden, Aria",3:20-3:35,Young
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
var fields = ImportedFieldsTable.ParseFields(result.Matches[0].Merge.Markdown);
|
||||
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(fields.Single(f => f.Name == "Interview Time").Value, Is.EqualTo("3:20-3:35"));
|
||||
Assert.That(fields.Single(f => f.Name == "Teacher Rec 1").Value, Is.EqualTo("Young"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_NameOnlyCsv_WarnsAndMatchesWithoutFieldChanges()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name
|
||||
Aria Chittenden
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
|
||||
Assert.That(result.IsSuccess, Is.True);
|
||||
Assert.That(result.Warnings, Has.Some.Contains("No leftover field columns"));
|
||||
Assert.That(result.Matches, Has.Count.EqualTo(1));
|
||||
Assert.That(result.Matches[0].Merge.Changed, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_ExcludesRosterAndRankingColumns()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name,Grade,TSA year,State ID,Regional ID,National ID,Officer,1,2,TOTAL # OF EVENTS,Interview Time,Application
|
||||
Aria Chittenden,6,1st,,,,,Coding,JSS,2,3:20-3:35,x
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
var fields = ImportedFieldsTable.ParseFields(result.Matches[0].Merge.Markdown);
|
||||
|
||||
Assert.That(result.FieldNames, Is.EquivalentTo(["Interview Time", "Application"]));
|
||||
Assert.That(fields.Select(f => f.Name), Is.EquivalentTo(["Interview Time", "Application"]));
|
||||
Assert.That(fields.Single(f => f.Name == "Application").Value, Is.EqualTo("Yes"));
|
||||
Assert.That(fields.Any(f => f.Name is "Grade" or "1" or "Officer"), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_RosterOnlyCsv_HasNoLeftoverFields()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
const string csv = """
|
||||
Student Name,Grade,TSA year,State ID,Regional ID,National ID
|
||||
Aria Chittenden,6,1st,,,
|
||||
""";
|
||||
|
||||
var result = Parse(csv, [aria]);
|
||||
|
||||
Assert.That(result.FieldNames, Is.Empty);
|
||||
Assert.That(result.Matches[0].Merge.Changed, Is.False);
|
||||
Assert.That(result.Warnings, Has.Some.Contains("No leftover field columns"));
|
||||
}
|
||||
|
||||
private static Core.Models.StudentNotesImportResult Parse(
|
||||
string csv,
|
||||
ICollection<Core.Entities.Student> students,
|
||||
IReadOnlyDictionary<int, string?>? notes = null)
|
||||
{
|
||||
using var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csv)));
|
||||
using var parser = new StudentNotesFieldParser(reader);
|
||||
return parser.Parse(students, notes ?? new Dictionary<int, string?>());
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
using System.Text;
|
||||
using Core.Parsers;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Parsers;
|
||||
|
||||
public class TeamParser_Tests
|
||||
@@ -37,4 +41,26 @@ public class TeamParser_Tests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Parse_MatchesNicknameAndLegalStudentColumns()
|
||||
{
|
||||
StudentBuilder.ResetIdCounter();
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var josiahB = StudentBuilder.Create("Josiah", "Green").WithNickname("Josiah B").Build();
|
||||
var coding = EventDefinitionBuilder.Team("Coding", 2, 2).Build();
|
||||
|
||||
const string csv = """
|
||||
Team Name,Event Name,Regional Time Slot,Student 1,Student 2
|
||||
A,Coding,,Jo,Josiah Green
|
||||
""";
|
||||
|
||||
using var reader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(csv)));
|
||||
using var parser = new TeamParser(reader);
|
||||
var teams = parser.Parse([coding], [jo, josiahB]);
|
||||
|
||||
Assert.That(teams, Has.Length.EqualTo(1));
|
||||
Assert.That(teams[0].Students.Select(s => s.Id), Is.EquivalentTo(new[] { jo.Id, josiahB.Id }));
|
||||
Assert.That(teams[0].Captain!.Id, Is.EqualTo(jo.Id));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using Core.Printing;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class MarkdownTableStencil_Tests
|
||||
{
|
||||
private const string Roster = """
|
||||
| Name | Grade |
|
||||
| --- | --- |
|
||||
| {{LastNameFirstName}} | {{Grade}} |
|
||||
""";
|
||||
|
||||
[Test]
|
||||
public void TryParse_SingleTable_SplitsPrefixHeaderBodySuffix()
|
||||
{
|
||||
var template = """
|
||||
# Rankings
|
||||
|
||||
| Name | 1st |
|
||||
| --- | --- |
|
||||
| {{LastNameFirstName}} | {{Rank1}} |
|
||||
|
||||
{{Legend}}
|
||||
""";
|
||||
|
||||
Assert.That(MarkdownTableStencil.TryParse(template, out var stencil), Is.True);
|
||||
Assert.That(stencil!.Prefix, Does.Contain("# Rankings"));
|
||||
Assert.That(stencil.Header, Does.Contain("| Name | 1st |"));
|
||||
Assert.That(stencil.Header, Does.Contain("| --- | --- |"));
|
||||
Assert.That(stencil.Body.Trim(), Is.EqualTo("| {{LastNameFirstName}} | {{Rank1}} |"));
|
||||
Assert.That(stencil.Suffix, Does.Contain("{{Legend}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryParse_NoTable_Fails()
|
||||
{
|
||||
Assert.That(MarkdownTableStencil.TryParse("# Interview\n\n{{FirstName}}", out _), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryParse_TwoTables_Fails()
|
||||
{
|
||||
var template = """
|
||||
| Grade | Time |
|
||||
| --- | --- |
|
||||
| {{Grade}} | {{Interview Time}} |
|
||||
|
||||
| 1st | 2nd |
|
||||
| --- | --- |
|
||||
| {{Rank1}} | {{Rank2}} |
|
||||
""";
|
||||
|
||||
Assert.That(MarkdownTableStencil.TryParse(template, out _), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryParse_HeaderOnly_Fails()
|
||||
{
|
||||
Assert.That(MarkdownTableStencil.TryParse("| Name |\n| --- |\n", out _), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Stitch_TwoStudents_HeaderOnceAndTwoBodyRows()
|
||||
{
|
||||
Assert.That(MarkdownTableStencil.TryParse(Roster, out var stencil), Is.True);
|
||||
|
||||
var merged = stencil!.Stitch(
|
||||
string.Empty,
|
||||
[
|
||||
NoteTemplateMerger.Merge(stencil.Body, Map(("LastNameFirstName", "Cole, Aria"), ("Grade", "6"))),
|
||||
NoteTemplateMerger.Merge(stencil.Body, Map(("LastNameFirstName", "Dean, Lucas"), ("Grade", "7")))
|
||||
],
|
||||
string.Empty);
|
||||
|
||||
Assert.That(CountOccurrences(merged, "| Name | Grade |"), Is.EqualTo(1));
|
||||
Assert.That(CountOccurrences(merged, "| --- | --- |"), Is.EqualTo(1));
|
||||
Assert.That(merged, Does.Contain("| Cole, Aria | 6 |"));
|
||||
Assert.That(merged, Does.Contain("| Dean, Lucas | 7 |"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Stitch_AttributesRow_RepeatsPerRecord()
|
||||
{
|
||||
var template = """
|
||||
| 1st | 2nd |
|
||||
| --- | --- |
|
||||
| {{Rank1}} | {{Rank2}} |
|
||||
| {{Rank1.Attributes}} | {{Rank2.Attributes}} |
|
||||
""";
|
||||
|
||||
Assert.That(MarkdownTableStencil.TryParse(template, out var stencil), Is.True);
|
||||
|
||||
var merged = stencil!.Stitch(
|
||||
string.Empty,
|
||||
[
|
||||
NoteTemplateMerger.Merge(stencil.Body, Map(("Rank1", "Coding"), ("Rank2", "Flight"), ("Rank1.Attributes", "I"), ("Rank2.Attributes", "T"))),
|
||||
NoteTemplateMerger.Merge(stencil.Body, Map(("Rank1", "Drone"), ("Rank2", "Robotics"), ("Rank1.Attributes", "R"), ("Rank2.Attributes", "O")))
|
||||
],
|
||||
string.Empty);
|
||||
|
||||
Assert.That(CountOccurrences(merged, "| Coding | Flight |"), Is.EqualTo(1));
|
||||
Assert.That(CountOccurrences(merged, "| I | T |"), Is.EqualTo(1));
|
||||
Assert.That(CountOccurrences(merged, "| Drone | Robotics |"), Is.EqualTo(1));
|
||||
Assert.That(CountOccurrences(merged, "| R | O |"), Is.EqualTo(1));
|
||||
Assert.That(CountOccurrences(merged, "| --- | --- |"), Is.EqualTo(1));
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> Map(params (string Key, string Value)[] pairs)
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
foreach (var (key, value) in pairs)
|
||||
tokens[key] = value;
|
||||
return tokens;
|
||||
}
|
||||
|
||||
private static int CountOccurrences(string haystack, string needle)
|
||||
{
|
||||
var count = 0;
|
||||
var index = 0;
|
||||
while ((index = haystack.IndexOf(needle, index, StringComparison.Ordinal)) >= 0)
|
||||
{
|
||||
count++;
|
||||
index += needle.Length;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
using Core.Printing;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class NoteTemplateMerger_Tests
|
||||
{
|
||||
[Test]
|
||||
public void Merge_ReplacesKnownTokens()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["FirstName"] = "Aria";
|
||||
tokens["LastName"] = "Cole";
|
||||
|
||||
var result = NoteTemplateMerger.Merge("Hello {{FirstName}} {{LastName}}", tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Hello Aria Cole"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_FirstNameStaysLegalWhenDisplayFirstNameIsNickname()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["FirstName"] = "Josiah";
|
||||
tokens["Nickname"] = "Jo";
|
||||
tokens["DisplayFirstName"] = "Jo";
|
||||
tokens["LastName"] = "Brown";
|
||||
|
||||
var result = NoteTemplateMerger.Merge(
|
||||
"{{FirstName}} {{LastName}} / {{DisplayFirstName}} ({{Nickname}})",
|
||||
tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Josiah Brown / Jo (Jo)"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_SupportsDottedAndSpacedNames()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["Chapter.Name"] = "North TSA";
|
||||
tokens["Interview Time"] = "3:20-3:35";
|
||||
|
||||
var result = NoteTemplateMerger.Merge(
|
||||
"{{Chapter.Name}} at {{Interview Time}}",
|
||||
tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("North TSA at 3:20-3:35"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_IsCaseAndWhitespaceTolerant()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["FirstName"] = "Aria";
|
||||
|
||||
var result = NoteTemplateMerger.Merge("{{ firstname }}", tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Aria"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_LeavesUnknownTokensInPlace()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["FirstName"] = "Aria";
|
||||
|
||||
var result = NoteTemplateMerger.Merge("{{FirstName}} {{Missing}}", tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Aria {{Missing}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_KnownEmptyBecomesBlank()
|
||||
{
|
||||
var tokens = PrintTokenMap.Create();
|
||||
tokens["Interview Time"] = string.Empty;
|
||||
|
||||
var result = NoteTemplateMerger.Merge("Time: {{Interview Time}}.", tokens);
|
||||
|
||||
Assert.That(result, Is.EqualTo("Time: ."));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_EmptyTemplate_IsEmpty()
|
||||
{
|
||||
Assert.That(NoteTemplateMerger.Merge(null, PrintTokenMap.Create()), Is.EqualTo(string.Empty));
|
||||
Assert.That(NoteTemplateMerger.Merge("", PrintTokenMap.Create()), Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_PageBreak_IsSentinel()
|
||||
{
|
||||
var result = NoteTemplateMerger.Merge(
|
||||
"Above\n{{PageBreak}}\nBelow",
|
||||
PrintTokenMap.Create());
|
||||
|
||||
Assert.That(result, Does.Contain(NoteTemplateMerger.PageBreakSentinel));
|
||||
Assert.That(result, Does.Not.Contain("{{PageBreak}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ApplyLayout_ReplacesSentinelAndParagraphWrap()
|
||||
{
|
||||
var raw = NoteTemplateMerger.ApplyLayout(
|
||||
$"x{NoteTemplateMerger.PageBreakSentinel}y");
|
||||
var wrapped = NoteTemplateMerger.ApplyLayout(
|
||||
$"<p>{NoteTemplateMerger.PageBreakSentinel}</p>");
|
||||
|
||||
Assert.That(raw, Is.EqualTo($"x{NoteTemplateMerger.PageBreakHtml}y"));
|
||||
Assert.That(wrapped, Is.EqualTo(NoteTemplateMerger.PageBreakHtml));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_AnswerSpace_IsSentinel()
|
||||
{
|
||||
var result = NoteTemplateMerger.Merge(
|
||||
"Q?\n{{AnswerSpace}}\nNext",
|
||||
PrintTokenMap.Create());
|
||||
|
||||
Assert.That(result, Does.Contain(NoteTemplateMerger.AnswerSpaceSentinel));
|
||||
Assert.That(result, Does.Not.Contain("{{AnswerSpace}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ApplyLayout_ReplacesAnswerSpaceSentinelAndParagraphWrap()
|
||||
{
|
||||
var raw = NoteTemplateMerger.ApplyLayout(
|
||||
$"x{NoteTemplateMerger.AnswerSpaceSentinel}y");
|
||||
var wrapped = NoteTemplateMerger.ApplyLayout(
|
||||
$"<p>{NoteTemplateMerger.AnswerSpaceSentinel}</p>");
|
||||
|
||||
Assert.That(raw, Is.EqualTo($"x{NoteTemplateMerger.AnswerSpaceHtml}y"));
|
||||
Assert.That(wrapped, Is.EqualTo(NoteTemplateMerger.AnswerSpaceHtml));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_RankedStudents_IsHtmlFragmentSentinel()
|
||||
{
|
||||
var result = NoteTemplateMerger.Merge(
|
||||
"{{Name}}\n{{RankedStudents}}",
|
||||
PrintTokenMap.Create());
|
||||
|
||||
Assert.That(result, Does.Contain(NoteTemplateMerger.HtmlFragmentSentinel(NoteTemplateMerger.RankedStudentsToken)));
|
||||
Assert.That(result, Does.Not.Contain("{{RankedStudents}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ApplyLayout_ReplacesHtmlFragmentSentinel()
|
||||
{
|
||||
var sentinel = NoteTemplateMerger.HtmlFragmentSentinel(NoteTemplateMerger.RankedStudentsToken);
|
||||
var fragments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
[NoteTemplateMerger.RankedStudentsToken] = "<div class=\"print-rank-badges\">Aria</div>"
|
||||
};
|
||||
|
||||
var raw = NoteTemplateMerger.ApplyLayout($"x{sentinel}y", fragments);
|
||||
var wrapped = NoteTemplateMerger.ApplyLayout($"<p>{sentinel}</p>", fragments);
|
||||
var missing = NoteTemplateMerger.ApplyLayout($"x{sentinel}y");
|
||||
|
||||
Assert.That(raw, Is.EqualTo("x<div class=\"print-rank-badges\">Aria</div>y"));
|
||||
Assert.That(wrapped, Is.EqualTo("<div class=\"print-rank-badges\">Aria</div>"));
|
||||
Assert.That(missing, Is.EqualTo("xy"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_Legend_IsSentinel()
|
||||
{
|
||||
var result = NoteTemplateMerger.Merge("{{Legend}}", PrintTokenMap.Create());
|
||||
|
||||
Assert.That(result, Does.Contain(NoteTemplateMerger.LegendSentinel));
|
||||
Assert.That(result, Does.Not.Contain("{{Legend}}"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ApplyLayout_ReplacesLegendSentinel()
|
||||
{
|
||||
var html = NoteTemplateMerger.ApplyLayout(
|
||||
$"<p>{NoteTemplateMerger.LegendSentinel}</p>");
|
||||
|
||||
Assert.That(html, Does.Contain("print-badge-legend"));
|
||||
Assert.That(html, Does.Contain(EventAttributeMarks.Individual));
|
||||
Assert.That(html, Does.Not.Contain("event-rank-1"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Core.Entities;
|
||||
using Core.Printing;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class PrintFieldCatalog_Tests
|
||||
{
|
||||
[Test]
|
||||
public void BuiltInFor_StudentIncludesChapterAndStudentTokens()
|
||||
{
|
||||
var tokens = PrintFieldCatalog.BuiltInFor(PrintEntityType.Student);
|
||||
|
||||
Assert.That(tokens, Does.Contain("FirstName"));
|
||||
Assert.That(tokens, Does.Contain("Chapter.ShortName"));
|
||||
Assert.That(tokens, Does.Contain("Rank1"));
|
||||
Assert.That(tokens, Does.Contain("Rank10.ShortName"));
|
||||
Assert.That(tokens, Does.Contain("Rank1.Attributes"));
|
||||
Assert.That(tokens, Does.Contain(NoteTemplateMerger.RankedEventsToken));
|
||||
Assert.That(tokens, Does.Not.Contain("Rank11"));
|
||||
Assert.That(tokens, Does.Not.Contain("Interview Time"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StudentRanks_MatchesMaxRankAndStaysOffEntityTokens()
|
||||
{
|
||||
Assert.That(PrintFieldCatalog.StudentRanks, Does.Contain("Rank1"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks, Does.Contain("Rank1.ShortName"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks, Does.Contain("Rank1.Attributes"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks, Does.Contain(NoteTemplateMerger.RankedEventsToken));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks, Has.Length.EqualTo(1 + StudentEventRanking.MaxRank * 3));
|
||||
Assert.That(PrintFieldCatalog.EntityTokens(PrintEntityType.Student), Does.Not.Contain("Rank1"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Not.Contain("Rank1"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks1To6, Does.Contain("Rank1"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks1To6, Does.Contain("Rank6.Attributes"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks1To6, Does.Not.Contain("Rank7"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks7To10, Does.Contain("Rank7"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks7To10, Does.Contain("Rank10.ShortName"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks7To10, Does.Not.Contain("Rank6"));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks1To6, Has.Length.EqualTo(6 * 3));
|
||||
Assert.That(PrintFieldCatalog.StudentRanks7To10, Has.Length.EqualTo(4 * 3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BuiltInFor_TeamAndEventHaveExpectedNames()
|
||||
{
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("EventName"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("EventAttributes"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("NationalEligibility"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("Eligibility"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("RegionalTeamCount"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Team), Does.Contain("StateTeamCount"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("RegionalEvent"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("NationalEligibility"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("Eligibility"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("RegionalTeamCount"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("StateTeamCount"));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain(NoteTemplateMerger.RankedStudentsToken));
|
||||
Assert.That(PrintFieldCatalog.BuiltInFor(PrintEntityType.Event), Does.Contain("EventAttributes"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Layout_IncludesPageBreakAndAnswerSpace()
|
||||
{
|
||||
Assert.That(PrintFieldCatalog.Layout, Does.Contain(NoteTemplateMerger.PageBreakToken));
|
||||
Assert.That(PrintFieldCatalog.Layout, Does.Contain(NoteTemplateMerger.AnswerSpaceToken));
|
||||
Assert.That(PrintFieldCatalog.Layout, Does.Contain(NoteTemplateMerger.LegendToken));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EntityTokens_MatchesCatalogArrays()
|
||||
{
|
||||
Assert.That(PrintFieldCatalog.EntityTokens(PrintEntityType.Student), Is.EqualTo(PrintFieldCatalog.Student));
|
||||
Assert.That(PrintFieldCatalog.EntityTokens(PrintEntityType.Team), Is.EqualTo(PrintFieldCatalog.Team));
|
||||
Assert.That(PrintFieldCatalog.EntityTokens(PrintEntityType.Event), Is.EqualTo(PrintFieldCatalog.Event));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Core.Entities;
|
||||
using Core.Printing;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class PrintPresetFilters_Tests
|
||||
{
|
||||
[Test]
|
||||
public void JsonRoundTrip_PreservesStudentAndImportedFilters()
|
||||
{
|
||||
var filters = new PrintPresetFilters
|
||||
{
|
||||
Grade = 9,
|
||||
TsaYear = 1,
|
||||
IsOfficer = true,
|
||||
RegionalOnly = true,
|
||||
EventFormat = EventFormat.Team,
|
||||
NewPagePerRecord = false,
|
||||
FontSizePt = 14,
|
||||
AnswerSpaceLines = 4
|
||||
};
|
||||
|
||||
var restored = PrintPresetFilters.FromJson(filters.ToJson());
|
||||
|
||||
Assert.That(restored.Grade, Is.EqualTo(9));
|
||||
Assert.That(restored.TsaYear, Is.EqualTo(1));
|
||||
Assert.That(restored.IsOfficer, Is.True);
|
||||
Assert.That(restored.RegionalOnly, Is.True);
|
||||
Assert.That(restored.EventFormat, Is.EqualTo(EventFormat.Team));
|
||||
Assert.That(restored.NewPagePerRecord, Is.False);
|
||||
Assert.That(restored.FontSizePt, Is.EqualTo(14));
|
||||
Assert.That(restored.AnswerSpaceLines, Is.EqualTo(4));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromJson_EmptyOrInvalid_ReturnsDefaults()
|
||||
{
|
||||
Assert.That(PrintPresetFilters.FromJson(null).TsaYear, Is.Null);
|
||||
Assert.That(PrintPresetFilters.FromJson("{}").Grade, Is.Null);
|
||||
Assert.That(PrintPresetFilters.FromJson("{}").NewPagePerRecord, Is.True);
|
||||
Assert.That(PrintPresetFilters.FromJson("{}").FontSizePt, Is.EqualTo(PrintPresetFilters.DefaultFontSizePt));
|
||||
Assert.That(PrintPresetFilters.FromJson("{}").AnswerSpaceLines, Is.EqualTo(PrintPresetFilters.DefaultAnswerSpaceLines));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromJson_ClampsPrintOptions()
|
||||
{
|
||||
var restored = PrintPresetFilters.FromJson("""{"fontSizePt":99,"answerSpaceLines":0}""");
|
||||
|
||||
Assert.That(restored.FontSizePt, Is.EqualTo(PrintPresetFilters.MaxFontSizePt));
|
||||
Assert.That(restored.AnswerSpaceLines, Is.EqualTo(PrintPresetFilters.MinAnswerSpaceLines));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using Core.Entities;
|
||||
using Core.Printing;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class PrintRankBadgeHtml_Tests
|
||||
{
|
||||
[Test]
|
||||
public void ForStudentEvents_Empty_IsEmpty()
|
||||
{
|
||||
Assert.That(PrintRankBadgeHtml.ForStudentEvents([]), Is.EqualTo(string.Empty));
|
||||
Assert.That(PrintRankBadgeHtml.ForStudentEvents(null), Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForStudentEvents_RendersShortNameDotAndAttributes()
|
||||
{
|
||||
var coding = EventDefinitionBuilder.Individual("Coding")
|
||||
.WithShortName("Code")
|
||||
.AsRegionalEvent()
|
||||
.Build();
|
||||
var student = StudentBuilder.Create("Aria", "Cole")
|
||||
.WithRanking(coding, 1)
|
||||
.Build();
|
||||
|
||||
var html = PrintRankBadgeHtml.ForStudentEvents(student.EventRankings);
|
||||
|
||||
Assert.That(html, Does.Contain("print-rank-badges"));
|
||||
Assert.That(html, Does.Contain("event-rank-1"));
|
||||
Assert.That(html, Does.Contain("Code"));
|
||||
Assert.That(html, Does.Contain(EventAttributeMarks.Individual));
|
||||
Assert.That(html, Does.Contain(EventAttributeMarks.Regional));
|
||||
Assert.That(html, Does.Not.Contain("{{"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForEventStudents_SortsByRankThenSeniority()
|
||||
{
|
||||
var evt = EventDefinitionBuilder.Individual("Coding").Build();
|
||||
var younger = StudentBuilder.Create("Bea", "Young").Build();
|
||||
younger.Grade = 9;
|
||||
younger.TsaYear = 1;
|
||||
var older = StudentBuilder.Create("Aria", "Cole").Build();
|
||||
older.Grade = 12;
|
||||
older.TsaYear = 4;
|
||||
|
||||
var rankings = new List<StudentEventRanking>
|
||||
{
|
||||
new() { Student = younger, EventDefinition = evt, Rank = 1 },
|
||||
new() { Student = older, EventDefinition = evt, Rank = 1 }
|
||||
};
|
||||
|
||||
var html = PrintRankBadgeHtml.ForEventStudents(rankings);
|
||||
var ariaAt = html.IndexOf("Aria", StringComparison.Ordinal);
|
||||
var beaAt = html.IndexOf("Bea", StringComparison.Ordinal);
|
||||
|
||||
Assert.That(ariaAt, Is.GreaterThanOrEqualTo(0));
|
||||
Assert.That(beaAt, Is.GreaterThan(ariaAt));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EventAttributeMarks_IncludesEffortAndFlags()
|
||||
{
|
||||
var evt = EventDefinitionBuilder.Individual("Coding")
|
||||
.AsOnSite()
|
||||
.AsRegionalEvent()
|
||||
.WithPresubmission()
|
||||
.WithLevelOfEffort(2)
|
||||
.Build();
|
||||
|
||||
var marks = EventAttributeMarks.For(evt);
|
||||
|
||||
Assert.That(marks, Does.Contain(EventAttributeMarks.LevelOfEffort2));
|
||||
Assert.That(marks, Does.Contain(EventAttributeMarks.Individual));
|
||||
Assert.That(marks, Does.Contain(EventAttributeMarks.OnSite));
|
||||
Assert.That(marks, Does.Contain(EventAttributeMarks.Regional));
|
||||
Assert.That(marks, Does.Contain(EventAttributeMarks.Presubmission));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EscapeHtml_MasksTags()
|
||||
{
|
||||
Assert.That(PrintTokenMap.EscapeHtml("A <b>"), Is.EqualTo("A <b>"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void For_UsesTheSameMarksAsTheLegend()
|
||||
{
|
||||
var evt = EventDefinitionBuilder.Individual("Coding")
|
||||
.AsOnSite()
|
||||
.AsRegionalEvent()
|
||||
.WithPresubmission()
|
||||
.WithLevelOfEffort(2)
|
||||
.Build();
|
||||
|
||||
var fromCatalog = string.Join(
|
||||
" ",
|
||||
EventAttributeMarks.LegendItems.Where(m => m.Applies(evt)).Select(m => m.Symbol));
|
||||
|
||||
Assert.That(EventAttributeMarks.For(evt), Is.EqualTo(fromCatalog));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Legend_IncludesRankDotsAndAttributeMarks()
|
||||
{
|
||||
var html = PrintRankBadgeHtml.Legend();
|
||||
|
||||
Assert.That(html, Does.Contain("print-badge-legend"));
|
||||
Assert.That(html, Does.Contain("·"));
|
||||
Assert.That(html, Does.Not.Contain(EventRankLegend.Ordinal(1)));
|
||||
Assert.That(html, Does.Not.Contain("event-rank-1"));
|
||||
foreach (var mark in EventAttributeMarks.LegendItems)
|
||||
{
|
||||
Assert.That(html, Does.Contain(mark.Symbol));
|
||||
Assert.That(html, Does.Contain(mark.Label));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using Core.Printing;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class PrintTokenMap_Tests
|
||||
{
|
||||
[Test]
|
||||
public void Build_BuiltInWinsOverImportedSameName()
|
||||
{
|
||||
var map = PrintTokenMap.Build(
|
||||
new Dictionary<string, string?> { ["Grade"] = "imported" },
|
||||
new Dictionary<string, string?> { ["Grade"] = "9" },
|
||||
null);
|
||||
|
||||
Assert.That(map["Grade"], Is.EqualTo("9"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Build_BuiltInRankTokenWinsOverImportedSameName()
|
||||
{
|
||||
var map = PrintTokenMap.Build(
|
||||
new Dictionary<string, string?> { ["Rank1"] = "imported" },
|
||||
new Dictionary<string, string?> { ["Rank1"] = "Coding" },
|
||||
null);
|
||||
|
||||
Assert.That(map["Rank1"], Is.EqualTo("Coding"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Build_IncludesAllImportedCatalogKeys()
|
||||
{
|
||||
var imported = new Dictionary<string, string?>
|
||||
{
|
||||
["Interview Time"] = "3:20-3:35",
|
||||
["Application"] = null
|
||||
};
|
||||
|
||||
var map = PrintTokenMap.Build(imported, null, null);
|
||||
|
||||
Assert.That(map.ContainsKey("Interview Time"), Is.True);
|
||||
Assert.That(map["Interview Time"], Is.EqualTo("3:20-3:35"));
|
||||
Assert.That(map.ContainsKey("Application"), Is.True);
|
||||
Assert.That(map["Application"], Is.EqualTo(string.Empty));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Escape_MasksMarkdownAndHtml()
|
||||
{
|
||||
var escaped = PrintTokenMap.Escape("A *B* <script>");
|
||||
|
||||
Assert.That(escaped, Does.Contain("\\*"));
|
||||
Assert.That(escaped, Does.Contain("<"));
|
||||
Assert.That(escaped, Does.Contain(">"));
|
||||
Assert.That(escaped, Does.Not.Contain("<script>"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_EscapedAsteriskDoesNotStayRaw()
|
||||
{
|
||||
var map = PrintTokenMap.Build(
|
||||
null,
|
||||
new Dictionary<string, string?> { ["FirstName"] = "A*ria" },
|
||||
null);
|
||||
|
||||
var merged = NoteTemplateMerger.Merge("Hi {{FirstName}}", map);
|
||||
|
||||
Assert.That(merged, Is.EqualTo("Hi A\\*ria"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Escape_FlattensNewlinesSoTableRowsStayIntact()
|
||||
{
|
||||
var escaped = PrintTokenMap.Escape("Drone Challenge (UAV)\r\n\r\n");
|
||||
|
||||
Assert.That(escaped, Is.EqualTo("Drone Challenge (UAV)"));
|
||||
Assert.That(escaped, Does.Not.Contain('\n'));
|
||||
Assert.That(escaped, Does.Not.Contain('\r'));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Escape_EscapesPipeForMarkdownTables()
|
||||
{
|
||||
Assert.That(PrintTokenMap.Escape("A | B"), Is.EqualTo("A \\| B"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_DroneNameWithTrailingNewlines_StaysOnOneTableRow()
|
||||
{
|
||||
var map = PrintTokenMap.Build(
|
||||
null,
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
["Rank1"] = "Drone Challenge (UAV)\n\n",
|
||||
["Rank2"] = "Off the Grid"
|
||||
},
|
||||
null);
|
||||
|
||||
var merged = NoteTemplateMerger.Merge(
|
||||
"| {{Rank1}} | {{Rank2}} |\n| --- | --- |",
|
||||
map);
|
||||
|
||||
Assert.That(merged, Is.EqualTo("| Drone Challenge (UAV) | Off the Grid |\n| --- | --- |"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using Core.Entities;
|
||||
using Core.Printing;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Printing;
|
||||
|
||||
[TestFixture]
|
||||
public class StudentRankTokens_Tests
|
||||
{
|
||||
[Test]
|
||||
public void FromRankings_AlwaysIncludesEveryRankThroughMax()
|
||||
{
|
||||
var map = StudentRankTokens.FromRankings([]);
|
||||
|
||||
for (var rank = 1; rank <= StudentEventRanking.MaxRank; rank++)
|
||||
{
|
||||
Assert.That(map.ContainsKey(StudentRankTokens.NameToken(rank)), Is.True);
|
||||
Assert.That(map.ContainsKey(StudentRankTokens.ShortNameToken(rank)), Is.True);
|
||||
Assert.That(map[StudentRankTokens.NameToken(rank)], Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromRankings_FillsNameAndShortNameForPresentRanks()
|
||||
{
|
||||
var coding = EventDefinitionBuilder.Individual("Coding")
|
||||
.WithShortName("Code")
|
||||
.Build();
|
||||
var flight = EventDefinitionBuilder.Individual("Flight Endurance")
|
||||
.WithShortName("Flight")
|
||||
.Build();
|
||||
var student = StudentBuilder.Create("Aria", "Cole")
|
||||
.WithRanking(coding, 1)
|
||||
.WithRanking(flight, 3)
|
||||
.Build();
|
||||
|
||||
var map = StudentRankTokens.FromRankings(student.EventRankings);
|
||||
|
||||
Assert.That(map["Rank1"], Is.EqualTo("Coding"));
|
||||
Assert.That(map["Rank1.ShortName"], Is.EqualTo("Code"));
|
||||
Assert.That(map["Rank1.Attributes"], Is.EqualTo(EventAttributeMarks.For(coding)));
|
||||
Assert.That(map["Rank3"], Is.EqualTo("Flight Endurance"));
|
||||
Assert.That(map["Rank3.ShortName"], Is.EqualTo("Flight"));
|
||||
Assert.That(map["Rank2"], Is.Null);
|
||||
Assert.That(map["Rank2.Attributes"], Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FromRankings_IgnoresRanksOutsideOneToMax()
|
||||
{
|
||||
var evt = EventDefinitionBuilder.Individual("Coding").Build();
|
||||
var rankings = new List<StudentEventRanking>
|
||||
{
|
||||
new() { EventDefinition = evt, Rank = 0 },
|
||||
new() { EventDefinition = evt, Rank = StudentEventRanking.MaxRank + 1 }
|
||||
};
|
||||
|
||||
var map = StudentRankTokens.FromRankings(rankings);
|
||||
|
||||
Assert.That(map["Rank1"], Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_KnownEmptyRankPrintsBlank()
|
||||
{
|
||||
var map = PrintTokenMap.Build(null, StudentRankTokens.FromRankings([]), null);
|
||||
|
||||
var merged = NoteTemplateMerger.Merge("1. {{Rank1}}", map);
|
||||
|
||||
Assert.That(merged, Is.EqualTo("1. "));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Merge_ReplacesRankTokens()
|
||||
{
|
||||
var evt = EventDefinitionBuilder.Individual("Video Game Design")
|
||||
.WithShortName("VGD")
|
||||
.Build();
|
||||
var student = StudentBuilder.Create("Aria", "Cole")
|
||||
.WithRanking(evt, 1)
|
||||
.Build();
|
||||
var map = PrintTokenMap.Build(null, StudentRankTokens.FromRankings(student.EventRankings), null);
|
||||
|
||||
var merged = NoteTemplateMerger.Merge("{{Rank1}} ({{Rank1.ShortName}})", map);
|
||||
|
||||
Assert.That(merged, Is.EqualTo("Video Game Design (VGD)"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Core.Services;
|
||||
|
||||
namespace Tests.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class NoteNamingService_Tests
|
||||
{
|
||||
private readonly NoteNamingService _service = new();
|
||||
|
||||
[Test]
|
||||
public void GetStudentNoteTitle_UsesStablePrefix()
|
||||
{
|
||||
Assert.That(_service.GetStudentNoteTitle(42), Is.EqualTo("#Student:42"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsStudentNote_AndParseId()
|
||||
{
|
||||
Assert.That(_service.IsStudentNote("#Student:12"), Is.True);
|
||||
Assert.That(_service.IsStudentNote("#Students"), Is.False);
|
||||
Assert.That(_service.TryParseStudentNoteId("#Student:12", out var id), Is.True);
|
||||
Assert.That(id, Is.EqualTo(12));
|
||||
Assert.That(_service.TryParseStudentNoteId("#Event Ranking", out _), Is.False);
|
||||
Assert.That(_service.IsStudentNote(null), Is.False);
|
||||
Assert.That(_service.TryParseStudentNoteId("#Student:", out _), Is.False);
|
||||
Assert.That(_service.IsPageNote("#Student:12"), Is.True);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Core.Models;
|
||||
using Core.Notes;
|
||||
using Core.Services;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class StudentNotesImportPlan_Tests
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
BuilderExtensions.ResetAllBuilders();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Create_SkipsUnchanged_CreatesAndUpdatesChanged()
|
||||
{
|
||||
var aria = StudentBuilder.Create("Aria", "Chittenden").Build();
|
||||
var blake = StudentBuilder.Create("Blake", "Nguyen").Build();
|
||||
var casey = StudentBuilder.Create("Casey", "Ortiz").Build();
|
||||
|
||||
var firstWrite = ImportedFieldsTable.Merge(null, [new ImportedField("Application", "Yes")]);
|
||||
var unchanged = ImportedFieldsTable.Merge(firstWrite.Markdown, [new ImportedField("Application", "Yes")]);
|
||||
var created = ImportedFieldsTable.Merge(null, [new ImportedField("Interview Time", "3:20-3:35")]);
|
||||
var updated = ImportedFieldsTable.Merge(
|
||||
ImportedFieldsTable.Merge(null, [new ImportedField("Application", "Yes")]).Markdown,
|
||||
[new ImportedField("Application", "")]);
|
||||
|
||||
var parseResult = new StudentNotesImportResult
|
||||
{
|
||||
Matches =
|
||||
[
|
||||
Match(aria, unchanged),
|
||||
Match(blake, created),
|
||||
Match(casey, updated)
|
||||
]
|
||||
};
|
||||
|
||||
HashSet<int> existingNoteIds = [casey.Id];
|
||||
var actions = StudentNotesImportPlan.Create(parseResult, existingNoteIds);
|
||||
|
||||
Assert.That(actions, Has.Count.EqualTo(2));
|
||||
Assert.That(actions.Any(a => a.StudentId == aria.Id), Is.False);
|
||||
Assert.That(actions.Single(a => a.StudentId == blake.Id).Kind, Is.EqualTo(StudentNotePersistKind.Create));
|
||||
Assert.That(actions.Single(a => a.StudentId == casey.Id).Kind, Is.EqualTo(StudentNotePersistKind.Update));
|
||||
Assert.That(actions.Single(a => a.StudentId == casey.Id).Markdown, Is.EqualTo(updated.Markdown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Create_EmptyMatches_ReturnsNoActions()
|
||||
{
|
||||
var actions = StudentNotesImportPlan.Create(new StudentNotesImportResult(), new HashSet<int>());
|
||||
Assert.That(actions, Is.Empty);
|
||||
}
|
||||
|
||||
private static StudentNotesImportMatch Match(Core.Entities.Student student, ImportedFieldsMergeResult merge) =>
|
||||
new()
|
||||
{
|
||||
Student = student,
|
||||
Merge = merge
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using Core.Utility;
|
||||
using Tests.Builders;
|
||||
|
||||
namespace Tests.Utility;
|
||||
|
||||
[TestFixture]
|
||||
public class StudentNameFormatter_Tests
|
||||
{
|
||||
[Test]
|
||||
public void FormatStudentName_UsesDisplayFirstNameAndSuffixes()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
|
||||
var formatted = StudentNameFormatter.FormatStudentName(student, new StudentNameFormatter.FormatOptions
|
||||
{
|
||||
HasOverlap = true,
|
||||
IsAbsent = true
|
||||
});
|
||||
|
||||
Assert.That(formatted, Is.EqualTo("Jo* (absent)"));
|
||||
}
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class TeamStudentNameFormatter_Tests
|
||||
{
|
||||
[Test]
|
||||
public void FormatStudentName_KeepsCaptainAndOverlapMarkersOnNickname()
|
||||
{
|
||||
var captain = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var teammate = StudentBuilder.Create("Aria", "Cole").Build();
|
||||
var evt = EventDefinitionBuilder.Team("Flight Endurance", 2, 2).Build();
|
||||
var team = TeamBuilder.Create(evt)
|
||||
.WithStudent(captain, isCaptain: true)
|
||||
.WithStudent(teammate)
|
||||
.Build();
|
||||
|
||||
var formatted = TeamStudentNameFormatter.FormatStudentName(
|
||||
captain,
|
||||
team,
|
||||
new TeamStudentNameFormatter.FormatOptions
|
||||
{
|
||||
CaptainIndicator = TeamStudentNameFormatter.CaptainIndicatorStyle.Captain,
|
||||
MarkOverlaps = true,
|
||||
HasOverlaps = _ => true
|
||||
});
|
||||
|
||||
Assert.That(formatted, Is.EqualTo("Jo(Cpt)*"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FormatStudentList_AlphabeticalUsesDisplayFirstName()
|
||||
{
|
||||
var jo = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").Build();
|
||||
var josiahB = StudentBuilder.Create("Josiah", "Green").WithNickname("Josiah B").Build();
|
||||
var aria = StudentBuilder.Create("Aria", "Cole").Build();
|
||||
var evt = EventDefinitionBuilder.Team("Flight Endurance", 3, 3).Build();
|
||||
var team = TeamBuilder.Create(evt)
|
||||
.WithStudent(josiahB)
|
||||
.WithStudent(aria)
|
||||
.WithStudent(jo, isCaptain: true)
|
||||
.Build();
|
||||
|
||||
var list = TeamStudentNameFormatter.FormatStudentList(
|
||||
team,
|
||||
new TeamStudentNameFormatter.FormatOptions
|
||||
{
|
||||
Ordering = TeamStudentNameFormatter.OrderingStyle.Alphabetical
|
||||
});
|
||||
|
||||
Assert.That(list, Is.EqualTo("Aria, Jo, Josiah B"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FormatStudentName_FallsBackToFirstName()
|
||||
{
|
||||
var student = StudentBuilder.Create("Josiah", "Green").Build();
|
||||
var evt = EventDefinitionBuilder.Team("Flight Endurance", 2, 2).Build();
|
||||
var team = TeamBuilder.Create(evt).WithStudent(student).Build();
|
||||
|
||||
var formatted = TeamStudentNameFormatter.FormatStudentName(
|
||||
student,
|
||||
team,
|
||||
new TeamStudentNameFormatter.FormatOptions());
|
||||
|
||||
Assert.That(formatted, Is.EqualTo("Josiah"));
|
||||
}
|
||||
}
|
||||
@@ -196,6 +196,32 @@ public class YearTransitionPlanner_Tests
|
||||
Assert.That(result.UnmatchedNames, Is.EquivalentTo(new[] { "Nobody Here" }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchPastedNames_MatchesNicknamePlusLastName()
|
||||
{
|
||||
var josiah = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").WithGrade(7).Build();
|
||||
|
||||
var result = YearTransitionPlanner.MatchPastedNames(
|
||||
[josiah],
|
||||
["Jo Brown", "Brown, Jo"]);
|
||||
|
||||
Assert.That(result.MatchedStudentIds, Is.EquivalentTo(new[] { josiah.Id }));
|
||||
Assert.That(result.UnmatchedNames, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchPastedNames_LegalNameStillMatchesWhenNicknameSet()
|
||||
{
|
||||
var josiah = StudentBuilder.Create("Josiah", "Brown").WithNickname("Jo").WithGrade(7).Build();
|
||||
|
||||
var result = YearTransitionPlanner.MatchPastedNames(
|
||||
[josiah],
|
||||
["Josiah Brown", "Brown, Josiah"]);
|
||||
|
||||
Assert.That(result.MatchedStudentIds, Is.EquivalentTo(new[] { josiah.Id }));
|
||||
Assert.That(result.UnmatchedNames, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchPastedNames_AmbiguousWhenDuplicateNames()
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<script src="_content/PSC.Blazor.Components.MarkdownEditor/js/easymde.min.js"></script>
|
||||
<script src="_content/PSC.Blazor.Components.MarkdownEditor/js/markdownEditor.js"></script>
|
||||
<script src="js/markdownTablePaste.js"></script>
|
||||
<script src="js/downloadFile.js"></script>
|
||||
<script src="js/login.js"></script>
|
||||
</body>
|
||||
|
||||
|
||||
@@ -222,7 +222,7 @@ else
|
||||
.ThenInclude(t => t!.Captain)
|
||||
.Include(s => s.Teams)
|
||||
.ThenInclude(t => t!.Students)
|
||||
.OrderBy(s => s.FirstName)
|
||||
.OrderBy(s => s.Nickname ?? s.FirstName)
|
||||
.ThenBy(s => s.LastName)
|
||||
.ToArrayAsync();
|
||||
}
|
||||
@@ -391,15 +391,15 @@ else
|
||||
|
||||
if (isIndividual)
|
||||
{
|
||||
var ordered = students.OrderBy(s => s.FirstName, StringComparer.OrdinalIgnoreCase);
|
||||
var ordered = students.OrderBy(s => s.DisplayFirstName, StringComparer.OrdinalIgnoreCase);
|
||||
return string.Join(", ", ordered.Select(s => FormatCombinedScheduleStudentSegment(s, team, isIndividual)));
|
||||
}
|
||||
|
||||
var cap = team.Captain;
|
||||
var capInRoster = cap != null && students.Exists(s => s.Id == cap.Id);
|
||||
IEnumerable<Student> orderedTeam = capInRoster
|
||||
? students.Where(s => s.Id != cap!.Id).OrderBy(s => s.FirstName, StringComparer.OrdinalIgnoreCase).Prepend(cap!)
|
||||
: students.OrderBy(s => s.FirstName, StringComparer.OrdinalIgnoreCase);
|
||||
? students.Where(s => s.Id != cap!.Id).OrderBy(s => s.DisplayFirstName, StringComparer.OrdinalIgnoreCase).Prepend(cap!)
|
||||
: students.OrderBy(s => s.DisplayFirstName, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
return string.Join(", ", orderedTeam.Select(s => FormatCombinedScheduleStudentSegment(s, team, isIndividual)));
|
||||
}
|
||||
@@ -410,12 +410,12 @@ else
|
||||
{
|
||||
var sid = student.StateId?.Trim();
|
||||
return !string.IsNullOrEmpty(sid)
|
||||
? $"{student.FirstName} ({sid})"
|
||||
: student.FirstName;
|
||||
? $"{student.DisplayFirstName} ({sid})"
|
||||
: student.DisplayFirstName;
|
||||
}
|
||||
|
||||
var isCpt = team.Captain?.Id == student.Id;
|
||||
return isCpt ? $"{student.FirstName} (Cpt.)" : student.FirstName;
|
||||
return isCpt ? $"{student.DisplayFirstName} (Cpt.)" : student.DisplayFirstName;
|
||||
}
|
||||
|
||||
private sealed record EventSummaryRow(string StateRegistrationId, string EventName, string Activity);
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
@page "/events/import"
|
||||
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
||||
@implements IAsyncDisposable
|
||||
@using Core.Parsers
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Authentication
|
||||
@using WebApp.Models
|
||||
@inject AppDbContext Context
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ILogger<EventCatalogImport> Logger
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageHeader
|
||||
Title="Import Event Catalog"
|
||||
Description="Add new event definitions from CSV. Existing event names are skipped."
|
||||
Icon="@AppIcons.Events"
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="/events" />
|
||||
|
||||
<MudGrid>
|
||||
<MudItem xs="12" md="5">
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Upload CSV</MudText>
|
||||
<MudStack Spacing="3">
|
||||
<MudText Typo="Typo.body2">
|
||||
Required columns: <code>Event</code>, <code>Team Size</code>, <code>State Count</code>.
|
||||
Optional: <code>Short Name</code>, <code>EventFormat</code>, <code>Level of Effort</code>,
|
||||
<code>Eligibility</code>, <code>Description</code>, <code>Theme</code>,
|
||||
<code>Documentation</code>, <code>State Presubmission</code>,
|
||||
<code>Semifinalist Activity</code>, <code>Regional Notes</code>.
|
||||
</MudText>
|
||||
<InputFile OnChange="HandleFileChanged" accept=".csv,text/csv" />
|
||||
@if (!string.IsNullOrEmpty(_fileName))
|
||||
{
|
||||
<MudText Typo="Typo.caption">@_fileName</MudText>
|
||||
}
|
||||
<MudStack Row="true" Spacing="2">
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary"
|
||||
StartIcon="@Icons.Material.Filled.Article"
|
||||
OnClick="HandleParse"
|
||||
Disabled="@(_isParsing || _fileBytes is null)">
|
||||
Parse
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Text" OnClick="HandleClear" Disabled="@_isParsing">
|
||||
Clear
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
</MudStack>
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" md="7">
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Parsed Results</MudText>
|
||||
@if (_isParsing)
|
||||
{
|
||||
<MudProgressLinear Indeterminate="true" Class="mb-4" />
|
||||
<MudText>Parsing...</MudText>
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_parseError))
|
||||
{
|
||||
<MudAlert Severity="Severity.Error">@_parseError</MudAlert>
|
||||
}
|
||||
else if (_events is null)
|
||||
{
|
||||
<MudText Class="mud-text-secondary">Upload and parse a CSV to see results here</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudStack Spacing="3">
|
||||
<MudAlert Severity="Severity.Success" Dense="true">
|
||||
@_events.Length event(s) parsed.
|
||||
@_newEventCount new, @_existingEventCount already in the database.
|
||||
</MudAlert>
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Success"
|
||||
StartIcon="@Icons.Material.Filled.Save"
|
||||
OnClick="HandleSave"
|
||||
Disabled="@(_isSaving || _newEventCount == 0)">
|
||||
Save to Database
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
}
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
|
||||
@code {
|
||||
private byte[]? _fileBytes;
|
||||
private string? _fileName;
|
||||
private EventDefinition[]? _events;
|
||||
private int _newEventCount;
|
||||
private int _existingEventCount;
|
||||
private string? _parseError;
|
||||
private bool _isParsing;
|
||||
private bool _isSaving;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
private async Task HandleFileChanged(InputFileChangeEventArgs args)
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = args.File.OpenReadStream(maxAllowedSize: 1024 * 1024);
|
||||
await using var memory = new MemoryStream();
|
||||
await stream.CopyToAsync(memory, _cancellationTokenSource?.Token ?? CancellationToken.None);
|
||||
_fileBytes = memory.ToArray();
|
||||
_fileName = args.File.Name;
|
||||
ResetParse();
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error reading event catalog CSV");
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not read file: {ex.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleParse()
|
||||
{
|
||||
if (_fileBytes is null)
|
||||
{
|
||||
Snackbar.Add("Please choose a CSV file first", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
_isParsing = true;
|
||||
_parseError = null;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
using var reader = new StreamReader(new MemoryStream(_fileBytes));
|
||||
_events = new EventDefinitionParser(reader).Parse();
|
||||
|
||||
var existingNames = await Context.Events
|
||||
.AsNoTracking()
|
||||
.Select(e => e.Name)
|
||||
.ToListAsync(token);
|
||||
var existingSet = existingNames.ToHashSet();
|
||||
|
||||
_existingEventCount = _events.Count(e => existingSet.Contains(e.Name));
|
||||
_newEventCount = _events.Length - _existingEventCount;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error parsing event catalog CSV");
|
||||
_events = null;
|
||||
_parseError = $"Error parsing CSV: {ex.Message}";
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add(_parseError, Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isParsing = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleSave()
|
||||
{
|
||||
if (_events is null)
|
||||
{
|
||||
Snackbar.Add("Parse a CSV first", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
_isSaving = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
var added = 0;
|
||||
foreach (var evt in _events)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var exists = await Context.Events.FirstOrDefaultAsync(e => e.Name == evt.Name, token);
|
||||
if (exists != null)
|
||||
continue;
|
||||
await Context.Events.AddAsync(evt, token);
|
||||
added++;
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync(token);
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
Snackbar.Add($"Added {added} event(s).", Severity.Success);
|
||||
NavigationManager.NavigateTo("/events");
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error saving imported events");
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Error saving events: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleClear()
|
||||
{
|
||||
_fileBytes = null;
|
||||
_fileName = null;
|
||||
ResetParse();
|
||||
}
|
||||
|
||||
private void ResetParse()
|
||||
{
|
||||
_events = null;
|
||||
_newEventCount = 0;
|
||||
_existingEventCount = 0;
|
||||
_parseError = null;
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_isDisposed = true;
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
}
|
||||
|
||||
await ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Models
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using WebApp.Authentication
|
||||
@inject AppDbContext Context
|
||||
@inject IDialogService DialogService
|
||||
@inject ISnackbar Snackbar
|
||||
@@ -13,6 +14,11 @@
|
||||
<MudTooltip Text="Create New">
|
||||
<MudButton StartIcon="@Icons.Material.Filled.Create" Href="events/create" Variant="Variant.Filled" Color="Color.Primary">Create New</MudButton>
|
||||
</MudTooltip>
|
||||
<AuthorizeView Roles="@AuthRoles.Administrator">
|
||||
<MudTooltip Text="Add new catalog events from CSV. Existing names are skipped.">
|
||||
<MudButton StartIcon="@Icons.Material.Filled.UploadFile" Href="/events/import" Variant="Variant.Outlined">Import</MudButton>
|
||||
</MudTooltip>
|
||||
</AuthorizeView>
|
||||
<MudTooltip Text="Printable Descriptions">
|
||||
<MudButton StartIcon="@Icons.Material.Filled.Print" Href="events/printout" Variant="Variant.Outlined">Printable Descriptions</MudButton>
|
||||
</MudTooltip>
|
||||
|
||||
@@ -321,7 +321,7 @@
|
||||
var dialog = await DialogService.ShowAsync<MeetingHistoryDetailDialog>("Meeting Details", parameters, options);
|
||||
var result = await dialog.Result;
|
||||
|
||||
if (!result.Canceled)
|
||||
if (result is { Canceled: false })
|
||||
{
|
||||
// Refresh data if meeting was updated or deleted
|
||||
await RefreshMeetingHistories();
|
||||
|
||||
@@ -664,7 +664,7 @@
|
||||
Snackbar.Add($"Selected {newCount} new team(s) from clipboard ({totalCount - newCount} already selected)", Severity.Success);
|
||||
}
|
||||
}
|
||||
catch (JSException ex)
|
||||
catch (JSException)
|
||||
{
|
||||
Snackbar.Add("Unable to access clipboard. Please ensure clipboard permissions are granted.", Severity.Error);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@
|
||||
<MudStack Row="true" Spacing="1" Wrap="Wrap.Wrap">
|
||||
@{
|
||||
var presentStudentIds = _meetingHistory.Students.Select(s => s.Id).ToHashSet();
|
||||
var allStudents = GetAllStudentsFromTeams().OrderBy(s => s.FirstName);
|
||||
var allStudents = GetAllStudentsFromTeams().OrderBy(s => s.DisplayFirstName);
|
||||
}
|
||||
@foreach (var student in allStudents)
|
||||
{
|
||||
@@ -401,7 +401,7 @@
|
||||
var result = await dialog.Result;
|
||||
|
||||
// Refresh meeting history if dialog was saved
|
||||
if (!result.Canceled && !_isDisposed)
|
||||
if (result is { Canceled: false } && !_isDisposed)
|
||||
{
|
||||
await LoadMeetingHistory();
|
||||
}
|
||||
|
||||
@@ -11,12 +11,11 @@
|
||||
|
||||
<MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center" Class="d-flex align-center">
|
||||
<MudStack Row="true" Spacing="1" AlignItems="AlignItems.Center">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Clear"
|
||||
<MudIconButton Icon="@Icons.Material.Filled.Clear"
|
||||
Size="Size.Small"
|
||||
Class="@(removed ? "" : "d-none")"
|
||||
OnClick="@(() => OnToggleTeam.InvokeAsync(team))"
|
||||
Style="cursor: pointer;">
|
||||
</MudIcon>
|
||||
aria-label="Restore team" />
|
||||
@{
|
||||
var teamMembers = TeamStudentNameFormatter.FormatStudentList(
|
||||
team,
|
||||
|
||||
@@ -0,0 +1,933 @@
|
||||
@page "/print"
|
||||
@attribute [Authorize]
|
||||
@implements IAsyncDisposable
|
||||
@using Core.Printing
|
||||
@inject INotesService NotesService
|
||||
@inject INotePrintService NotePrintService
|
||||
@inject IPrintPresetService PrintPresetService
|
||||
@inject IConfiguration Configuration
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IDialogService DialogService
|
||||
@inject IJSRuntime JSRuntime
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject MarkdownTablePasteService MarkdownTablePasteService
|
||||
|
||||
<div class="no-print">
|
||||
<PageHeader Title="Page printer"
|
||||
Description="Write a markdown template, merge it onto students, teams, or events, and print."
|
||||
Icon="@Icons.Material.Filled.Print" />
|
||||
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6 mb-4">
|
||||
@if (_isLoading)
|
||||
{
|
||||
<MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="mb-4" />
|
||||
}
|
||||
|
||||
<MudGrid>
|
||||
<MudItem xs="12" md="5">
|
||||
<MudSelect T="int?"
|
||||
Label="Print preset"
|
||||
Value="_selectedPresetId"
|
||||
ValueChanged="OnPresetSelected"
|
||||
Clearable="true"
|
||||
Variant="Variant.Outlined">
|
||||
@foreach (var preset in _presets)
|
||||
{
|
||||
<MudSelectItem T="int?" Value="@preset.Id">@preset.Name</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
<MudItem xs="12" md="7" Class="d-flex align-center gap-2 flex-wrap">
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
OnClick="NewTemplate"
|
||||
Disabled="@_isBusy">
|
||||
New
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
StartIcon="@Icons.Material.Filled.Save"
|
||||
OnClick="SavePreset"
|
||||
Disabled="@_isBusy">
|
||||
Save
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
Color="Color.Error"
|
||||
OnClick="DeletePreset"
|
||||
Disabled="@(_isBusy || !_selectedPresetId.HasValue)">
|
||||
Delete
|
||||
</MudButton>
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" md="4">
|
||||
<MudSelect T="PrintEntityType"
|
||||
Label="Entity"
|
||||
Value="_entityType"
|
||||
ValueChanged="OnEntityTypeChanged"
|
||||
Variant="Variant.Outlined">
|
||||
<MudSelectItem Value="PrintEntityType.Student">Students</MudSelectItem>
|
||||
<MudSelectItem Value="PrintEntityType.Team">Teams</MudSelectItem>
|
||||
<MudSelectItem Value="PrintEntityType.Event">Events</MudSelectItem>
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
|
||||
@if (_entityType == PrintEntityType.Student)
|
||||
{
|
||||
<MudItem xs="12" sm="4" md="2">
|
||||
<MudNumericField T="int?"
|
||||
Label="Grade"
|
||||
Value="_grade"
|
||||
ValueChanged="OnGradeChanged"
|
||||
Variant="Variant.Outlined"
|
||||
Min="5"
|
||||
Max="12"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4" md="2">
|
||||
<MudNumericField T="int?"
|
||||
Label="TSA year"
|
||||
Value="_tsaYear"
|
||||
ValueChanged="OnTsaYearChanged"
|
||||
Variant="Variant.Outlined"
|
||||
Min="1"
|
||||
Max="12"
|
||||
Clearable="true" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4" md="4">
|
||||
<MudSelect T="string"
|
||||
Label="Officer"
|
||||
Value="@_officerChoice"
|
||||
ValueChanged="OnOfficerChanged"
|
||||
Variant="Variant.Outlined">
|
||||
<MudSelectItem Value="@("any")">Any</MudSelectItem>
|
||||
<MudSelectItem Value="@("yes")">Officers only</MudSelectItem>
|
||||
<MudSelectItem Value="@("no")">Non-officers only</MudSelectItem>
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
}
|
||||
else if (_entityType == PrintEntityType.Team)
|
||||
{
|
||||
<MudItem xs="12" md="8">
|
||||
<MudTextField T="string"
|
||||
Value="@_teamIdentifierContains"
|
||||
ValueChanged="OnTeamIdentifierChanged"
|
||||
Label="Identifier contains"
|
||||
Variant="Variant.Outlined"
|
||||
Immediate="true" />
|
||||
</MudItem>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudItem xs="12" sm="4">
|
||||
<MudTextField T="string"
|
||||
Value="@_eventNameContains"
|
||||
ValueChanged="OnEventNameChanged"
|
||||
Label="Name contains"
|
||||
Variant="Variant.Outlined"
|
||||
Immediate="true" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4">
|
||||
<MudSelect T="EventFormat?"
|
||||
Label="Event format"
|
||||
Value="_eventFormat"
|
||||
ValueChanged="OnEventFormatChanged"
|
||||
Variant="Variant.Outlined"
|
||||
Clearable="true">
|
||||
@foreach (var format in Enum.GetValues<EventFormat>())
|
||||
{
|
||||
<MudSelectItem T="EventFormat?" Value="@format">@format</MudSelectItem>
|
||||
}
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4">
|
||||
<MudSelect T="string"
|
||||
Label="Regional"
|
||||
Value="@_regionalChoice"
|
||||
ValueChanged="OnRegionalChanged"
|
||||
Variant="Variant.Outlined">
|
||||
<MudSelectItem Value="@("any")">Any</MudSelectItem>
|
||||
<MudSelectItem Value="@("yes")">Regional only</MudSelectItem>
|
||||
<MudSelectItem Value="@("no")">Non-regional only</MudSelectItem>
|
||||
</MudSelect>
|
||||
</MudItem>
|
||||
}
|
||||
|
||||
<MudItem xs="12">
|
||||
<MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center" Class="flex-wrap mb-2">
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
StartIcon="@Icons.Material.Filled.DataObject"
|
||||
OnClick="OpenTokenDialog"
|
||||
Disabled="@_isBusy">
|
||||
Insert token
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
StartIcon="@Icons.Material.Filled.Visibility"
|
||||
OnClick="Preview"
|
||||
Disabled="@_isBusy">
|
||||
Preview
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Outlined"
|
||||
StartIcon="@Icons.Material.Filled.Print"
|
||||
OnClick="Print"
|
||||
Disabled="@(_isBusy || _previewStale || _pages.Count == 0)">
|
||||
Print
|
||||
</MudButton>
|
||||
<MudCheckBox T="bool"
|
||||
Value="@_newPagePerRecord"
|
||||
ValueChanged="OnNewPagePerRecordChanged"
|
||||
Label="New page per record"
|
||||
Dense="true" />
|
||||
<MudNumericField T="int"
|
||||
Label="Font size (pt)"
|
||||
Value="_fontSizePt"
|
||||
ValueChanged="OnFontSizeChanged"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense"
|
||||
Min="PrintPresetFilters.MinFontSizePt"
|
||||
Max="PrintPresetFilters.MaxFontSizePt"
|
||||
Style="max-width: 8rem;" />
|
||||
<MudNumericField T="int"
|
||||
Label="Answer lines"
|
||||
Value="_answerSpaceLines"
|
||||
ValueChanged="OnAnswerSpaceLinesChanged"
|
||||
Variant="Variant.Outlined"
|
||||
Margin="Margin.Dense"
|
||||
Min="PrintPresetFilters.MinAnswerSpaceLines"
|
||||
Max="PrintPresetFilters.MaxAnswerSpaceLines"
|
||||
Style="max-width: 8rem;" />
|
||||
@if (IsDirty())
|
||||
{
|
||||
<MudText Typo="Typo.caption" Color="Color.Warning">Unsaved changes</MudText>
|
||||
}
|
||||
@if (!_previewStale && _pages.Count > 0)
|
||||
{
|
||||
<MudText Typo="Typo.body2">@_pages.Count page@(_pages.Count == 1 ? "" : "s")</MudText>
|
||||
}
|
||||
else if (!_previewStale && _didPreview)
|
||||
{
|
||||
<MudText Typo="Typo.body2">No matches</MudText>
|
||||
}
|
||||
</MudStack>
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12">
|
||||
<MudText Typo="Typo.subtitle2" Class="mb-2">Template (Markdown)</MudText>
|
||||
<div id="@EditorElementId" @key="_editorGeneration">
|
||||
<MarkdownEditor Value="@_templateMarkdown"
|
||||
ValueChanged="OnMarkdownChanged"
|
||||
Placeholder="Write the printable page. Insert tokens for student, team, or event values."
|
||||
AutoSaveEnabled="false"
|
||||
NativeSpellChecker="false"
|
||||
HideIcons="@HiddenEditorIcons" />
|
||||
</div>
|
||||
<MudText Typo="Typo.subtitle2" Class="mt-4 mb-1">Template preview</MudText>
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary mb-2">Markdown only — tokens are not merged here.</MudText>
|
||||
@if (string.IsNullOrWhiteSpace(_templateMarkdown))
|
||||
{
|
||||
<MudText Typo="Typo.body2" Class="mud-text-secondary">The template will preview here as you type.</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudPaper Elevation="0" Class="pa-3 note-print-page" Style="@($"background-color: var(--mud-palette-background-grey);{PrintPageStyle}")">
|
||||
<div class="markdown-content">
|
||||
@((MarkupString)MarkdownHelper.ToHtml(_templateMarkdown))
|
||||
</div>
|
||||
</MudPaper>
|
||||
}
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
</div>
|
||||
|
||||
<div class="print-only">
|
||||
<PrintPageStack Pages="_pages"
|
||||
NewPagePerRecord="_newPagePerRecord"
|
||||
PrintPageStyle="@PrintPageStyle" />
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private const string EditorElementId = "page-printer-editor";
|
||||
private static readonly string[] HiddenEditorIcons = ["preview", "side-by-side", "fullscreen"];
|
||||
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed;
|
||||
private bool _isLoading = true;
|
||||
private bool _isBusy;
|
||||
private bool _previewStale = true;
|
||||
private bool _didPreview;
|
||||
private bool _pasteInitialized;
|
||||
private int _editorGeneration;
|
||||
|
||||
private List<PrintPreset> _presets = [];
|
||||
private List<string> _importedTokenNames = [];
|
||||
private IReadOnlyList<NotePrintPage> _pages = [];
|
||||
private IDialogReference? _previewDialog;
|
||||
private IDisposable? _navigationRegistration;
|
||||
|
||||
private int? _selectedPresetId;
|
||||
private string _templateMarkdown = string.Empty;
|
||||
private PrintEntityType _entityType = PrintEntityType.Student;
|
||||
private int? _grade;
|
||||
private int? _tsaYear;
|
||||
private string _officerChoice = "any";
|
||||
private string? _teamIdentifierContains;
|
||||
private string? _eventNameContains;
|
||||
private EventFormat? _eventFormat;
|
||||
private string _regionalChoice = "any";
|
||||
private bool _newPagePerRecord = true;
|
||||
private int _fontSizePt = PrintPresetFilters.DefaultFontSizePt;
|
||||
private int _answerSpaceLines = PrintPresetFilters.DefaultAnswerSpaceLines;
|
||||
|
||||
private string _snapshotMarkdown = string.Empty;
|
||||
private PrintEntityType _snapshotEntityType = PrintEntityType.Student;
|
||||
private string _snapshotFiltersJson = string.Empty;
|
||||
|
||||
private string PrintPageStyle =>
|
||||
$"--print-font-size:{_fontSizePt}pt;--print-answer-lines:{_answerSpaceLines};";
|
||||
|
||||
private bool CanPreview => !string.IsNullOrWhiteSpace(_templateMarkdown);
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
CaptureSnapshot();
|
||||
_navigationRegistration = NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await LoadAsync();
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (_pasteInitialized || _isDisposed)
|
||||
return;
|
||||
|
||||
await Task.Delay(150);
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
await MarkdownTablePasteService.InitializeAsync(EditorElementId);
|
||||
_pasteInitialized = true;
|
||||
}
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
_isLoading = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
await RefreshImportedTokenNamesAsync(token);
|
||||
_presets = [.. await PrintPresetService.GetAllAsync(token)];
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task MarkStaleAsync()
|
||||
{
|
||||
_previewStale = true;
|
||||
_pages = [];
|
||||
await ClosePreviewDialogAsync();
|
||||
}
|
||||
|
||||
private void MarkStale() => _ = MarkStaleAsync();
|
||||
|
||||
private async Task ClosePreviewDialogAsync()
|
||||
{
|
||||
var dialog = _previewDialog;
|
||||
_previewDialog = null;
|
||||
if (dialog is null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
dialog.Close();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnEntityTypeChanged(PrintEntityType value)
|
||||
{
|
||||
_entityType = value;
|
||||
await MarkStaleAsync();
|
||||
}
|
||||
|
||||
private void OnMarkdownChanged(string? value)
|
||||
{
|
||||
_templateMarkdown = value ?? string.Empty;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnGradeChanged(int? value)
|
||||
{
|
||||
_grade = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnTsaYearChanged(int? value)
|
||||
{
|
||||
_tsaYear = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnOfficerChanged(string value)
|
||||
{
|
||||
_officerChoice = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnTeamIdentifierChanged(string? value)
|
||||
{
|
||||
_teamIdentifierContains = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnEventNameChanged(string? value)
|
||||
{
|
||||
_eventNameContains = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnEventFormatChanged(EventFormat? value)
|
||||
{
|
||||
_eventFormat = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnRegionalChanged(string value)
|
||||
{
|
||||
_regionalChoice = value;
|
||||
MarkStale();
|
||||
}
|
||||
|
||||
private void OnNewPagePerRecordChanged(bool value)
|
||||
{
|
||||
_newPagePerRecord = value;
|
||||
}
|
||||
|
||||
private void OnFontSizeChanged(int value)
|
||||
{
|
||||
_fontSizePt = Math.Clamp(value, PrintPresetFilters.MinFontSizePt, PrintPresetFilters.MaxFontSizePt);
|
||||
}
|
||||
|
||||
private void OnAnswerSpaceLinesChanged(int value)
|
||||
{
|
||||
_answerSpaceLines = Math.Clamp(value, PrintPresetFilters.MinAnswerSpaceLines, PrintPresetFilters.MaxAnswerSpaceLines);
|
||||
}
|
||||
|
||||
private async Task RefreshImportedTokenNamesAsync(CancellationToken token)
|
||||
{
|
||||
var indexFields = WebApp.Models.ChapterSettings.ReadIndexNoteFields(Configuration);
|
||||
var discovered = await NotesService.GetImportedFieldNamesAsync(token);
|
||||
_importedTokenNames =
|
||||
[
|
||||
.. indexFields
|
||||
.Concat(discovered)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)
|
||||
];
|
||||
}
|
||||
|
||||
private async Task FlushEditorAsync()
|
||||
{
|
||||
var value = await MarkdownTablePasteService.GetValueAsync(EditorElementId);
|
||||
if (value is not null && value != _templateMarkdown)
|
||||
{
|
||||
_templateMarkdown = value;
|
||||
await MarkStaleAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsDirty()
|
||||
{
|
||||
return !string.Equals(_templateMarkdown, _snapshotMarkdown, StringComparison.Ordinal)
|
||||
|| _entityType != _snapshotEntityType
|
||||
|| !string.Equals(BuildFilters().ToJson(), _snapshotFiltersJson, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private void CaptureSnapshot()
|
||||
{
|
||||
_snapshotMarkdown = _templateMarkdown;
|
||||
_snapshotEntityType = _entityType;
|
||||
_snapshotFiltersJson = BuildFilters().ToJson();
|
||||
}
|
||||
|
||||
private async Task<bool> ConfirmDiscardIfDirtyAsync()
|
||||
{
|
||||
await FlushEditorAsync();
|
||||
if (!IsDirty())
|
||||
return true;
|
||||
|
||||
var confirmed = await DialogService.ShowMessageBox(
|
||||
"Unsaved changes",
|
||||
"Discard unsaved template or filter changes?",
|
||||
yesText: "Discard",
|
||||
cancelText: "Stay");
|
||||
|
||||
return confirmed == true && !_isDisposed;
|
||||
}
|
||||
|
||||
private async ValueTask OnLocationChanging(LocationChangingContext context)
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
if (!await ConfirmDiscardIfDirtyAsync())
|
||||
context.PreventNavigation();
|
||||
}
|
||||
|
||||
private async Task OnPresetSelected(int? id)
|
||||
{
|
||||
if (id == _selectedPresetId)
|
||||
return;
|
||||
|
||||
if (!await ConfirmDiscardIfDirtyAsync())
|
||||
return;
|
||||
|
||||
_selectedPresetId = id;
|
||||
if (!id.HasValue)
|
||||
{
|
||||
await MarkStaleAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
var preset = _presets.FirstOrDefault(p => p.Id == id.Value);
|
||||
if (preset is null)
|
||||
return;
|
||||
|
||||
ApplyPreset(preset);
|
||||
CaptureSnapshot();
|
||||
await MarkStaleAsync();
|
||||
}
|
||||
|
||||
private async Task NewTemplate()
|
||||
{
|
||||
if (_isDisposed || !await ConfirmDiscardIfDirtyAsync())
|
||||
return;
|
||||
|
||||
_selectedPresetId = null;
|
||||
_templateMarkdown = string.Empty;
|
||||
_entityType = PrintEntityType.Student;
|
||||
_grade = null;
|
||||
_tsaYear = null;
|
||||
_officerChoice = "any";
|
||||
_teamIdentifierContains = null;
|
||||
_eventNameContains = null;
|
||||
_eventFormat = null;
|
||||
_regionalChoice = "any";
|
||||
_newPagePerRecord = true;
|
||||
_fontSizePt = PrintPresetFilters.DefaultFontSizePt;
|
||||
_answerSpaceLines = PrintPresetFilters.DefaultAnswerSpaceLines;
|
||||
_editorGeneration++;
|
||||
_pasteInitialized = false;
|
||||
CaptureSnapshot();
|
||||
await MarkStaleAsync();
|
||||
}
|
||||
|
||||
private void ApplyPreset(PrintPreset preset)
|
||||
{
|
||||
_entityType = preset.EntityType;
|
||||
_templateMarkdown = preset.TemplateMarkdown ?? string.Empty;
|
||||
var filters = PrintPresetFilters.FromJson(preset.FiltersJson);
|
||||
_grade = filters.Grade;
|
||||
_tsaYear = filters.TsaYear;
|
||||
_officerChoice = PrintPresetFilters.ToTriState(filters.IsOfficer);
|
||||
_teamIdentifierContains = filters.TeamIdentifierContains;
|
||||
_eventNameContains = filters.EventNameContains;
|
||||
_eventFormat = filters.EventFormat;
|
||||
_regionalChoice = PrintPresetFilters.ToTriState(filters.RegionalOnly);
|
||||
_newPagePerRecord = filters.NewPagePerRecord;
|
||||
_fontSizePt = filters.FontSizePt;
|
||||
_answerSpaceLines = filters.AnswerSpaceLines;
|
||||
_editorGeneration++;
|
||||
_pasteInitialized = false;
|
||||
}
|
||||
|
||||
private PrintPresetFilters BuildFilters() =>
|
||||
new()
|
||||
{
|
||||
Grade = _entityType == PrintEntityType.Student ? _grade : null,
|
||||
TsaYear = _entityType == PrintEntityType.Student ? _tsaYear : null,
|
||||
IsOfficer = _entityType == PrintEntityType.Student ? PrintPresetFilters.FromTriState(_officerChoice) : null,
|
||||
TeamIdentifierContains = _entityType == PrintEntityType.Team ? _teamIdentifierContains : null,
|
||||
EventNameContains = _entityType == PrintEntityType.Event ? _eventNameContains : null,
|
||||
EventFormat = _entityType == PrintEntityType.Event ? _eventFormat : null,
|
||||
RegionalOnly = _entityType == PrintEntityType.Event ? PrintPresetFilters.FromTriState(_regionalChoice) : null,
|
||||
NewPagePerRecord = _newPagePerRecord,
|
||||
FontSizePt = _fontSizePt,
|
||||
AnswerSpaceLines = _answerSpaceLines
|
||||
};
|
||||
|
||||
private async Task OpenTokenDialog()
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
await RefreshImportedTokenNamesAsync(token);
|
||||
|
||||
var parameters = new DialogParameters<PrintTokenInsertDialog>
|
||||
{
|
||||
{ x => x.EntityType, _entityType },
|
||||
{ x => x.ImportedFieldNames, _importedTokenNames },
|
||||
{ x => x.OnInsert, EventCallback.Factory.Create<string>(this, InsertToken) }
|
||||
};
|
||||
|
||||
var options = new DialogOptions
|
||||
{
|
||||
MaxWidth = MaxWidth.Large,
|
||||
FullWidth = true,
|
||||
CloseButton = true
|
||||
};
|
||||
|
||||
await DialogService.ShowAsync<PrintTokenInsertDialog>("Insert token", parameters, options);
|
||||
}
|
||||
|
||||
private async Task InsertToken(string tokenName)
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
await FlushEditorAsync();
|
||||
var wrapped = "{{" + tokenName + "}}";
|
||||
var inserted = await MarkdownTablePasteService.InsertAtCursorAsync(EditorElementId, wrapped);
|
||||
if (!inserted)
|
||||
{
|
||||
_templateMarkdown += wrapped;
|
||||
await MarkdownTablePasteService.SetValueAsync(EditorElementId, _templateMarkdown);
|
||||
}
|
||||
else
|
||||
{
|
||||
var value = await MarkdownTablePasteService.GetValueAsync(EditorElementId);
|
||||
if (value is not null)
|
||||
_templateMarkdown = value;
|
||||
}
|
||||
|
||||
await MarkStaleAsync();
|
||||
}
|
||||
|
||||
private async Task Preview()
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
await FlushEditorAsync();
|
||||
if (!CanPreview)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add("Write a template before previewing.", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_previewStale && _didPreview)
|
||||
{
|
||||
await OpenPreviewDialogAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
_isBusy = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
await RefreshImportedTokenNamesAsync(token);
|
||||
|
||||
_pages = await NotePrintService.PreviewAsync(
|
||||
new NotePrintRequest
|
||||
{
|
||||
EntityType = _entityType,
|
||||
TemplateMarkdown = _templateMarkdown,
|
||||
Filters = BuildFilters(),
|
||||
ImportedFieldCatalog = _importedTokenNames
|
||||
},
|
||||
token);
|
||||
|
||||
_previewStale = false;
|
||||
_didPreview = true;
|
||||
|
||||
if (!_isDisposed)
|
||||
await OpenPreviewDialogAsync();
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Preview failed: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OpenPreviewDialogAsync()
|
||||
{
|
||||
await ClosePreviewDialogAsync();
|
||||
|
||||
var parameters = new DialogParameters<PrintPreviewDialog>
|
||||
{
|
||||
{ x => x.Pages, _pages },
|
||||
{ x => x.NewPagePerRecord, _newPagePerRecord },
|
||||
{ x => x.PrintPageStyle, PrintPageStyle },
|
||||
{ x => x.OnPrint, EventCallback.Factory.Create(this, Print) }
|
||||
};
|
||||
|
||||
var options = new DialogOptions
|
||||
{
|
||||
MaxWidth = MaxWidth.ExtraLarge,
|
||||
FullWidth = true,
|
||||
CloseButton = true
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<PrintPreviewDialog>("Print preview", parameters, options);
|
||||
_previewDialog = dialog;
|
||||
_ = TrackPreviewDialog(dialog);
|
||||
}
|
||||
|
||||
private async Task TrackPreviewDialog(IDialogReference dialog)
|
||||
{
|
||||
try
|
||||
{
|
||||
await dialog.Result;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (_previewDialog == dialog)
|
||||
_previewDialog = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Print()
|
||||
{
|
||||
if (_isDisposed || _previewStale || _pages.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await JSRuntime.InvokeVoidAsync("window.print");
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SavePreset()
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
await FlushEditorAsync();
|
||||
if (!CanPreview)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add("Write a template before saving.", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedPresetId.HasValue)
|
||||
{
|
||||
await UpdateSelectedPresetAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
var name = await PromptForPresetNameAsync();
|
||||
if (string.IsNullOrWhiteSpace(name) || _isDisposed)
|
||||
return;
|
||||
|
||||
_isBusy = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
if (await PrintPresetService.NameExistsAsync(name, null, token))
|
||||
{
|
||||
Snackbar.Add($"A print preset named '{name}' already exists.", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
var created = await PrintPresetService.CreateAsync(
|
||||
new PrintPreset
|
||||
{
|
||||
Name = name,
|
||||
TemplateMarkdown = _templateMarkdown,
|
||||
EntityType = _entityType,
|
||||
FiltersJson = BuildFilters().ToJson()
|
||||
},
|
||||
token);
|
||||
|
||||
_presets = [.. await PrintPresetService.GetAllAsync(token)];
|
||||
_selectedPresetId = created.Id;
|
||||
CaptureSnapshot();
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Saved print preset '{name}'.", Severity.Success);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not save: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string?> PromptForPresetNameAsync()
|
||||
{
|
||||
var options = new DialogOptions
|
||||
{
|
||||
MaxWidth = MaxWidth.Small,
|
||||
FullWidth = true,
|
||||
CloseButton = true
|
||||
};
|
||||
|
||||
var dialog = await DialogService.ShowAsync<PrintPresetNameDialog>("Save print preset", options);
|
||||
var result = await dialog.Result;
|
||||
if (result is null || result.Canceled || result.Data is not string name)
|
||||
return null;
|
||||
|
||||
return name.Trim();
|
||||
}
|
||||
|
||||
private async Task UpdateSelectedPresetAsync()
|
||||
{
|
||||
if (_isDisposed || !_selectedPresetId.HasValue)
|
||||
return;
|
||||
|
||||
_isBusy = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
var existing = _presets.FirstOrDefault(p => p.Id == _selectedPresetId.Value);
|
||||
if (existing is null)
|
||||
return;
|
||||
|
||||
await PrintPresetService.UpdateAsync(
|
||||
new PrintPreset
|
||||
{
|
||||
Id = existing.Id,
|
||||
Name = existing.Name,
|
||||
TemplateMarkdown = _templateMarkdown,
|
||||
EntityType = _entityType,
|
||||
FiltersJson = BuildFilters().ToJson()
|
||||
},
|
||||
token);
|
||||
|
||||
_presets = [.. await PrintPresetService.GetAllAsync(token)];
|
||||
CaptureSnapshot();
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Saved print preset '{existing.Name}'.", Severity.Success);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not save: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DeletePreset()
|
||||
{
|
||||
if (_isDisposed || !_selectedPresetId.HasValue)
|
||||
return;
|
||||
|
||||
var existing = _presets.FirstOrDefault(p => p.Id == _selectedPresetId.Value);
|
||||
if (existing is null)
|
||||
return;
|
||||
|
||||
var confirmed = await DialogService.ShowMessageBox(
|
||||
"Delete print preset",
|
||||
(MarkupString)$"Delete <b>{existing.Name}</b>? This cannot be undone.",
|
||||
yesText: "Delete",
|
||||
cancelText: "Cancel");
|
||||
|
||||
if (confirmed != true || _isDisposed)
|
||||
return;
|
||||
|
||||
_isBusy = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
await PrintPresetService.DeleteAsync(existing.Id, token);
|
||||
_presets = [.. await PrintPresetService.GetAllAsync(token)];
|
||||
_selectedPresetId = null;
|
||||
CaptureSnapshot();
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Deleted print preset '{existing.Name}'.", Severity.Info);
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not delete: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_isDisposed = true;
|
||||
_navigationRegistration?.Dispose();
|
||||
_navigationRegistration = null;
|
||||
await ClosePreviewDialogAsync();
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
}
|
||||
|
||||
await ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
@using WebApp.Services
|
||||
|
||||
@for (var i = 0; i < Pages.Count; i++)
|
||||
{
|
||||
var page = Pages[i];
|
||||
var isLast = i == Pages.Count - 1;
|
||||
var pageClass = NewPagePerRecord && !isLast
|
||||
? "note-print-page pagebreak"
|
||||
: "note-print-page";
|
||||
<MudContainer Class="@pageClass" Style="@PrintPageStyle">
|
||||
<div class="markdown-content">
|
||||
@((MarkupString)page.Html)
|
||||
</div>
|
||||
</MudContainer>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired]
|
||||
public IReadOnlyList<NotePrintPage> Pages { get; set; } = [];
|
||||
|
||||
[Parameter]
|
||||
public bool NewPagePerRecord { get; set; } = true;
|
||||
|
||||
[Parameter]
|
||||
public string PrintPageStyle { get; set; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<MudDialog>
|
||||
<DialogContent>
|
||||
<MudTextField @bind-Value="_name"
|
||||
Label="Preset name"
|
||||
Variant="Variant.Outlined"
|
||||
Immediate="true"
|
||||
MaxLength="100"
|
||||
Autofocus="true" />
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Cancel">Cancel</MudButton>
|
||||
<MudButton Color="Color.Primary"
|
||||
Variant="Variant.Filled"
|
||||
OnClick="Save"
|
||||
Disabled="@string.IsNullOrWhiteSpace(_name)">
|
||||
Save
|
||||
</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
private string _name = string.Empty;
|
||||
|
||||
private void Save()
|
||||
{
|
||||
var name = _name.Trim();
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return;
|
||||
|
||||
MudDialog.Close(DialogResult.Ok(name));
|
||||
}
|
||||
|
||||
private void Cancel() => MudDialog.Cancel();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
@using WebApp.Services
|
||||
|
||||
<MudDialog Class="no-print">
|
||||
<DialogContent>
|
||||
@if (Pages.Count == 0)
|
||||
{
|
||||
<MudText Class="mud-text-secondary">No matching records for these filters.</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (Pages.Count > 75)
|
||||
{
|
||||
<MudAlert Severity="Severity.Warning" Dense="true" Class="mb-3">
|
||||
This preview has @Pages.Count pages. Printing a large set can be slow.
|
||||
</MudAlert>
|
||||
}
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary mb-2">
|
||||
@Pages.Count page@(Pages.Count == 1 ? "" : "s")
|
||||
</MudText>
|
||||
<PrintPageStack Pages="Pages"
|
||||
NewPagePerRecord="NewPagePerRecord"
|
||||
PrintPageStyle="@PrintPageStyle" />
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Close">Close</MudButton>
|
||||
<MudButton Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
StartIcon="@Icons.Material.Filled.Print"
|
||||
OnClick="PrintAsync"
|
||||
Disabled="@(Pages.Count == 0)">
|
||||
Print
|
||||
</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
[Parameter, EditorRequired]
|
||||
public IReadOnlyList<NotePrintPage> Pages { get; set; } = [];
|
||||
|
||||
[Parameter]
|
||||
public bool NewPagePerRecord { get; set; } = true;
|
||||
|
||||
[Parameter]
|
||||
public string PrintPageStyle { get; set; } = string.Empty;
|
||||
|
||||
[Parameter]
|
||||
public EventCallback OnPrint { get; set; }
|
||||
|
||||
private async Task PrintAsync()
|
||||
{
|
||||
if (OnPrint.HasDelegate)
|
||||
await OnPrint.InvokeAsync();
|
||||
}
|
||||
|
||||
private void Close() => MudDialog.Close();
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
@using Core.Printing
|
||||
@inject ClipboardService ClipboardService
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudDialog>
|
||||
<DialogContent>
|
||||
<MudTextField @bind-Value="_search"
|
||||
Label="Search tokens"
|
||||
Variant="Variant.Outlined"
|
||||
Immediate="true"
|
||||
Adornment="Adornment.Start"
|
||||
AdornmentIcon="@Icons.Material.Filled.Search"
|
||||
Class="mb-3" />
|
||||
|
||||
@if (!HasAnyMatches)
|
||||
{
|
||||
<MudText Class="mud-text-secondary">No tokens match this search.</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
@foreach (var group in TokenGroups)
|
||||
{
|
||||
var tokens = Visible(group.Tokens);
|
||||
if (tokens.Count == 0)
|
||||
continue;
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary">@group.Label</MudText>
|
||||
<div class="mb-2">
|
||||
<TokenChips Tokens="tokens" Insert="Insert" Copy="Copy" />
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="Close">Close</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter]
|
||||
IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public PrintEntityType EntityType { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public IReadOnlyList<string> ImportedFieldNames { get; set; } = [];
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<string> OnInsert { get; set; }
|
||||
|
||||
private string _search = string.Empty;
|
||||
|
||||
private IEnumerable<(string Label, IReadOnlyList<string> Tokens)> TokenGroups
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return ("Layout", PrintFieldCatalog.Layout);
|
||||
yield return ("Chapter", PrintFieldCatalog.Chapter);
|
||||
yield return (EntityType.ToString(), PrintFieldCatalog.EntityTokens(EntityType));
|
||||
if (EntityType == PrintEntityType.Student)
|
||||
yield return ("Event ranks", PrintFieldCatalog.StudentRanks);
|
||||
if (EntityType == PrintEntityType.Student && ImportedFieldNames.Count > 0)
|
||||
yield return ("Additional fields", ImportedFieldNames);
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasAnyMatches =>
|
||||
TokenGroups.Any(group => Visible(group.Tokens).Count > 0);
|
||||
|
||||
private IReadOnlyList<string> Visible(IReadOnlyList<string> tokens) =>
|
||||
string.IsNullOrWhiteSpace(_search)
|
||||
? tokens
|
||||
: [.. tokens.Where(Matches)];
|
||||
|
||||
private bool Matches(string token) =>
|
||||
string.IsNullOrWhiteSpace(_search)
|
||||
|| token.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
private async Task Insert(string token)
|
||||
{
|
||||
if (OnInsert.HasDelegate)
|
||||
await OnInsert.InvokeAsync(token);
|
||||
}
|
||||
|
||||
private async Task Copy(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ClipboardService.WriteTextAsync("{{" + token + "}}");
|
||||
Snackbar.Add("Copied {{" + token + "}}", Severity.Info);
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Snackbar.Add($"Could not copy: {ex.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void Close() => MudDialog.Close();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
@foreach (var token in Tokens)
|
||||
{
|
||||
var name = token;
|
||||
<span class="d-inline-flex align-center mr-1 mb-1">
|
||||
<MudChip T="string"
|
||||
Size="Size.Small"
|
||||
OnClick="() => Insert.InvokeAsync(name)">
|
||||
{{@name}}
|
||||
</MudChip>
|
||||
<MudTooltip Text="Copy">
|
||||
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy"
|
||||
Size="Size.Small"
|
||||
OnClick="() => Copy.InvokeAsync(name)" />
|
||||
</MudTooltip>
|
||||
</span>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired]
|
||||
public IReadOnlyList<string> Tokens { get; set; } = [];
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<string> Insert { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback<string> Copy { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
@using Core.Services
|
||||
@using PSC.Blazor.Components.MarkdownEditor
|
||||
@inject INotesService NotesService
|
||||
@inject INoteNamingService NoteNamingService
|
||||
@inject MarkdownTablePasteService MarkdownTablePasteService
|
||||
@inject ISnackbar Snackbar
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Notes</MudText>
|
||||
@if (_isLoading)
|
||||
{
|
||||
<MudProgressLinear Indeterminate="true" />
|
||||
}
|
||||
else if (ReadOnly)
|
||||
{
|
||||
@if (string.IsNullOrWhiteSpace(_content))
|
||||
{
|
||||
<MudText Class="mud-text-secondary">No notes yet.</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
@((MarkupString)MarkdownHelper.ToHtml(_content))
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary mb-2">Markdown is supported. Additional fields appear in a table and can be edited.</MudText>
|
||||
<MarkdownEditor Value="@_content"
|
||||
ValueChanged="@((string? value) => _content = value ?? string.Empty)"
|
||||
Placeholder="Student notes..."
|
||||
AutoSaveEnabled="false"
|
||||
NativeSpellChecker="false" />
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
public int StudentId { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public bool ReadOnly { get; set; }
|
||||
|
||||
private string _content = string.Empty;
|
||||
private int? _noteId;
|
||||
private int _loadedStudentId;
|
||||
private bool _isLoading = true;
|
||||
private bool _pasteInitialized;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
if (StudentId <= 0 || _loadedStudentId == StudentId)
|
||||
return;
|
||||
|
||||
_isLoading = true;
|
||||
try
|
||||
{
|
||||
var note = await NotesService.GetStudentNoteAsync(StudentId);
|
||||
_noteId = note?.Id;
|
||||
_content = note?.Content ?? string.Empty;
|
||||
_loadedStudentId = StudentId;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (ReadOnly || _pasteInitialized)
|
||||
return;
|
||||
|
||||
await Task.Delay(150);
|
||||
await MarkdownTablePasteService.InitializeAsync();
|
||||
_pasteInitialized = true;
|
||||
}
|
||||
|
||||
public async Task SaveAsync()
|
||||
{
|
||||
if (StudentId <= 0)
|
||||
return;
|
||||
|
||||
if (_noteId is null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_content))
|
||||
return;
|
||||
|
||||
var created = await NotesService.CreateNoteAsync(new Note
|
||||
{
|
||||
Title = NoteNamingService.GetStudentNoteTitle(StudentId),
|
||||
Content = _content
|
||||
});
|
||||
_noteId = created.Id;
|
||||
return;
|
||||
}
|
||||
|
||||
var existing = await NotesService.GetNoteAsync(_noteId.Value);
|
||||
if (existing is null)
|
||||
return;
|
||||
|
||||
if (string.Equals(existing.Content ?? string.Empty, _content, StringComparison.Ordinal))
|
||||
return;
|
||||
|
||||
existing.Content = _content;
|
||||
await NotesService.UpdateNoteAsync(existing);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
Label="@Label"
|
||||
@bind-Value="_currentStudent"
|
||||
SearchFunc="@SearchStudents"
|
||||
ToStringFunc="@(s => ShowFullName ? s?.FirstNameLastName : s?.FirstName)"
|
||||
ToStringFunc="@(s => ShowFullName ? s?.FirstNameLastName : s?.DisplayFirstName)"
|
||||
Immediate="true"
|
||||
ResetValueOnEmptyText="true"
|
||||
CoerceText="false"
|
||||
@@ -21,7 +21,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
@student.FirstName
|
||||
@student.DisplayFirstName
|
||||
}
|
||||
@if (ShowGrade)
|
||||
{
|
||||
@@ -33,11 +33,11 @@
|
||||
@if (SelectedStudents.Any())
|
||||
{
|
||||
<MudChipSet T="Student" AllClosable="true" Class="mt-2">
|
||||
@foreach (var student in SelectedStudents.OrderBy(s => s.FirstName))
|
||||
@foreach (var student in SelectedStudents.OrderBy(s => s.DisplayFirstName))
|
||||
{
|
||||
<MudChip T="Student"
|
||||
Value="@student"
|
||||
Text="@(ShowFullName ? student.FirstNameLastName : student.FirstName)"
|
||||
Text="@(ShowFullName ? student.FirstNameLastName : student.DisplayFirstName)"
|
||||
OnClose="@(() => RemoveStudent(student))" />
|
||||
}
|
||||
</MudChipSet>
|
||||
@@ -91,8 +91,9 @@
|
||||
return Task.FromResult<IEnumerable<Student>>(Students
|
||||
.Where(s => !SelectedStudents.Contains(s))
|
||||
.Where(s => s.FirstName.ToLower().Contains(search) ||
|
||||
s.LastName.ToLower().Contains(search))
|
||||
.OrderBy(s => s.FirstName));
|
||||
s.LastName.ToLower().Contains(search) ||
|
||||
(s.Nickname != null && s.Nickname.ToLower().Contains(search)))
|
||||
.OrderBy(s => s.DisplayFirstName));
|
||||
}
|
||||
|
||||
private void RemoveStudent(Student student)
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
ValuesChanged="@OnSelectedStudentsChanged"
|
||||
Vertical="true"
|
||||
CheckMark>
|
||||
@foreach (var student in Students.OrderBy(e => e.FirstName))
|
||||
@foreach (var student in Students.OrderBy(e => e.DisplayFirstName))
|
||||
{
|
||||
<MudToggleItem Value="@student" Style="font-size: .75rem;">
|
||||
@if (ShowFullName)
|
||||
@@ -18,7 +18,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
@student.FirstName
|
||||
@student.DisplayFirstName
|
||||
}
|
||||
</MudToggleItem>
|
||||
}
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
<MudItem xs="12" sm="6">
|
||||
<MudTextField T="string" Label="Last Name" @bind-Value="Student.LastName" For="@(() => Student.LastName)" Variant="Variant.Outlined"></MudTextField>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6">
|
||||
<MudTextField T="string" Label="Nickname" @bind-Value="Student.Nickname" For="@(() => Student.Nickname)" Variant="Variant.Outlined" HelperText="Shown instead of first name on teams, rankings, and printouts"></MudTextField>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6">
|
||||
<MudTextField T="string" Label="Email Address" @bind-Value="Student.Email" For="@(() => Student.Email)" Variant="Variant.Outlined"></MudTextField>
|
||||
</MudItem>
|
||||
@@ -80,6 +83,7 @@
|
||||
|
||||
try
|
||||
{
|
||||
Student.NormalizeNickname();
|
||||
Context.Students.Add(Student);
|
||||
await Context.SaveChangesAsync();
|
||||
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
<MudText Typo="Typo.subtitle2" Class="mud-text-secondary">Last Name</MudText>
|
||||
<MudText Typo="Typo.body1">@student.LastName</MudText>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudText Typo="Typo.subtitle2" Class="mud-text-secondary">Nickname</MudText>
|
||||
<MudText Typo="Typo.body1">@(string.IsNullOrWhiteSpace(student.Nickname) ? "—" : student.Nickname)</MudText>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="6" md="4">
|
||||
<MudText Typo="Typo.subtitle2" Class="mud-text-secondary">Grade</MudText>
|
||||
<MudText Typo="Typo.body1">@student.Grade</MudText>
|
||||
@@ -82,6 +86,10 @@
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6 mt-4">
|
||||
<StudentNotePanel StudentId="student.Id" ReadOnly="true" />
|
||||
</MudPaper>
|
||||
|
||||
@code {
|
||||
private Student? student;
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudTextField T="string" Label="First Name" @bind-Value="Student.FirstName" For="@(() => Student.FirstName)"></MudTextField>
|
||||
<MudTextField T="string" Label="Last Name" @bind-Value="Student.LastName" For="@(() => Student.LastName)"></MudTextField>
|
||||
<MudTextField T="string" Label="Nickname" @bind-Value="Student.Nickname" For="@(() => Student.Nickname)" HelperText="Shown instead of first name on teams, rankings, and printouts"></MudTextField>
|
||||
<MudTextField T="string" Label="Email Adress" @bind-Value="Student.Email" For="@(() => Student.Email)"></MudTextField>
|
||||
<MudTextField T="string" Label="Phone Number" @bind-Value="Student.PhoneNumber" For="@(() => Student.PhoneNumber)"></MudTextField>
|
||||
<MudTextField T="int" Label="Grade" @bind-Value="Student.Grade" For="@(() => Student.Grade)"></MudTextField>
|
||||
@@ -52,6 +53,11 @@
|
||||
</MudSelect>
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="5">
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<StudentNotePanel @ref="_notePanel" StudentId="Student.Id" />
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</EditForm>
|
||||
|
||||
@@ -73,6 +79,7 @@
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
private EditContext? _editContext;
|
||||
private List<string> _validationErrors = new();
|
||||
private StudentNotePanel? _notePanel;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@@ -115,11 +122,14 @@
|
||||
if (Student?.OfficerRole == 0)
|
||||
Student.OfficerRole = null;
|
||||
|
||||
Context.Attach(Student!).State = EntityState.Modified;
|
||||
Student!.NormalizeNickname();
|
||||
Context.Attach(Student).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
{
|
||||
await Context.SaveChangesAsync();
|
||||
if (_notePanel is not null)
|
||||
await _notePanel.SaveAsync();
|
||||
Snackbar.Add($"Student '{Student!.FirstNameLastName}' saved successfully.", Severity.Success);
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo(ReturnUrl ?? "/students");
|
||||
|
||||
@@ -2,17 +2,40 @@
|
||||
@attribute [Authorize]
|
||||
@implements IAsyncDisposable
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Authentication
|
||||
@using WebApp.Models
|
||||
@using WebApp.Components.Shared.Components
|
||||
@inject AppDbContext Context
|
||||
@inject IDialogService DialogService
|
||||
@inject ISnackbar Snackbar
|
||||
@inject INotesService NotesService
|
||||
@inject IConfiguration Configuration
|
||||
@inject IJSRuntime JSRuntime
|
||||
@using Core.Notes
|
||||
@using Core.Parsers
|
||||
@using WebApp.Services
|
||||
|
||||
<PageHeader Title="Students">
|
||||
<ActionButtons>
|
||||
<MudTooltip Text="Create New">
|
||||
<MudButton StartIcon="@Icons.Material.Filled.Create" Href="students/create" Variant="Variant.Filled" Color="Color.Primary">Create New</MudButton>
|
||||
</MudTooltip>
|
||||
<AuthorizeView Roles="@AuthRoles.Administrator">
|
||||
<MudButtonGroup Variant="Variant.Outlined">
|
||||
<MudTooltip Text="Add new students from CSV. Existing names are skipped; leftover columns merge into student notes.">
|
||||
<MudButton StartIcon="@Icons.Material.Filled.UploadFile" Href="/students/import">Import</MudButton>
|
||||
</MudTooltip>
|
||||
<MudMenu Icon="@Icons.Material.Filled.ArrowDropDown"
|
||||
AriaLabel="More import actions"
|
||||
AnchorOrigin="Origin.BottomRight"
|
||||
TransformOrigin="Origin.TopRight">
|
||||
<MudMenuItem Icon="@Icons.Material.Filled.Download"
|
||||
OnClick="DownloadStudentImportTemplate">
|
||||
Download CSV template
|
||||
</MudMenuItem>
|
||||
</MudMenu>
|
||||
</MudButtonGroup>
|
||||
</AuthorizeView>
|
||||
<MudTooltip Text="Event Rankings">
|
||||
<MudButton StartIcon="@AppIcons.EventRank" Href="students/event-ranking" Variant="Variant.Outlined">Event Rankings</MudButton>
|
||||
</MudTooltip>
|
||||
@@ -24,6 +47,7 @@
|
||||
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudDataGrid T="Student"
|
||||
@key="NoteFieldColumnsKey"
|
||||
ServerData="ServerReload"
|
||||
@ref="_dataGrid"
|
||||
Filterable="true"
|
||||
@@ -43,6 +67,10 @@
|
||||
Color="Color.Primary">
|
||||
@context.Item.LastNameFirstName
|
||||
</MudLink>
|
||||
@if (!string.IsNullOrWhiteSpace(context.Item.Nickname))
|
||||
{
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary">@context.Item.Nickname</MudText>
|
||||
}
|
||||
@if (context.Item.OfficerRole != null)
|
||||
{
|
||||
<MudChip T="string" Size="Size.Small" Icon="@(AppIcons.OfficerRoleIcon(context.Item.OfficerRole.Value))">@context.Item.OfficerRole</MudChip>
|
||||
@@ -65,6 +93,15 @@
|
||||
<span style="white-space: nowrap;">@((MarkupString)AppIcons.GetOrdinalSuperscript(context.Item.Grade))</span> (@context.Item.TsaYear)
|
||||
</CellTemplate>
|
||||
</PropertyColumn>
|
||||
@foreach (var field in _noteFieldColumns)
|
||||
{
|
||||
var fieldName = field;
|
||||
<TemplateColumn Title="@fieldName" Sortable="false" Filterable="false">
|
||||
<CellTemplate>
|
||||
@GetNoteField(context.Item.Id, fieldName)
|
||||
</CellTemplate>
|
||||
</TemplateColumn>
|
||||
}
|
||||
</Columns>
|
||||
<PagerContent>
|
||||
<MudDataGridPager T="Student"></MudDataGridPager>
|
||||
@@ -77,12 +114,20 @@
|
||||
private bool _isLoading = true;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed = false;
|
||||
private List<string> _noteFieldColumns = [];
|
||||
private Dictionary<int, string?> _noteContentByStudentId = [];
|
||||
private string NoteFieldColumnsKey => string.Join('\u001f', _noteFieldColumns);
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
_noteFieldColumns = WebApp.Models.ChapterSettings.ReadIndexNoteFields(Configuration);
|
||||
}
|
||||
|
||||
private async Task<GridData<Student>> ServerReload(GridState<Student> state)
|
||||
{
|
||||
if (_isDisposed)
|
||||
@@ -104,6 +149,9 @@
|
||||
var totalItems = await query.CountAsync(cancellationToken);
|
||||
var pagedData = await query.Skip(state.Page * state.PageSize).Take(state.PageSize).ToArrayAsync(cancellationToken);
|
||||
|
||||
var notes = await NotesService.GetStudentNotesAsync(pagedData.Select(s => s.Id));
|
||||
_noteContentByStudentId = notes.ToDictionary(k => k.Key, v => v.Value.Content);
|
||||
|
||||
return new GridData<Student>
|
||||
{
|
||||
TotalItems = totalItems,
|
||||
@@ -160,6 +208,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
await NotesService.SoftDeleteStudentNotesAsync([studentToDelete.Id], cancellationToken);
|
||||
Context.Students.Remove(studentToDelete);
|
||||
await Context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
@@ -203,4 +252,42 @@
|
||||
}
|
||||
await ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task DownloadStudentImportTemplate()
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var cancellationToken = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
var fromNotes = await NotesService.GetImportedFieldNamesAsync(cancellationToken);
|
||||
var leftover = _noteFieldColumns
|
||||
.Concat(fromNotes)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase);
|
||||
var csv = StudentImportCsvTemplate.Build(leftover);
|
||||
byte[] bytes = [..System.Text.Encoding.UTF8.GetPreamble(), ..System.Text.Encoding.UTF8.GetBytes(csv)];
|
||||
var base64 = Convert.ToBase64String(bytes);
|
||||
await JSRuntime.InvokeVoidAsync("tsaDownload.fromBase64", "student-import-template.csv", "text/csv;charset=utf-8", base64);
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not download template: {ex.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private string GetNoteField(int studentId, string fieldName)
|
||||
{
|
||||
if (!_noteContentByStudentId.TryGetValue(studentId, out var content))
|
||||
return string.Empty;
|
||||
|
||||
return ImportedFieldsTable.GetFieldValue(content, fieldName) ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
@page "/students/import"
|
||||
@page "/import"
|
||||
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
||||
@implements IAsyncDisposable
|
||||
@using Core.Parsers
|
||||
@using Core.Services
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Authentication
|
||||
@inject AppDbContext Context
|
||||
@inject IStudentNotesImportService NotesImportService
|
||||
@inject IStudentNotesImportSaveService NotesSaveService
|
||||
@inject INotesService NotesService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ISnackbar Snackbar
|
||||
@inject ILogger<StudentImport> Logger
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageHeader
|
||||
Title="Import Students"
|
||||
Description="Add new students from CSV. Existing first+last names are skipped; leftover columns merge into student notes."
|
||||
ShowBackButton="true"
|
||||
BackButtonUrl="/students" />
|
||||
|
||||
<MudGrid>
|
||||
<MudItem xs="12" md="5">
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Upload CSV</MudText>
|
||||
<MudStack Spacing="3">
|
||||
<MudText Typo="Typo.body2">
|
||||
Required columns: <code>Student Name</code>, <code>Grade</code>, <code>TSA year</code>.
|
||||
Optional IDs are saved on new students. Every other column is merged into that student's notes.
|
||||
</MudText>
|
||||
<InputFile OnChange="HandleFileChanged" accept=".csv,text/csv" />
|
||||
@if (!string.IsNullOrEmpty(_fileName))
|
||||
{
|
||||
<MudText Typo="Typo.caption">@_fileName</MudText>
|
||||
}
|
||||
<MudStack Row="true" Spacing="2">
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary"
|
||||
StartIcon="@Icons.Material.Filled.Article"
|
||||
OnClick="HandleParse"
|
||||
Disabled="@(_isParsing || _fileBytes is null)">
|
||||
Parse
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Text" OnClick="HandleClear" Disabled="@_isParsing">
|
||||
Clear
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
</MudStack>
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
|
||||
<MudItem xs="12" md="7">
|
||||
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Parsed Results</MudText>
|
||||
@if (_isParsing)
|
||||
{
|
||||
<MudProgressLinear Indeterminate="true" Class="mb-4" />
|
||||
<MudText>Parsing...</MudText>
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_parseError))
|
||||
{
|
||||
<MudAlert Severity="Severity.Error">@_parseError</MudAlert>
|
||||
}
|
||||
else if (_students is null)
|
||||
{
|
||||
<MudText Class="mud-text-secondary">Upload and parse a CSV to see results here</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudStack Spacing="3">
|
||||
<MudAlert Severity="Severity.Success" Dense="true">
|
||||
@_students.Length student(s) parsed.
|
||||
@_newStudentCount new, @_existingStudentCount already in the database.
|
||||
</MudAlert>
|
||||
@if (_leftoverFieldNames.Count > 0)
|
||||
{
|
||||
<MudText Typo="Typo.body2">Leftover note fields: @string.Join(", ", _leftoverFieldNames)</MudText>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudText Typo="Typo.body2" Class="mud-text-secondary">No leftover note fields in this file.</MudText>
|
||||
}
|
||||
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Success"
|
||||
StartIcon="@Icons.Material.Filled.Save"
|
||||
OnClick="HandleSave"
|
||||
Disabled="@(_isSaving || _students.Length == 0)">
|
||||
Save to Database
|
||||
</MudButton>
|
||||
</MudStack>
|
||||
}
|
||||
</MudPaper>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
|
||||
@code {
|
||||
private byte[]? _fileBytes;
|
||||
private string? _fileName;
|
||||
private Student[]? _students;
|
||||
private List<string> _leftoverFieldNames = [];
|
||||
private int _newStudentCount;
|
||||
private int _existingStudentCount;
|
||||
private string? _parseError;
|
||||
private bool _isParsing;
|
||||
private bool _isSaving;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
private async Task HandleFileChanged(InputFileChangeEventArgs args)
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await using var stream = args.File.OpenReadStream(maxAllowedSize: 1024 * 1024);
|
||||
await using var memory = new MemoryStream();
|
||||
await stream.CopyToAsync(memory, _cancellationTokenSource?.Token ?? CancellationToken.None);
|
||||
_fileBytes = memory.ToArray();
|
||||
_fileName = args.File.Name;
|
||||
ResetParse();
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error reading student CSV");
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Could not read file: {ex.Message}", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleParse()
|
||||
{
|
||||
if (_fileBytes is null)
|
||||
{
|
||||
Snackbar.Add("Please choose a CSV file first", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
_isParsing = true;
|
||||
_parseError = null;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
using var reader = new StreamReader(new MemoryStream(_fileBytes));
|
||||
_students = new StudentParser(reader).Parse();
|
||||
_leftoverFieldNames = PeekLeftoverFieldNames(_fileBytes);
|
||||
|
||||
var existingNames = await Context.Students
|
||||
.AsNoTracking()
|
||||
.Select(s => new { s.FirstName, s.LastName })
|
||||
.ToListAsync(token);
|
||||
var existingSet = existingNames
|
||||
.Select(s => (s.FirstName, s.LastName))
|
||||
.ToHashSet();
|
||||
|
||||
_existingStudentCount = _students.Count(s => existingSet.Contains((s.FirstName, s.LastName)));
|
||||
_newStudentCount = _students.Length - _existingStudentCount;
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error parsing student CSV");
|
||||
_students = null;
|
||||
_leftoverFieldNames = [];
|
||||
_parseError = $"Error parsing CSV: {ex.Message}";
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add(_parseError, Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isParsing = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleSave()
|
||||
{
|
||||
if (_students is null || _fileBytes is null)
|
||||
{
|
||||
Snackbar.Add("Parse a CSV first", Severity.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
_isSaving = true;
|
||||
try
|
||||
{
|
||||
var token = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
var added = 0;
|
||||
foreach (var student in _students)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var exists = await Context.Students
|
||||
.FirstOrDefaultAsync(e => e.FirstName == student.FirstName && e.LastName == student.LastName, token);
|
||||
if (exists != null)
|
||||
continue;
|
||||
await Context.Students.AddAsync(student, token);
|
||||
added++;
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync(token);
|
||||
|
||||
var notesCreated = 0;
|
||||
var notesUpdated = 0;
|
||||
if (_leftoverFieldNames.Count > 0)
|
||||
{
|
||||
var students = await Context.Students
|
||||
.AsNoTracking()
|
||||
.OrderBy(s => s.LastName)
|
||||
.ThenBy(s => s.FirstName)
|
||||
.ToListAsync(token);
|
||||
var notes = await NotesService.GetStudentNotesAsync(students.Select(s => s.Id));
|
||||
var existing = notes.ToDictionary(k => k.Key, v => v.Value.Content);
|
||||
|
||||
await using var csvStream = new MemoryStream(_fileBytes, writable: false);
|
||||
var parseResult = NotesImportService.Parse(csvStream, students, existing);
|
||||
if (parseResult.IsSuccess && parseResult.StudentsWithChanges > 0)
|
||||
{
|
||||
var saveResult = await NotesSaveService.SaveAsync(parseResult, token);
|
||||
notesCreated = saveResult.NotesCreated;
|
||||
notesUpdated = saveResult.NotesUpdated;
|
||||
}
|
||||
}
|
||||
|
||||
if (_isDisposed)
|
||||
return;
|
||||
|
||||
Snackbar.Add(
|
||||
$"Added {added} student(s). Notes created {notesCreated}, updated {notesUpdated}.",
|
||||
Severity.Success);
|
||||
NavigationManager.NavigateTo("/students");
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error saving imported students");
|
||||
if (!_isDisposed)
|
||||
Snackbar.Add($"Error saving students: {ex.Message}", Severity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleClear()
|
||||
{
|
||||
_fileBytes = null;
|
||||
_fileName = null;
|
||||
ResetParse();
|
||||
}
|
||||
|
||||
private void ResetParse()
|
||||
{
|
||||
_students = null;
|
||||
_leftoverFieldNames = [];
|
||||
_newStudentCount = 0;
|
||||
_existingStudentCount = 0;
|
||||
_parseError = null;
|
||||
}
|
||||
|
||||
private static List<string> PeekLeftoverFieldNames(byte[] csvBytes)
|
||||
{
|
||||
using var reader = new StreamReader(new MemoryStream(csvBytes));
|
||||
using var parser = new StudentNotesFieldParser(reader);
|
||||
return parser.PeekLeftoverFieldNames();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_isDisposed = true;
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
}
|
||||
|
||||
await ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
Value="@SelectedCaptain"
|
||||
ValueChanged="@OnSelectedCaptainChanged"
|
||||
CheckMark>
|
||||
@foreach (var student in Students.OrderBy(e => e.FirstName))
|
||||
@foreach (var student in Students.OrderBy(e => e.DisplayFirstName))
|
||||
{
|
||||
<MudToggleItem Value="@student" Text="@student.Name" />
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@
|
||||
{
|
||||
// For individual events, use student's first name as identifier
|
||||
var student = _selectedStudents.First();
|
||||
Team.Identifier = student.FirstName;
|
||||
Team.Identifier = student.DisplayFirstName;
|
||||
Team.Captain = student;
|
||||
}
|
||||
else if (existingTeamCount == 1)
|
||||
|
||||
@@ -88,7 +88,7 @@
|
||||
{
|
||||
case EventFormat.Individual when Team.Students.Count == 1:
|
||||
Team.Captain ??= Team.Students[0];
|
||||
Team.Identifier ??= Team.Captain.FirstName;
|
||||
Team.Identifier ??= Team.Captain.DisplayFirstName;
|
||||
break;
|
||||
case EventFormat.Team:
|
||||
break;
|
||||
@@ -134,7 +134,7 @@
|
||||
if (Team is { Event.EventFormat: EventFormat.Individual, Students.Count: 1 })
|
||||
{
|
||||
Team.Captain ??= Team.Students[0];
|
||||
Team.Identifier = Team.Captain.FirstName;
|
||||
Team.Identifier = Team.Captain.DisplayFirstName;
|
||||
}
|
||||
|
||||
try
|
||||
|
||||
@@ -72,7 +72,7 @@ else
|
||||
.ThenInclude(e => e.Event)
|
||||
.Include(e => e.Teams)
|
||||
.ThenInclude(e => e.Students)
|
||||
.OrderBy(e => e.FirstName)
|
||||
.OrderBy(e => e.Nickname ?? e.FirstName)
|
||||
.ToArrayAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,6 +124,6 @@ else
|
||||
.ThenInclude(e => e.Captain)
|
||||
.Include(e => e.EventRankings)
|
||||
.ThenInclude(e => e.EventDefinition)
|
||||
.OrderBy(e => e.FirstName).ToArrayAsync();
|
||||
.OrderBy(e => e.Nickname ?? e.FirstName).ToArrayAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
@page "/settings/chapter"
|
||||
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
||||
@implements IAsyncDisposable
|
||||
@using WebApp.Authentication
|
||||
@using WebApp.Models
|
||||
@using WebApp.Components.Shared.Components
|
||||
@@ -7,12 +8,13 @@
|
||||
@using Core.Models
|
||||
@inject IConfiguration Configuration
|
||||
@inject IChapterSettingsWriter ChapterSettingsWriter
|
||||
@inject INotesService NotesService
|
||||
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageHeader
|
||||
Title="Chapter Settings"
|
||||
Description="Configure chapter information. Changes take effect on next application restart." />
|
||||
Description="Configure chapter information. Student index columns apply the next time you open Students. Printouts that cache chapter name or year may still need a restart." />
|
||||
|
||||
<MudContainer MaxWidth="MaxWidth.Large" Class="mt-4">
|
||||
|
||||
@@ -90,6 +92,41 @@
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
<MudPaper Class="pa-6 mb-4">
|
||||
<MudText Typo="Typo.h5" Class="mb-4">Student Index Columns</MudText>
|
||||
<MudText Typo="Typo.body2" Class="mb-3">
|
||||
Field names from each student's additional-fields table to show as extra Students index columns. Click a field found in notes to add or remove it, or type names (one per line).
|
||||
</MudText>
|
||||
@if (_availableFields.Count > 0)
|
||||
{
|
||||
<MudText Typo="Typo.subtitle2" Class="mb-2">Fields in student notes</MudText>
|
||||
<MudStack Row="true" Spacing="1" Wrap="Wrap.Wrap" Class="mb-4">
|
||||
@foreach (var field in _availableFields)
|
||||
{
|
||||
var selected = IsFieldSelected(field);
|
||||
<MudChip T="string"
|
||||
Size="Size.Small"
|
||||
Color="@(selected ? Color.Primary : Color.Default)"
|
||||
Variant="@(selected ? Variant.Filled : Variant.Outlined)"
|
||||
OnClick="() => ToggleField(field)">
|
||||
@field
|
||||
</MudChip>
|
||||
}
|
||||
</MudStack>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudText Typo="Typo.caption" Class="mud-text-secondary mb-3">
|
||||
No additional fields found in student notes yet.
|
||||
</MudText>
|
||||
}
|
||||
<MudTextField @bind-Value="_noteFieldsText"
|
||||
Label="Additional field columns"
|
||||
Variant="Variant.Outlined"
|
||||
Lines="5"
|
||||
HelperText="Example: Interview Time" />
|
||||
</MudPaper>
|
||||
|
||||
<MudPaper Class="pa-6">
|
||||
<MudGrid>
|
||||
<MudItem xs="12">
|
||||
@@ -125,14 +162,52 @@
|
||||
|
||||
@code {
|
||||
private Models.ChapterSettings? _settings;
|
||||
private string _noteFieldsText = string.Empty;
|
||||
private IReadOnlyList<string> _availableFields = [];
|
||||
private bool _isSaving;
|
||||
private string? _statusMessage;
|
||||
private Severity _statusSeverity = Severity.Success;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed;
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
_settings = Configuration.GetSection("ChapterSettings").Get<Models.ChapterSettings>()
|
||||
?? new Models.ChapterSettings();
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
_settings = Models.ChapterSettings.FromConfiguration(Configuration);
|
||||
_noteFieldsText = string.Join(Environment.NewLine, _settings.StudentIndexNoteFields);
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var cancellationToken = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
_availableFields = await NotesService.GetImportedFieldNamesAsync(cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<string> SelectedFields() =>
|
||||
_noteFieldsText
|
||||
.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||
.ToList();
|
||||
|
||||
private bool IsFieldSelected(string field) =>
|
||||
SelectedFields().Any(selected => selected.Equals(field, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
private void ToggleField(string field)
|
||||
{
|
||||
var selected = SelectedFields().ToList();
|
||||
var index = selected.FindIndex(name => name.Equals(field, StringComparison.OrdinalIgnoreCase));
|
||||
if (index >= 0)
|
||||
selected.RemoveAt(index);
|
||||
else
|
||||
selected.Add(field);
|
||||
|
||||
_noteFieldsText = string.Join(Environment.NewLine, selected);
|
||||
}
|
||||
|
||||
private async Task SaveSettings()
|
||||
@@ -144,18 +219,41 @@
|
||||
|
||||
try
|
||||
{
|
||||
await ChapterSettingsWriter.WriteAsync(_settings);
|
||||
_statusMessage = "Settings saved successfully! Changes will take effect on next application restart.";
|
||||
_settings.StudentIndexNoteFields = [.. SelectedFields()];
|
||||
var cancellationToken = _cancellationTokenSource?.Token ?? CancellationToken.None;
|
||||
await ChapterSettingsWriter.WriteAsync(_settings, cancellationToken);
|
||||
if (_isDisposed)
|
||||
return;
|
||||
_statusMessage = "Settings saved. Open Students again to see the updated index columns.";
|
||||
_statusSeverity = Severity.Success;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_isDisposed)
|
||||
return;
|
||||
_statusMessage = $"Error saving settings: {ex.Message}";
|
||||
_statusSeverity = Severity.Error;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_isDisposed)
|
||||
_isSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_isDisposed = true;
|
||||
_cancellationTokenSource?.Cancel();
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
}
|
||||
|
||||
await ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
@page "/import"
|
||||
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
||||
@using Core.Parsers
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Authentication
|
||||
@inject AppDbContext Context
|
||||
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<PageTitle>Import Data</PageTitle>
|
||||
|
||||
<h1>Import Data</h1>
|
||||
|
||||
<h3>Events</h3>
|
||||
<InputFile OnChange="UploadEvents"></InputFile>
|
||||
<text>@_events?.Length Events</text>
|
||||
<button class="btn btn-primary" @onclick="SaveEvents">Save to Database</button>
|
||||
<br/>
|
||||
|
||||
<h3>Students</h3>
|
||||
<InputFile OnChange="UploadStudents"></InputFile>
|
||||
<text>@_students?.Length Students</text>
|
||||
<button class="btn btn-primary" @onclick="SaveStudents">Save to Database</button>
|
||||
|
||||
@code {
|
||||
private EventDefinition[]? _events;
|
||||
private Student[]? _students;
|
||||
|
||||
async Task UploadEvents(InputFileChangeEventArgs arg)
|
||||
{
|
||||
await GetStreamReaderFromInputFile(arg, reader =>
|
||||
{
|
||||
var eventDefinitionParser = new EventDefinitionParser(reader);
|
||||
_events = eventDefinitionParser.Parse();
|
||||
});
|
||||
}
|
||||
|
||||
async Task SaveEvents()
|
||||
{
|
||||
if (_events == null)
|
||||
return;
|
||||
|
||||
foreach (var evt in _events)
|
||||
{
|
||||
// check if it already exists
|
||||
var exists
|
||||
= await Context.Events
|
||||
.FirstOrDefaultAsync(e => e.Name == evt.Name);
|
||||
if (exists != null)
|
||||
continue;
|
||||
await Context.Events.AddAsync(evt);
|
||||
}
|
||||
|
||||
await Context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
async Task UploadStudents(InputFileChangeEventArgs arg)
|
||||
{
|
||||
await GetStreamReaderFromInputFile(arg, reader =>
|
||||
{
|
||||
var studentParser = new StudentParser(reader);
|
||||
_students = studentParser.Parse();
|
||||
});
|
||||
}
|
||||
|
||||
async Task SaveStudents()
|
||||
{
|
||||
if (_students == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var student in _students)
|
||||
{
|
||||
// check if it already exists
|
||||
var exists
|
||||
= await Context.Students
|
||||
.FirstOrDefaultAsync(e
|
||||
=> e.FirstName == student.FirstName
|
||||
&& e.LastName == student.LastName);
|
||||
if (exists != null)
|
||||
continue;
|
||||
await Context.Students.AddAsync(student);
|
||||
}
|
||||
await Context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
static async Task GetStreamReaderFromInputFile(InputFileChangeEventArgs arg, Action<StreamReader> f)
|
||||
{
|
||||
StreamReader? streamReader = null;
|
||||
try
|
||||
{
|
||||
var browserFile = arg.File;
|
||||
|
||||
await using var fs = browserFile.OpenReadStream();
|
||||
await using var ms = new MemoryStream();
|
||||
|
||||
await fs.CopyToAsync(ms);
|
||||
ms.Seek(0,0);
|
||||
streamReader = new StreamReader(ms);
|
||||
f(streamReader);
|
||||
}
|
||||
catch
|
||||
{
|
||||
streamReader?.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,14 @@
|
||||
@using WebApp.Models
|
||||
@using Core.Printing
|
||||
@using WebApp.Models
|
||||
<MudPaper>
|
||||
<h3>Legend</h3>
|
||||
|
||||
<MudContainer>
|
||||
<ul>
|
||||
<li>@AppIcons.LevelOfEffortIcon(1) - Level of Effort </li>
|
||||
<li>@AppIcons.IndividualEvent - Individual Event </li>
|
||||
<li>@AppIcons.RegionalEvent - Regional </li>
|
||||
<li>@AppIcons.OnSiteActivity - On-site Activity</li>
|
||||
<li>@AppIcons.PresubmissionEvent - Pre-submission</li>
|
||||
<li>@AppIcons.PresentationEvent - Interview Or Presentation</li>
|
||||
@foreach (var mark in EventAttributeMarks.LegendItems)
|
||||
{
|
||||
<li>@mark.Symbol - @mark.Label</li>
|
||||
}
|
||||
</ul>
|
||||
</MudContainer>
|
||||
</MudPaper>
|
||||
@@ -80,7 +80,7 @@
|
||||
{
|
||||
_history = (await NotesService.GetNoteHistoryAsync(NoteId)).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
// Error handling - could show snackbar if we had access
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
var isExpanded = GetNoteExpanded(noteId);
|
||||
<MudExpansionPanel @key="@($"note-{noteId}")"
|
||||
Icon="@Icons.Material.Filled.Note"
|
||||
IsExpanded="@isExpanded"
|
||||
Expanded="@isExpanded"
|
||||
ExpandedChanged="@((bool expanded) => OnPanelExpandedChanged(noteId, expanded))"
|
||||
Class="@MarkdownHelper.GetNoteColorClass(noteId)">
|
||||
<TitleContent>
|
||||
@@ -71,15 +71,16 @@
|
||||
</MudText>
|
||||
@if (!NoteNamingService.IsPageNote(note.Title) && !note.IsDeleted && !isExpanded)
|
||||
{
|
||||
<div @onclick:stopPropagation="true" class="flex-shrink-0">
|
||||
<MudTooltip Text="@(note.IsPinned ? "Unpin note" : "Pin note")">
|
||||
<MudButton StartIcon="@(note.IsPinned ? Icons.Material.Filled.PushPin : Icons.Material.Outlined.PushPin)"
|
||||
OnClick="() => TogglePin(note)"
|
||||
OnClick:StopPropagation="true"
|
||||
Variant="Variant.Text"
|
||||
Size="Size.Small"
|
||||
Color="@(note.IsPinned ? Color.Primary : Color.Default)"
|
||||
Disabled="@(IsPinDisabled(note))"
|
||||
Title="@(note.IsPinned ? "Unpin note" : "Pin note")"
|
||||
Class="flex-shrink-0" />
|
||||
Disabled="@(IsPinDisabled(note))" />
|
||||
</MudTooltip>
|
||||
</div>
|
||||
}
|
||||
</MudStack>
|
||||
</TitleContent>
|
||||
@@ -301,7 +302,7 @@
|
||||
var dialog = await DialogService.ShowAsync<NoteEditDialog>("Create Note", parameters, GetDefaultDialogOptions());
|
||||
var result = await dialog.Result;
|
||||
|
||||
if (!result.Canceled && !_isDisposed)
|
||||
if (result is { Canceled: false } && !_isDisposed)
|
||||
{
|
||||
await LoadNotes();
|
||||
}
|
||||
@@ -327,7 +328,7 @@
|
||||
var dialog = await DialogService.ShowAsync<NoteEditDialog>("Edit Note", parameters, options);
|
||||
var result = await dialog.Result;
|
||||
|
||||
if (!result.Canceled && !_isDisposed)
|
||||
if (result is { Canceled: false } && !_isDisposed)
|
||||
{
|
||||
await LoadNotes();
|
||||
}
|
||||
|
||||
@@ -3,28 +3,26 @@
|
||||
@inject IDialogService DialogService
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<div @ref="_anchorElement"
|
||||
@onmouseenter="@(() => { if (_hasContent) _popoverOpen = true; })"
|
||||
<div @onmouseenter="@(() => { if (_hasContent) _popoverOpen = true; })"
|
||||
@onmouseleave="@(() => _popoverOpen = false)"
|
||||
style="display: inline-block;">
|
||||
style="display: inline-block; position: relative;">
|
||||
<MudTooltip Text="@TooltipText">
|
||||
<MudButton StartIcon="@IconValue"
|
||||
OnClick="OpenDialog"
|
||||
Variant="@Variant"
|
||||
Size="@Size"
|
||||
Color="@ButtonColor"
|
||||
Tooltip="@TooltipText"
|
||||
Class="@MarkdownHelper.GetNoteColorClass(_noteId)">
|
||||
@if (!string.IsNullOrEmpty(ButtonText))
|
||||
{
|
||||
@ButtonText
|
||||
}
|
||||
</MudButton>
|
||||
</div>
|
||||
<MudPopover @bind-Open="_popoverOpen"
|
||||
</MudTooltip>
|
||||
<MudPopover Open="_popoverOpen"
|
||||
AnchorOrigin="Origin.BottomCenter"
|
||||
TransformOrigin="Origin.TopCenter"
|
||||
Elevation="8"
|
||||
Anchor="@_anchorElement">
|
||||
Elevation="8">
|
||||
<ChildContent>
|
||||
@if (_hasContent && !string.IsNullOrWhiteSpace(_noteContent))
|
||||
{
|
||||
@@ -42,6 +40,7 @@
|
||||
}
|
||||
</ChildContent>
|
||||
</MudPopover>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
@@ -68,7 +67,6 @@
|
||||
private int _noteId = 0;
|
||||
private string _noteContent = string.Empty;
|
||||
private bool _popoverOpen = false;
|
||||
private ElementReference _anchorElement;
|
||||
private Color ButtonColor => _hasContent ? Color.Success : (Variant == Variant.Filled ? Color.Primary : Color.Default);
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
private bool _isDisposed = false;
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
<MudNavGroup Title="Tools" Icon="@Icons.Material.Filled.Build" Expanded="false">
|
||||
<MudNavLink Href="/notes" Icon="@Icons.Material.Filled.Note">Notes</MudNavLink>
|
||||
<MudNavLink Href="/print" Icon="@Icons.Material.Filled.Print">Page printer</MudNavLink>
|
||||
</MudNavGroup>
|
||||
|
||||
<AuthorizeView Roles="Administrator">
|
||||
|
||||
+24
-91
@@ -1,4 +1,5 @@
|
||||
using Core.Entities;
|
||||
using Core.Printing;
|
||||
using MudBlazor;
|
||||
|
||||
namespace WebApp.Models
|
||||
@@ -14,50 +15,26 @@ namespace WebApp.Models
|
||||
public static string Captain = Icons.Material.Filled.Star;
|
||||
public static string Registration = Icons.Material.Filled.AppRegistration;
|
||||
public static string EventCalendar = Icons.Material.Filled.Event;
|
||||
public static string LevelOfEffortIcon(int? loe)
|
||||
public static string LevelOfEffortIcon(int? loe) =>
|
||||
loe switch
|
||||
{
|
||||
|
||||
return loe switch
|
||||
{
|
||||
1 => "○",
|
||||
2 => "◐",
|
||||
3 => "⬤",
|
||||
1 => EventAttributeMarks.LevelOfEffort1,
|
||||
2 => EventAttributeMarks.LevelOfEffort2,
|
||||
3 => EventAttributeMarks.LevelOfEffort3,
|
||||
_ => Icons.Material.Filled.QuestionMark
|
||||
};
|
||||
}
|
||||
|
||||
/*https://unicodeplus.com/search*/
|
||||
public static string OnSiteActivity = "ⓐ";
|
||||
public static string RegionalEvent = "ⓡ";
|
||||
public static string IndividualEvent = "ⓘ";
|
||||
public static string PresubmissionEvent = "ⓟ";
|
||||
public static string PresentationEvent = "";
|
||||
public static string OnSiteActivity => EventAttributeMarks.OnSite;
|
||||
public static string RegionalEvent => EventAttributeMarks.Regional;
|
||||
public static string IndividualEvent => EventAttributeMarks.Individual;
|
||||
public static string PresubmissionEvent => EventAttributeMarks.Presubmission;
|
||||
public static string PresentationEvent => "";
|
||||
|
||||
// Tooltip mapping for icon unicode characters
|
||||
public static Dictionary<string, string> IconTooltips => new()
|
||||
{
|
||||
{ OnSiteActivity, "On-Site Activity" },
|
||||
{ RegionalEvent, "Regional Event" },
|
||||
{ IndividualEvent, "Individual Event" },
|
||||
{ PresubmissionEvent, "Presubmission" },
|
||||
{ PresentationEvent, "Presentation/Interview" },
|
||||
{ "○", "Level of Effort: 1" },
|
||||
{ "◐", "Level of Effort: 2" },
|
||||
{ "●", "Level of Effort: 3" }
|
||||
};
|
||||
public static IReadOnlyDictionary<string, string> IconTooltips { get; } =
|
||||
EventAttributeMarks.LegendItems.ToDictionary(m => m.Symbol, m => m.Label, StringComparer.Ordinal);
|
||||
|
||||
// Color mapping for icon unicode characters
|
||||
public static Dictionary<string, string> IconColors => new()
|
||||
{
|
||||
{ OnSiteActivity, "#ff9800" }, // Orange
|
||||
{ RegionalEvent, "#2196f3" }, // Blue
|
||||
{ IndividualEvent, "#9c27b0" }, // Purple
|
||||
{ PresubmissionEvent, "#4caf50" }, // Green
|
||||
{ PresentationEvent, "#f44336" }, // Red
|
||||
{ "○", "#757575" }, // Gray
|
||||
{ "◐", "#616161" }, // Darker Gray
|
||||
{ "●", "#424242" } // Even Darker Gray
|
||||
};
|
||||
public static IReadOnlyDictionary<string, string> IconColors { get; } =
|
||||
EventAttributeMarks.LegendItems.ToDictionary(m => m.Symbol, m => m.Color, StringComparer.Ordinal);
|
||||
|
||||
public static string EventEffort(EventDefinition eventDefinition)
|
||||
{
|
||||
@@ -96,63 +73,19 @@ namespace WebApp.Models
|
||||
};
|
||||
}
|
||||
|
||||
public static string RankedEventColor(int rank)
|
||||
{
|
||||
return rank switch
|
||||
{
|
||||
1 => "#dd7e6b",
|
||||
2 => "#ea9999",
|
||||
3 => "#f9cb9c",
|
||||
4 => "#ffe599",
|
||||
5 => "#fff2cc",
|
||||
6 => "#fffaea",
|
||||
7 => "#fffefa",
|
||||
8 => "#fffefc",
|
||||
9 => "#fffffd",
|
||||
10 => "#fffffe",
|
||||
_ => "#ddd"
|
||||
};
|
||||
}
|
||||
public static string RankedEventColor(int rank) => EventRankLegend.ColorHex(rank);
|
||||
|
||||
public static string GetOrdinal(int num)
|
||||
{
|
||||
if (num <= 0) return num.ToString();
|
||||
|
||||
switch (num % 100)
|
||||
{
|
||||
case 11:
|
||||
case 12:
|
||||
case 13:
|
||||
return num + "th";
|
||||
}
|
||||
|
||||
switch (num % 10)
|
||||
{
|
||||
case 1:
|
||||
return num + "st";
|
||||
case 2:
|
||||
return num + "nd";
|
||||
case 3:
|
||||
return num + "rd";
|
||||
default:
|
||||
return num + "th";
|
||||
}
|
||||
}
|
||||
public static string GetOrdinal(int num) => EventRankLegend.Ordinal(num);
|
||||
|
||||
public static string GetOrdinalSuperscript(int number)
|
||||
{
|
||||
var suffix = number switch
|
||||
{
|
||||
11 or 12 or 13 => "th",
|
||||
_ => (number % 10) switch
|
||||
{
|
||||
1 => "st",
|
||||
2 => "nd",
|
||||
3 => "rd",
|
||||
_ => "th"
|
||||
}
|
||||
};
|
||||
return $"{number}<sup>{suffix}</sup>";
|
||||
var ordinal = EventRankLegend.Ordinal(number);
|
||||
var suffixAt = 0;
|
||||
while (suffixAt < ordinal.Length && (char.IsDigit(ordinal[suffixAt]) || ordinal[suffixAt] == '-'))
|
||||
suffixAt++;
|
||||
return suffixAt is 0 || suffixAt == ordinal.Length
|
||||
? ordinal
|
||||
: $"{ordinal[..suffixAt]}<sup>{ordinal[suffixAt..]}</sup>";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Core.Models;
|
||||
using Core.Notes;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace WebApp.Models;
|
||||
|
||||
@@ -51,4 +53,26 @@ public class ChapterSettings
|
||||
/// School level for the chapter (null = import both MS and HS events)
|
||||
/// </summary>
|
||||
public SchoolLevel? SchoolLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Field names from student note <c>## Additional fields</c> tables to show as Students index columns.
|
||||
/// </summary>
|
||||
public List<string> StudentIndexNoteFields { get; set; } = [.. StudentNoteFieldDefaults.IndexColumns];
|
||||
|
||||
public static ChapterSettings FromConfiguration(IConfiguration configuration)
|
||||
{
|
||||
var settings = configuration.GetSection("ChapterSettings").Get<ChapterSettings>()
|
||||
?? new ChapterSettings();
|
||||
settings.StudentIndexNoteFields = ReadIndexNoteFields(configuration);
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static List<string> ReadIndexNoteFields(IConfiguration configuration)
|
||||
{
|
||||
var fields = configuration.GetSection("ChapterSettings:StudentIndexNoteFields").Get<List<string>>();
|
||||
if (fields is null)
|
||||
return [.. StudentNoteFieldDefaults.IndexColumns];
|
||||
|
||||
return [.. fields.Where(f => !string.IsNullOrWhiteSpace(f)).Select(f => f.Trim())];
|
||||
}
|
||||
}
|
||||
|
||||
+14
-5
@@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
|
||||
using MudBlazor.Services;
|
||||
using Serilog;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Core.Notes;
|
||||
using VisNetwork.Blazor;
|
||||
using WebApp;
|
||||
using WebApp.Authentication;
|
||||
@@ -34,21 +36,24 @@ if (!File.Exists(dataAppSettingsPath))
|
||||
var baseConfig = File.ReadAllText(baseAppSettingsPath);
|
||||
var baseDoc = JsonDocument.Parse(baseConfig);
|
||||
|
||||
var templateSettings = new Dictionary<string, object?>();
|
||||
JsonObject templateSettings = [];
|
||||
|
||||
if (baseDoc.RootElement.TryGetProperty("ChapterSettings", out var chapterSettings))
|
||||
{
|
||||
templateSettings["ChapterSettings"] = JsonSerializer.Deserialize<object>(chapterSettings.GetRawText());
|
||||
var chapterObj = JsonNode.Parse(chapterSettings.GetRawText()) as JsonObject ?? [];
|
||||
if (chapterObj["StudentIndexNoteFields"] is null)
|
||||
chapterObj["StudentIndexNoteFields"] = JsonSerializer.SerializeToNode(StudentNoteFieldDefaults.IndexColumns);
|
||||
templateSettings["ChapterSettings"] = chapterObj;
|
||||
}
|
||||
|
||||
if (baseDoc.RootElement.TryGetProperty("ValidationSettings", out var validationSettings))
|
||||
{
|
||||
templateSettings["ValidationSettings"] = JsonSerializer.Deserialize<object>(validationSettings.GetRawText());
|
||||
templateSettings["ValidationSettings"] = JsonNode.Parse(validationSettings.GetRawText());
|
||||
}
|
||||
|
||||
if (templateSettings.Any())
|
||||
if (templateSettings.Count > 0)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(templateSettings, new JsonSerializerOptions
|
||||
var json = templateSettings.ToJsonString(new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true
|
||||
});
|
||||
@@ -206,6 +211,10 @@ builder.Services.AddScoped<WebApp.Services.IDatabaseBackupService, WebApp.Servic
|
||||
builder.Services.AddScoped<WebApp.Services.IYearRolloverService, WebApp.Services.YearRolloverService>();
|
||||
builder.Services.AddScoped<Core.Services.IStudentEventRankingImportService, Core.Services.StudentEventRankingImportService>();
|
||||
builder.Services.AddScoped<WebApp.Services.IStudentEventRankingSaveService, WebApp.Services.StudentEventRankingSaveService>();
|
||||
builder.Services.AddScoped<Core.Services.IStudentNotesImportService, Core.Services.StudentNotesImportService>();
|
||||
builder.Services.AddScoped<WebApp.Services.IStudentNotesImportSaveService, WebApp.Services.StudentNotesImportSaveService>();
|
||||
builder.Services.AddScoped<WebApp.Services.INotePrintService, WebApp.Services.NotePrintService>();
|
||||
builder.Services.AddScoped<WebApp.Services.IPrintPresetService, WebApp.Services.PrintPresetService>();
|
||||
|
||||
builder.Services.Configure<StateScheduleHandoutOptions>(
|
||||
builder.Configuration.GetSection(StateScheduleHandoutOptions.SectionName));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using WebApp.Models;
|
||||
|
||||
namespace WebApp.Services;
|
||||
@@ -8,6 +9,11 @@ namespace WebApp.Services;
|
||||
/// </summary>
|
||||
public class ChapterSettingsWriter : IChapterSettingsWriter
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
private readonly IWebHostEnvironment _environment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ChapterSettingsWriter> _logger;
|
||||
@@ -29,35 +35,41 @@ public class ChapterSettingsWriter : IChapterSettingsWriter
|
||||
var appSettingsPath = GetAppSettingsPath();
|
||||
var dataDir = Path.GetDirectoryName(appSettingsPath);
|
||||
if (dataDir != null && !Directory.Exists(dataDir))
|
||||
{
|
||||
Directory.CreateDirectory(dataDir);
|
||||
}
|
||||
|
||||
Dictionary<string, object?> root;
|
||||
JsonObject root;
|
||||
if (File.Exists(appSettingsPath))
|
||||
{
|
||||
var existingJson = await File.ReadAllTextAsync(appSettingsPath, cancellationToken);
|
||||
root = JsonSerializer.Deserialize<Dictionary<string, object?>>(existingJson)
|
||||
?? [];
|
||||
root = JsonNode.Parse(existingJson) as JsonObject ?? [];
|
||||
}
|
||||
else
|
||||
{
|
||||
root = [];
|
||||
}
|
||||
|
||||
root["ChapterSettings"] = settings;
|
||||
var incoming = JsonSerializer.SerializeToNode(settings, JsonOptions) as JsonObject ?? [];
|
||||
if (root["ChapterSettings"] is JsonObject existing)
|
||||
{
|
||||
foreach (var property in incoming)
|
||||
existing[property.Key] = property.Value?.DeepClone();
|
||||
}
|
||||
else
|
||||
{
|
||||
root["ChapterSettings"] = incoming;
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions { WriteIndented = true };
|
||||
var json = JsonSerializer.Serialize(root, options);
|
||||
await File.WriteAllTextAsync(appSettingsPath, json, cancellationToken);
|
||||
await File.WriteAllTextAsync(appSettingsPath, root.ToJsonString(JsonOptions), cancellationToken);
|
||||
|
||||
if (_configuration is IConfigurationRoot configurationRoot)
|
||||
configurationRoot.Reload();
|
||||
|
||||
_logger.LogInformation("Chapter settings written to {Path}", appSettingsPath);
|
||||
}
|
||||
|
||||
public async Task UpdateCompetitionYearAsync(string competitionYear, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var settings = _configuration.GetSection("ChapterSettings").Get<ChapterSettings>()
|
||||
?? new ChapterSettings();
|
||||
var settings = ChapterSettings.FromConfiguration(_configuration);
|
||||
settings.CompetitionYear = competitionYear;
|
||||
await WriteAsync(settings, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using Core.Printing;
|
||||
|
||||
namespace WebApp.Services;
|
||||
|
||||
public class NotePrintRequest
|
||||
{
|
||||
public required PrintEntityType EntityType { get; init; }
|
||||
|
||||
public required string TemplateMarkdown { get; init; }
|
||||
|
||||
public required PrintPresetFilters Filters { get; init; }
|
||||
|
||||
public required IReadOnlyList<string> ImportedFieldCatalog { get; init; }
|
||||
}
|
||||
|
||||
public class NotePrintPage
|
||||
{
|
||||
public required string DisplayName { get; init; }
|
||||
|
||||
public required string Html { get; init; }
|
||||
}
|
||||
|
||||
public interface INotePrintService
|
||||
{
|
||||
Task<IReadOnlyList<NotePrintPage>> PreviewAsync(
|
||||
NotePrintRequest request,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -22,6 +22,26 @@ public interface INotesService
|
||||
/// <returns>The note with title "#{pageIdentifier}" or null if not found</returns>
|
||||
Task<Note?> GetPageNoteAsync(string pageIdentifier);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the system note for a student, or null if none exists.
|
||||
/// </summary>
|
||||
Task<Note?> GetStudentNoteAsync(int studentId);
|
||||
|
||||
/// <summary>
|
||||
/// Gets system notes for the given student ids, keyed by student id.
|
||||
/// </summary>
|
||||
Task<IReadOnlyDictionary<int, Note>> GetStudentNotesAsync(IEnumerable<int> studentIds);
|
||||
|
||||
/// <summary>
|
||||
/// Soft-deletes system notes for the given student ids.
|
||||
/// </summary>
|
||||
Task SoftDeleteStudentNotesAsync(IEnumerable<int> studentIds, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Distinct Field names from student note <c>## Additional fields</c> tables.
|
||||
/// </summary>
|
||||
Task<IReadOnlyList<string>> GetImportedFieldNamesAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all history entries for a note.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using Core.Entities;
|
||||
|
||||
namespace WebApp.Services;
|
||||
|
||||
public interface IPrintPresetService
|
||||
{
|
||||
Task<IReadOnlyList<PrintPreset>> GetAllAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
Task<PrintPreset?> GetAsync(int id, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<bool> NameExistsAsync(string name, int? excludeId = null, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<PrintPreset> CreateAsync(PrintPreset preset, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<PrintPreset> UpdateAsync(PrintPreset preset, CancellationToken cancellationToken = default);
|
||||
|
||||
Task DeleteAsync(int id, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Core.Models;
|
||||
|
||||
namespace WebApp.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Persists merged student field notes.
|
||||
/// </summary>
|
||||
public interface IStudentNotesImportSaveService
|
||||
{
|
||||
Task<StudentNotesImportSaveResult> SaveAsync(
|
||||
StudentNotesImportResult parseResult,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public class StudentNotesImportSaveResult
|
||||
{
|
||||
public int NotesCreated { get; set; }
|
||||
|
||||
public int NotesUpdated { get; set; }
|
||||
|
||||
public int StudentsUnchanged { get; set; }
|
||||
}
|
||||
@@ -38,4 +38,55 @@ public class MarkdownTablePasteService
|
||||
_logger.LogError(ex, "Unexpected error initializing paste-markdown for editor {EditorId}", editorId ?? "unknown");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string?> GetValueAsync(string editorId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<string?>("markdownTablePaste.getValue", editorId);
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
catch (JSException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to read markdown editor {EditorId}", editorId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> SetValueAsync(string editorId, string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<bool>("markdownTablePaste.setValue", editorId, text);
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (JSException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to set markdown editor {EditorId}", editorId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> InsertAtCursorAsync(string editorId, string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<bool>("markdownTablePaste.insertAtCursor", editorId, text);
|
||||
}
|
||||
catch (JSDisconnectedException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (JSException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to insert into markdown editor {EditorId}", editorId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ public class MeetingScheduleDataService : IMeetingScheduleDataService
|
||||
.ThenInclude(t => t.Captain)
|
||||
.Include(e => e.EventRankings)
|
||||
.ThenInclude(e => e.EventDefinition)
|
||||
.OrderBy(e => e.FirstName)
|
||||
.OrderBy(e => e.Nickname ?? e.FirstName)
|
||||
.ToArrayAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user