feat: persist timetable and validate confirmed imports
This commit is contained in:
@@ -27,4 +27,43 @@ class Course {
|
||||
final int weekday, startPeriod, endPeriod;
|
||||
final Set<int> weeks;
|
||||
final CourseSource source;
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'teacher': teacher,
|
||||
'room': room,
|
||||
'weekday': weekday,
|
||||
'startPeriod': startPeriod,
|
||||
'endPeriod': endPeriod,
|
||||
'weeks': weeks.toList()..sort(),
|
||||
'source': source.name,
|
||||
};
|
||||
|
||||
static Course fromJson(Map<String, dynamic> json, {CourseSource? source}) {
|
||||
try {
|
||||
String text(String key) {
|
||||
final v = json[key];
|
||||
if (v is! String) throw const FormatException('课程字段缺失');
|
||||
return v.trim();
|
||||
}
|
||||
|
||||
final rawWeeks = json['weeks'];
|
||||
if (rawWeeks is! List || rawWeeks.any((w) => w is! int))
|
||||
throw const FormatException('周次必须是整数数组');
|
||||
return Course(
|
||||
id: text('id'),
|
||||
name: text('name'),
|
||||
teacher: text('teacher'),
|
||||
room: text('room'),
|
||||
weekday: json['weekday'] as int,
|
||||
startPeriod: json['startPeriod'] as int,
|
||||
endPeriod: json['endPeriod'] as int,
|
||||
weeks: rawWeeks.cast<int>().toSet(),
|
||||
source: source ?? CourseSource.values.byName(json['source'] as String),
|
||||
);
|
||||
} catch (_) {
|
||||
throw const FormatException('课程字段、节次或周次无效');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user