-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModels.cs
39 lines (31 loc) · 1.1 KB
/
Models.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Models used by the students "database".
using System.Text.Json;
namespace NestedCommands;
internal record class Student(string FirstName, string LastName, string? Major, List<StudentCourse> Courses);
internal record class Course(string Name, string Teacher);
internal record class StudentCourse(int CourseId, float Grade);
internal record class Database(Dictionary<int, Student> Students, Dictionary<int, Course> Courses)
{
public static async Task<Database> Load(string path)
{
Database? result = null;
try
{
using var stream = File.OpenRead(path);
result = await JsonSerializer.DeserializeAsync<Database>(stream);
}
catch (FileNotFoundException)
{
}
return result ?? new Database(new(), new());
}
public async Task Save(string path)
{
using var stream = File.Create(path);
var options = new JsonSerializerOptions()
{
WriteIndented = true,
};
await JsonSerializer.SerializeAsync(stream, this, options);
}
}