diff --git a/integration_test/framework/database_tuple_repository_tests.dart b/integration_test/framework/database_tuple_repository_tests.dart index dfc72de3e..a71872f72 100644 --- a/integration_test/framework/database_tuple_repository_tests.dart +++ b/integration_test/framework/database_tuple_repository_tests.dart @@ -178,6 +178,48 @@ void main() => group( ); }, ); + + group( + '#replace', + () { + late TestObjectDatabaseTupleEntity savedFalseSample; + setUpAll( + () async { + final falseSample = TestObjectEntity(false); + final now = DateTime.now(); + + final dbA = await DatabaseFactory.open(_defDbTest); + + await dbA.delete(_defTableTestObject); + savedFalseSample = + TestObjectDatabaseTupleEntity.fromMap(falseSample.toMap + ..addAll({ + defPkId.name: await dbA.insert( + _defTableTestObject, + falseSample.toMap + ..addAll({defColCreatedAt.name: now})), + defColCreatedAt.name: now + })); + }, + ); + + test( + ': updated.', + () async { + final updatedAt = DateTime.now(); + final updating = savedFalseSample.copiedWith( + a: () => true, + ); + + final updated = await repository.replace(updating, + updatedAt: updatedAt); + + expect(updated.a, equals(updating.a)); + expect(updated.updatedAt, equals(updatedAt)); + }, + ); + }, + ); }, ); }, diff --git a/lib/framework/repository/database_tuple_repository.dart b/lib/framework/repository/database_tuple_repository.dart index 6aef6544e..becea3d3d 100644 --- a/lib/framework/repository/database_tuple_repository.dart +++ b/lib/framework/repository/database_tuple_repository.dart @@ -91,4 +91,25 @@ abstract class DatabaseTupleRepository replace(Saved savedEntity, {DateTime? updatedAt}) => v( + () async { + final entityMap = savedEntity.toMap; + + entityMap[defColUpdatedAt.name] = updatedAt ?? DateTime.now(); + + final byId = Equals(defPkId.name, entityMap[defPkId.name]); + await (await _dbA).update( + _tableDefinition, + entityMap, + where: byId.where(), + whereArgs: byId.whereArgs(), + ); + + return pack(entityMap); + }, + { + 'savedEntity': savedEntity, + }, + ); } diff --git a/lib/framework/repository/entity.dart b/lib/framework/repository/entity.dart index 2ae9fa1bd..8c97dd55d 100644 --- a/lib/framework/repository/entity.dart +++ b/lib/framework/repository/entity.dart @@ -11,6 +11,8 @@ abstract class EntityV1 {} mixin Entity { Map get toMap; + Entity copiedWith(); + @override String toString() => "${super.toString()}: $toMap"; diff --git a/test/framework/repository/database_tuple_entity_test.dart b/test/framework/repository/database_tuple_entity_test.dart index 536d42ea9..6c2e1d0fe 100644 --- a/test/framework/repository/database_tuple_entity_test.dart +++ b/test/framework/repository/database_tuple_entity_test.dart @@ -14,6 +14,11 @@ class TestObjectDatabaseTupleEntity extends TestObjectEntity : super.fromMap(map) { withMap(map); } + + @override + TestObjectDatabaseTupleEntity copiedWith({bool Function()? a}) => + TestObjectDatabaseTupleEntity.fromMap( + toMap..addAll(super.copiedWith(a: a).toMap)); } void main() => group( @@ -63,5 +68,26 @@ void main() => group( expect(testObject.toMap, map); }, ); + + test( + '#copiedWith', + () { + final from = TestObjectDatabaseTupleEntity.fromMap({ + TestObjectEntity.fieldNames[0]: false, + defPkId.name: 1, + defColCreatedAt.name: DateTime.now(), + defColUpdatedAt.name: DateTime.now(), + defColArchivedAt.name: DateTime.now() + }); + + final copied = from.copiedWith(a: () => true); + + expect(copied.a, equals(true)); + expect(copied.id, equals(from.id)); + expect(copied.createdAt, equals(from.createdAt)); + expect(copied.updatedAt, equals(from.updatedAt)); + expect(copied.archivedAt, equals(from.archivedAt)); + }, + ); }, ); diff --git a/test/framework/repository/entity_test.dart b/test/framework/repository/entity_test.dart index 47bc819b6..549276528 100644 --- a/test/framework/repository/entity_test.dart +++ b/test/framework/repository/entity_test.dart @@ -19,6 +19,10 @@ class TestObjectEntity extends _TestObject with Entity { @override Map get toMap => {fieldNames[0]: a}; + + @override + TestObjectEntity copiedWith({bool Function()? a}) => + TestObjectEntity(a == null ? this.a : a()); } void main() => group( @@ -70,5 +74,16 @@ void main() => group( expect(testObjectA, equals(testObjectB)); }, ); + + test( + '#copiedWith', + () { + final from = TestObjectEntity(false); + + final copied = from.copiedWith(a: () => true); + + expect(copied.a, equals(true)); + }, + ); }, );