Enhance school simulation and management by integrating roster functionality, allowing for the installation and persistence of student and staff data. Update the school architecture to include a roster alongside existing components, ensuring proper validation against map layouts. Revise room definitions to support homeroom designations and update related tests to validate new functionalities and ensure robustness in roster handling.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Text.Json;
|
||||
using HSchool.Content;
|
||||
using HSchool.People;
|
||||
using HSchool.Simulation;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -101,7 +102,9 @@ internal sealed class SchoolStore
|
||||
|
||||
foreach (var path in Directory.EnumerateFiles(DirectoryPath, "*.json"))
|
||||
{
|
||||
if (string.Equals(Path.GetFileName(path), IndexFileName, StringComparison.OrdinalIgnoreCase))
|
||||
var fileName = Path.GetFileName(path);
|
||||
if (string.Equals(fileName, IndexFileName, StringComparison.OrdinalIgnoreCase)
|
||||
|| fileName.EndsWith(".people.json", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -179,15 +182,48 @@ internal sealed class SchoolStore
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
var people = PeoplePath(id);
|
||||
if (File.Exists(people))
|
||||
{
|
||||
File.Delete(people);
|
||||
}
|
||||
}
|
||||
|
||||
public RosterDocument? TryReadPeople(int id)
|
||||
{
|
||||
var path = PeoplePath(id);
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return RosterJson.Parse(File.ReadAllText(path));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new SchoolContentUnavailableException(
|
||||
$"School {id} people file could not be read.",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void SavePeople(int id, RosterDocument document)
|
||||
{
|
||||
WriteAtomic(PeoplePath(id), document, RosterJson.Options);
|
||||
}
|
||||
|
||||
private string SchoolPath(int id) => Path.Combine(DirectoryPath, $"{id}.json");
|
||||
|
||||
private string PeoplePath(int id) => Path.Combine(DirectoryPath, $"{id}.people.json");
|
||||
|
||||
private string IndexPath() => Path.Combine(DirectoryPath, IndexFileName);
|
||||
|
||||
private static void WriteAtomic<T>(string path, T value)
|
||||
private static void WriteAtomic<T>(string path, T value, JsonSerializerOptions? options = null)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(value, Json);
|
||||
var json = JsonSerializer.Serialize(value, options ?? Json);
|
||||
var temp = path + ".tmp";
|
||||
File.WriteAllText(temp, json);
|
||||
File.Move(temp, path, overwrite: true);
|
||||
|
||||
Reference in New Issue
Block a user