From 67d7890c38e9b3a8d6b0c85af76b2216fe4faaa5 Mon Sep 17 00:00:00 2001 From: Artem Dadashiants Date: Wed, 6 Nov 2024 00:08:29 +0200 Subject: [PATCH] fix: one teacher for all lessons --- Nure.NET.csproj | 4 +- Parsers/NureParser.cs | 168 ++++++++++++++++++++++++------------------ 2 files changed, 97 insertions(+), 75 deletions(-) diff --git a/Nure.NET.csproj b/Nure.NET.csproj index 5927935..faaf90b 100644 --- a/Nure.NET.csproj +++ b/Nure.NET.csproj @@ -8,8 +8,8 @@ GPL-3.0-or-later https://github.com/mindenit/Nure.NET README.md - 0.9.8 - 0.9.8 + 1.0.0 + 1.0.0 diff --git a/Parsers/NureParser.cs b/Parsers/NureParser.cs index d4a6412..420539a 100644 --- a/Parsers/NureParser.cs +++ b/Parsers/NureParser.cs @@ -44,7 +44,7 @@ private static string GetType(int? id) var json = Requests.GetAuditoriesJson(); var cistAuditories = JsonSerializer.Deserialize(json); - + if (cistAuditories.TryGetProperty("university", out var university) && university.TryGetProperty("buildings", out var buildings)) { @@ -207,121 +207,144 @@ private static List RemoveDuplicateTeachers(List? deserialize) .ToList(); } - private static Subject? FindSubjectById(JsonElement subjects, int? id) + private static Subject? FindSubjectById(JsonArray subjects, int? id) { - foreach (var subject in subjects.EnumerateArray()) + foreach (var subject in subjects) { - if (subject.GetProperty("id").GetInt32() == id) + if (subject is JsonObject subjectObj && + subjectObj["id"]?.GetValue() == id) { - return new Subject + return new Subject() { - Id = subject.GetProperty("id").GetInt32(), - Brief = subject.GetProperty("brief").GetString(), - Title = subject.GetProperty("title").GetString() + Id = subjectObj["id"]!.GetValue(), + Brief = subjectObj["brief"]?.GetValue(), + Title = subjectObj["title"]?.GetValue() }; } } - return null; // Return null if no subject with the given identifier is found + return null; // якщо предмет з таким ідентифікатором не знайдено } - private static Teacher? FindTeacherById(JsonElement teachers, int? id) + private static Teacher? FindTeacherById(JsonArray teachers, int? id) { - foreach (var teacher in teachers.EnumerateArray()) + foreach (var teacher in teachers) { - if (teacher.TryGetProperty("id", out var idProperty) && - teacher.TryGetProperty("short_name", out var shortNameProperty) && - teacher.TryGetProperty("full_name", out var fullNameProperty)) + if (teacher is JsonObject teacherObj && + teacherObj["id"]?.GetValue() == id && + teacherObj["id"]?.GetValue() is not null && + teacherObj["short_name"] is not null && + teacherObj["full_name"] is not null) { - if (idProperty.ValueKind == JsonValueKind.Number && - shortNameProperty.ValueKind == JsonValueKind.String && - fullNameProperty.ValueKind == JsonValueKind.String) + return new Teacher() { - return new Teacher - { - Id = idProperty.GetInt64(), - ShortName = shortNameProperty.GetString(), - FullName = fullNameProperty.GetString() - }; - } + Id = teacherObj["id"]!.GetValue(), + ShortName = teacherObj["short_name"]!.GetValue(), + FullName = teacherObj["full_name"]!.GetValue() + }; } } - return null; // Return null if no teacher with the given identifier is found + return null; // Повертаємо null, якщо вчителя не знайдено } - private static Group? FindGroupById(JsonElement groups, int? id) + private static Group? FindGroupById(JsonArray groups, int? id) { - foreach (var group in groups.EnumerateArray()) + foreach (var group in groups) { - if (group.TryGetProperty("id", out var idProperty) && - group.TryGetProperty("name", out var nameProperty)) + if (group is JsonObject groupObj && + groupObj["id"]?.GetValue() == id && + groupObj["name"] is not null) { - if (idProperty.ValueKind == JsonValueKind.Number && - nameProperty.ValueKind == JsonValueKind.String) + return new Group { - return new Group - { - Id = idProperty.GetInt64(), - Name = nameProperty.GetString() - }; - } + Id = groupObj["id"]!.GetValue(), + Name = groupObj["name"]!.GetValue() + }; } } - return null; // Return null if no group with the given identifier is found + return null; // Повертаємо null, якщо групу з таким ідентифікатором не знайдено } - - public static List ParseEvents(string json) + + public static List? ParseEvents(string json) { try { - var data = JsonSerializer.Deserialize(json); - var events = data.GetProperty("events"); + var data = JsonNode.Parse(json)?.AsObject(); + if (data == null || !data.ContainsKey("events")) + return null; + + var events = data["events"]?.AsArray(); + var subjects = data["subjects"]?.AsArray(); + var teachers = data["teachers"]?.AsArray(); + var groups = data["groups"]?.AsArray(); List pairs = new List(); - foreach (var lesson in events.EnumerateArray()) + if (events != null) { - Event pair = new Event(); - pair.NumberPair = lesson.GetProperty("number_pair").GetInt32(); - pair.StartTime = lesson.GetProperty("start_time").GetInt64(); - pair.EndTime = lesson.GetProperty("end_time").GetInt64(); - pair.Type = GetType(lesson.GetProperty("type").GetInt32()); + foreach (var lesson in events) + { + if (lesson is not JsonObject lessonObj) continue; - pair.Auditory = lesson.GetProperty("auditory").GetString(); + Event pair = new Event + { + NumberPair = lessonObj["number_pair"]?.GetValue() ?? 0, + StartTime = lessonObj["start_time"]?.GetValue() ?? 0, + EndTime = lessonObj["end_time"]?.GetValue() ?? 0, + Type = GetType(lessonObj["type"]?.GetValue() ?? 0), + Auditory = lessonObj["auditory"]?.GetValue() + }; - pair.Subject = FindSubjectById(data.GetProperty("subjects"), lesson.GetProperty("subject_id").GetInt32()); + if (subjects != null && lessonObj["subject_id"]?.GetValue() is int subjectId) + { + pair.Subject = FindSubjectById(subjects, subjectId); + } - if (lesson.TryGetProperty("teachers", out var teachersProperty) && teachersProperty.GetArrayLength() > 0) - { - pair.Teachers = new List(); - foreach (var teacher in teachersProperty.EnumerateArray()) + if (lessonObj["teachers"] is JsonArray teachersProperty) { - pair.Teachers.Add(FindTeacherById(data.GetProperty("teachers"), teacher.GetInt32())); + pair.Teachers = new List(); + foreach (var teacher in teachersProperty) + { + if (teacher.GetValue() is int teacherId && teachers != null) + { + var foundTeacher = FindTeacherById(teachers, teacherId); + if (foundTeacher != null) + { + pair.Teachers.Add(foundTeacher); + } + } + } + } + else + { + pair.Teachers = new List(); } - } - else - { - pair.Teachers = new List(); - } - if (lesson.TryGetProperty("groups", out var groupsProperty) && groupsProperty.GetArrayLength() > 0) - { - pair.Groups = new List(); - foreach (var group in groupsProperty.EnumerateArray()) + if (lessonObj["groups"] is JsonArray groupsProperty) + { + pair.Groups = new List(); + foreach (var group in groupsProperty) + { + if (group.GetValue() is int groupId && groups != null) + { + var foundGroup = FindGroupById(groups, groupId); + if (foundGroup != null) + { + pair.Groups.Add(foundGroup); + } + } + } + } + else { - var foundGroup = FindGroupById(data.GetProperty("groups"), group.GetInt32()); - pair.Groups.Add(foundGroup); + pair.Groups = new List(); } - } - else - { - pair.Groups = new List(); - } - pairs.Add(pair); + pairs.Add(pair); + } } return pairs.OrderBy(x => x.StartTime).ToList(); @@ -330,6 +353,5 @@ public static List ParseEvents(string json) { return null; } - } }