Skip to content

Commit

Permalink
Implement auto-serialization (#1072)
Browse files Browse the repository at this point in the history
  • Loading branch information
DGoiana authored Jul 17, 2024
2 parents ba00571 + a53c0e9 commit e1b4136
Show file tree
Hide file tree
Showing 53 changed files with 725 additions and 276 deletions.
6 changes: 5 additions & 1 deletion uni/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ targets:
mockito|mockBuilder:
options:
build_extensions:
'^test/{{}}.dart': 'test/mocks/{{}}.mocks.dart'
'^test/{{}}.dart': 'test/mocks/{{}}.mocks.dart'
source_gen:combining_builder:
options:
build_extensions:
'^lib/{{}}.dart' : 'lib/generated/{{}}.g.dart'
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class CurrentCourseUnitsFetcher implements SessionDependantFetcher {
(course as Map<String, dynamic>)['inscricoes'] as List<dynamic>;
for (final uc in enrollments) {
final courseUnit = CourseUnit.fromJson(uc as Map<String, dynamic>);
if (courseUnit != null) {
ucs.add(courseUnit);
}
ucs.add(courseUnit);
}
}

Expand Down
6 changes: 3 additions & 3 deletions uni/lib/controller/fetchers/exam_fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class ExamFetcher implements SessionDependantFetcher {
final exams = <Exam>{};
for (final courseExam in courseExams) {
for (final uc in userUcs) {
if (!courseExam.type.contains(
if (!courseExam.examType.contains(
'''Exames ao abrigo de estatutos especiais - Port.Est.Especiais''',
) &&
courseExam.type != 'EE' &&
courseExam.type != 'EAE' &&
courseExam.examType != 'EE' &&
courseExam.examType != 'EAE' &&
courseExam.subject == uc.abbreviation &&
uc.enrollmentIsValid() &&
!courseExam.hasEnded()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CalendarDatabase extends AppDatabase<List<CalendarEvent>> {
await db.transaction((txn) async {
await txn.delete('CALENDAR');
for (final event in data) {
await txn.insert('CALENDAR', event.toMap());
await txn.insert('CALENDAR', event.toJson());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,30 @@ class AppCourseUnitsDatabase extends AppDatabase<List<CourseUnit>> {
[createScript],
);
static const String createScript =
'''CREATE TABLE course_units(id INTEGER, code TEXT, abbreviation TEXT , '''
'''name TEXT, curricularYear INTEGER, occurrId INTEGER, semesterCode TEXT, '''
'''semesterName TEXT, type TEXT, status TEXT, grade TEXT, ectsGrade TEXT, '''
'''result TEXT, ects REAL, schoolYear TEXT)''';
'''CREATE TABLE course_units(ucurr_id INTEGER, ucurr_codigo TEXT, ucurr_sigla TEXT , '''
'''ucurr_nome TEXT, ano INTEGER, ocorr_id INTEGER, per_codigo TEXT, '''
'''per_nome TEXT, tipo TEXT, estado TEXT, resultado_melhor TEXT, resultado_ects TEXT, '''
'''resultado_insc TEXT, creditos_ects REAL, schoolYear TEXT)''';

Future<List<CourseUnit>> courseUnits() async {
final db = await getDatabase();
final List<Map<String, dynamic>> maps = await db.query('course_units');

return List.generate(maps.length, (i) {
return CourseUnit(
id: maps[i]['id'] as int,
code: maps[i]['code'] as String,
abbreviation: maps[i]['abbreviation'] as String,
name: maps[i]['name'] as String,
curricularYear: maps[i]['curricularYear'] as int?,
occurrId: maps[i]['occurrId'] as int,
semesterCode: maps[i]['semesterCode'] as String?,
semesterName: maps[i]['semesterName'] as String?,
type: maps[i]['type'] as String?,
status: maps[i]['status'] as String?,
grade: maps[i]['grade'] as String?,
id: maps[i]['ucurr_id'] as int,
code: maps[i]['ucurr_codigo'] as String,
abbreviation: maps[i]['ucurr_sigla'] as String,
name: maps[i]['ucurr_nome'] as String,
curricularYear: maps[i]['ano'] as int?,
occurrId: maps[i]['ocorr_id'] as int,
semesterCode: maps[i]['per_codigo'] as String?,
semesterName: maps[i]['per_nome'] as String?,
type: maps[i]['tipo'] as String?,
status: maps[i]['estado'] as String?,
grade: maps[i]['resultado_melhor'] as String?,
ectsGrade: maps[i]['ectsGrade'] as String?,
result: maps[i]['result'] as String?,
ects: maps[i]['ects'] as double?,
result: maps[i]['resultado_insc'] as String?,
ects: maps[i]['creditos_ects'] as double?,
schoolYear: maps[i]['schoolYear'] as String?,
);
});
Expand All @@ -45,7 +44,7 @@ class AppCourseUnitsDatabase extends AppDatabase<List<CourseUnit>> {
for (final courseUnit in courseUnits) {
await insertInDatabase(
'course_units',
courseUnit.toMap(),
courseUnit.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand Down
25 changes: 10 additions & 15 deletions uni/lib/controller/local_storage/database/app_courses_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ import 'package:uni/model/entities/course.dart';
/// See the [Course] class to see what data is stored in this database.
class AppCoursesDatabase extends AppDatabase<List<Course>> {
AppCoursesDatabase()
: super(
'courses.db',
[createScript],
onUpgrade: migrate,
version: 3,
);
: super('courses.db', [createScript], onUpgrade: migrate, version: 4);
static const String createScript =
'''CREATE TABLE courses(id INTEGER, fest_id INTEGER, name TEXT, '''
'''abbreviation TEXT, currYear TEXT, firstEnrollment INTEGER, state TEXT, '''
'''faculty TEXT, currentAverage REAL, finishedEcts REAL)''';
'''CREATE TABLE courses(cur_id INTEGER, fest_id INTEGER, cur_nome TEXT, '''
'''abbreviation TEXT, ano_curricular TEXT, fest_a_lect_1_insc INTEGER, state TEXT, '''
'''inst_sigla TEXT, currentAverage REAL, finishedEcts REAL)''';

/// Returns a list containing all of the courses stored in this database.
Future<List<Course>> courses() async {
Expand All @@ -29,14 +24,14 @@ class AppCoursesDatabase extends AppDatabase<List<Course>> {
// Convert the List<Map<String, dynamic> into a List<Course>.
return List.generate(maps.length, (i) {
return Course(
id: maps[i]['id'] as int? ?? 0,
id: maps[i]['cur_id'] as int? ?? 0,
festId: maps[i]['fest_id'] as int? ?? 0,
name: maps[i]['name'] as String?,
name: maps[i]['cur_nome'] as String?,
abbreviation: maps[i]['abbreviation'] as String?,
currYear: maps[i]['currYear'] as String?,
firstEnrollment: maps[i]['firstEnrollment'] as int? ?? 0,
currYear: maps[i]['ano_curricular'] as String?,
firstEnrollment: maps[i]['fest_a_lect_1_insc'] as int? ?? 0,
state: maps[i]['state'] as String?,
faculty: maps[i]['faculty'] as String?,
faculty: maps[i]['inst_sigla'] as String?,
finishedEcts: maps[i]['finishedEcts'] as double? ?? 0,
currentAverage: maps[i]['currentAverage'] as double? ?? 0,
);
Expand All @@ -50,7 +45,7 @@ class AppCoursesDatabase extends AppDatabase<List<Course>> {
for (final course in courses) {
await insertInDatabase(
'courses',
course.toMap(),
course.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ abstract class AppDatabase<T> {
await lock.synchronized(
() async {
final db = await getDatabase();

await db.insert(
table,
values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import 'package:uni/model/entities/exam.dart';
/// See the [Exam] class to see what data is stored in this database.
class AppExamsDatabase extends AppDatabase<List<Exam>> {
AppExamsDatabase()
: super(
'exams.db',
[_createScript],
onUpgrade: migrate,
version: 5,
);
: super('exams.db', [_createScript], onUpgrade: migrate, version: 6);

static const _createScript = '''
CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT,
Expand Down Expand Up @@ -44,9 +39,11 @@ CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT,
/// If a row with the same data is present, it will be replaced.
Future<void> _insertExams(List<Exam> exams) async {
for (final exam in exams) {
final examJson = exam.toJson();
examJson['rooms'] = (examJson['rooms'] as List<dynamic>).join(',');
await insertInDatabase(
'exams',
exam.toMap(),
examJson,
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class AppLecturesDatabase extends AppDatabase<List<Lecture>> {
createScript,
],
onUpgrade: migrate,
version: 7,
version: 9,
);
static const createScript = '''
CREATE TABLE lectures(subject TEXT, typeClass TEXT,
startDateTime TEXT, blocks INTEGER, room TEXT, teacher TEXT, classNumber TEXT, occurrId INTEGER)''';
startTime TEXT,endTime TEXT, blocks INTEGER, room TEXT, teacher TEXT, classNumber TEXT, occurrId INTEGER)''';

/// Returns a list containing all of the lectures stored in this database.
Future<List<Lecture>> lectures() async {
Expand Down Expand Up @@ -48,7 +48,7 @@ CREATE TABLE lectures(subject TEXT, typeClass TEXT,
for (final lec in lecs) {
await insertInDatabase(
'lectures',
lec.toMap(),
lec.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LibraryOccupationDatabase extends AppDatabase<LibraryOccupation> {
await db.transaction((txn) async {
await txn.delete('FLOOR_OCCUPATION');
for (final floor in data.floors) {
await txn.insert('FLOOR_OCCUPATION', floor.toMap());
await txn.insert('FLOOR_OCCUPATION', floor.toJson());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AppReferencesDatabase extends AppDatabase<List<Reference>> {
for (final reference in references) {
await insertInDatabase(
'refs',
reference.toMap(),
reference.toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {

/// Insert restaurant and meals in database
Future<void> insertRestaurant(Transaction txn, Restaurant restaurant) async {
final id = await txn.insert('RESTAURANTS', restaurant.toMap());
for (final meals in restaurant.meals.values) {
final id = await txn.insert('RESTAURANTS', restaurant.toJson());
restaurant.meals.forEach((dayOfWeak, meals) async {
for (final meal in meals) {
await txn.insert('MEALS', meal.toMap(id));
}
}
});
}

/// Deletes all restaurants and meals
Expand Down
23 changes: 23 additions & 0 deletions uni/lib/generated/model/entities/bug_report.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions uni/lib/generated/model/entities/bus.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions uni/lib/generated/model/entities/bus_stop.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions uni/lib/generated/model/entities/calendar_event.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions uni/lib/generated/model/entities/course.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1b4136

Please sign in to comment.