Skip to content

Commit

Permalink
use json_serialisable
Browse files Browse the repository at this point in the history
  • Loading branch information
Leptopoda committed Oct 15, 2022
1 parent 69f80c5 commit b28fba9
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 196 deletions.
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include: package:lint/analysis_options.yaml
# You might want to exclude auto-generated files from dart analysis
analyzer:
exclude:
#- '**.freezed.dart'
- "**.g.dart"

# You can customize the lint rules set to your own liking. A list of all rules
# can be found at https://dart-lang.github.io/linter/lints/options/options.html
Expand Down
4 changes: 0 additions & 4 deletions lib/common/data/database_entity.dart

This file was deleted.

11 changes: 11 additions & 0 deletions lib/common/data/epoch_date_time_converter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:json_annotation/json_annotation.dart';

class EpochDateTimeConverter implements JsonConverter<DateTime, int> {
const EpochDateTimeConverter();

@override
DateTime fromJson(int json) => DateTime.fromMillisecondsSinceEpoch(json);

@override
int toJson(DateTime object) => object.millisecondsSinceEpoch;
}
49 changes: 0 additions & 49 deletions lib/date_management/data/date_entry_entity.dart

This file was deleted.

17 changes: 8 additions & 9 deletions lib/date_management/data/date_entry_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:dhbwstudentapp/common/data/database_access.dart';
import 'package:dhbwstudentapp/date_management/data/date_entry_entity.dart';
import 'package:dhbwstudentapp/date_management/model/date_entry.dart';

class DateEntryRepository {
Expand All @@ -12,7 +11,7 @@ class DateEntryRepository {
String? year,
) async {
final rows = await _database.queryRows(
DateEntryEntity.tableName(),
DateEntry.tableName,
where: "databaseName=? AND year=?",
whereArgs: [databaseName, year],
);
Expand All @@ -27,7 +26,7 @@ class DateEntryRepository {
DateTime end,
) async {
final rows = await _database.queryRows(
DateEntryEntity.tableName(),
DateEntry.tableName,
where: "date>=? AND date<=? AND databaseName=? AND year=?",
whereArgs: [
start.millisecondsSinceEpoch,
Expand All @@ -46,7 +45,7 @@ class DateEntryRepository {
DateTime date,
) async {
final rows = await _database.queryRows(
DateEntryEntity.tableName(),
DateEntry.tableName,
where: "date>=? AND databaseName=? AND year=?",
whereArgs: [
date.millisecondsSinceEpoch,
Expand All @@ -64,7 +63,7 @@ class DateEntryRepository {
DateTime date,
) async {
final rows = await _database.queryRows(
DateEntryEntity.tableName(),
DateEntry.tableName,
where: "date<=? AND databaseName=? AND year=?",
whereArgs: [
date.millisecondsSinceEpoch,
Expand All @@ -77,8 +76,8 @@ class DateEntryRepository {
}

Future saveDateEntry(DateEntry entry) async {
final row = DateEntryEntity.fromModel(entry).toMap();
await _database.insert(DateEntryEntity.tableName(), row);
final row = entry.toJson();
await _database.insert(DateEntry.tableName, row);
}

Future saveDateEntries(List<DateEntry> entries) async {
Expand All @@ -92,7 +91,7 @@ class DateEntryRepository {
String? year,
) async {
await _database.deleteWhere(
DateEntryEntity.tableName(),
DateEntry.tableName,
where: "databaseName=? AND year=?",
whereArgs: [
databaseName,
Expand All @@ -106,7 +105,7 @@ class DateEntryRepository {

for (final row in rows) {
dateEntries.add(
DateEntryEntity.fromMap(row).asDateEntry(),
DateEntry.fromJson(row),
);
}

Expand Down
18 changes: 17 additions & 1 deletion lib/date_management/model/date_entry.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import 'package:dhbwstudentapp/common/data/epoch_date_time_converter.dart';
import 'package:json_annotation/json_annotation.dart';

part 'date_entry.g.dart';

@JsonSerializable()
class DateEntry {
final String description;
final String year;
final String comment;
final String databaseName;
@JsonKey(name: "date")
@EpochDateTimeConverter()
final DateTime start;
@EpochDateTimeConverter()
final DateTime end;
final String? room;

Expand All @@ -16,9 +25,16 @@ class DateEntry {
DateTime? end,
this.room,
}) : start = start ?? DateTime.fromMicrosecondsSinceEpoch(0),
end = end ?? DateTime.fromMicrosecondsSinceEpoch(0),
end = end ?? start ?? DateTime.fromMicrosecondsSinceEpoch(0),
comment = comment ?? "",
description = description ?? "",
year = year ?? "",
databaseName = databaseName ?? "";

factory DateEntry.fromJson(Map<String, dynamic> json) =>
_$DateEntryFromJson(json);

Map<String, dynamic> toJson() => _$DateEntryToJson(this);

static const tableName = "DateEntries";
}
35 changes: 35 additions & 0 deletions lib/date_management/model/date_entry.g.dart

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

58 changes: 0 additions & 58 deletions lib/schedule/data/schedule_entry_entity.dart

This file was deleted.

25 changes: 12 additions & 13 deletions lib/schedule/data/schedule_entry_repository.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:dhbwstudentapp/common/data/database_access.dart';
import 'package:dhbwstudentapp/schedule/data/schedule_entry_entity.dart';
import 'package:dhbwstudentapp/schedule/model/schedule.dart';
import 'package:dhbwstudentapp/schedule/model/schedule_entry.dart';

Expand All @@ -17,7 +16,7 @@ class ScheduleEntryRepository {
DateTime end,
) async {
final rows = await _database.queryRows(
ScheduleEntryEntity.tableName(),
ScheduleEntry.tableName,
where: "end>? AND start<?",
whereArgs: [
start.millisecondsSinceEpoch,
Expand All @@ -27,15 +26,15 @@ class ScheduleEntryRepository {

final entries = <ScheduleEntry>[];
for (final row in rows) {
entries.add(ScheduleEntryEntity.fromMap(row).asScheduleEntry());
entries.add(ScheduleEntry.fromJson(row));
}

return Schedule(entries: entries);
}

Future<ScheduleEntry?> queryExistingScheduleEntry(ScheduleEntry entry) async {
final rows = await _database.queryRows(
ScheduleEntryEntity.tableName(),
ScheduleEntry.tableName,
where: "start=? AND end=? AND title=? AND details=? AND professor=?",
whereArgs: [
entry.start.millisecondsSinceEpoch,
Expand All @@ -48,12 +47,12 @@ class ScheduleEntryRepository {

if (rows.isEmpty) return null;

return ScheduleEntryEntity.fromMap(rows[0]).asScheduleEntry();
return ScheduleEntry.fromJson(rows[0]);
}

Future<ScheduleEntry?> queryNextScheduleEntry(DateTime dateTime) async {
final nextScheduleEntry = await _database.queryRows(
ScheduleEntryEntity.tableName(),
ScheduleEntry.tableName,
where: "start>?",
whereArgs: [dateTime.millisecondsSinceEpoch],
limit: 1,
Expand All @@ -63,7 +62,7 @@ class ScheduleEntryRepository {
final entriesList = nextScheduleEntry.toList();

if (entriesList.length == 1) {
return ScheduleEntryEntity.fromMap(entriesList[0]).asScheduleEntry();
return ScheduleEntry.fromJson(entriesList[0]);
}

return null;
Expand All @@ -86,12 +85,12 @@ class ScheduleEntryRepository {
return;
}

final row = ScheduleEntryEntity.fromModel(entry).toMap();
final row = entry.toJson();
if (entry.id == null) {
final id = await _database.insert(ScheduleEntryEntity.tableName(), row);
final id = await _database.insert(ScheduleEntry.tableName, row);
entry = entry.copyWith.id(id);
} else {
await _database.update(ScheduleEntryEntity.tableName(), row);
await _database.update(ScheduleEntry.tableName, row);
}
}

Expand All @@ -102,15 +101,15 @@ class ScheduleEntryRepository {
}

Future<void> deleteScheduleEntry(ScheduleEntry entry) async {
await _database.delete(ScheduleEntryEntity.tableName(), entry.id);
await _database.delete(ScheduleEntry.tableName, entry.id);
}

Future<void> deleteScheduleEntriesBetween(
DateTime start,
DateTime end,
) async {
await _database.deleteWhere(
ScheduleEntryEntity.tableName(),
ScheduleEntry.tableName,
where: "start>=? AND end<=?",
whereArgs: [
start.millisecondsSinceEpoch,
Expand All @@ -121,7 +120,7 @@ class ScheduleEntryRepository {

Future<void> deleteAllScheduleEntries() async {
await _database.deleteWhere(
ScheduleEntryEntity.tableName(),
ScheduleEntry.tableName,
where: "1=1",
whereArgs: [],
);
Expand Down
Loading

0 comments on commit b28fba9

Please sign in to comment.