diff --git a/src/DataMapper/DataMapper.php b/src/DataMapper/DataMapper.php index 695b7cd6..bd614fc2 100644 --- a/src/DataMapper/DataMapper.php +++ b/src/DataMapper/DataMapper.php @@ -17,6 +17,7 @@ use Mongolid\Schema\Schema; use Mongolid\Util\ObjectIdUtils; use Mongolid\Connection\Connection; +Use Mongolid\Util\SoftDeleteQueries; /** * The DataMapper class will abstract how an Entity is persisted and retrieved @@ -259,13 +260,14 @@ public function where( $cursorClass = $cacheable ? SchemaCacheableCursor::class : SchemaCursor::class; $model = new $this->schema->entityClass; + $query = $this->prepareValueQuery($query); return new $cursorClass( $this->schema, $this->getCollection(), 'find', [ - $this->prepareValueQuery($query), + SoftDeleteQueries::insertFilterForSoftDelete($query), [ 'projection' => $this->prepareProjection($projection), 'eagerLoads' => $model->with ?? [], diff --git a/src/Model/SoftDeletesTrait.php b/src/Model/SoftDeletesTrait.php new file mode 100644 index 00000000..0d01d394 --- /dev/null +++ b/src/Model/SoftDeletesTrait.php @@ -0,0 +1,55 @@ +{$this->getDeletedAtColumn()}); + } + + public function restore(): bool + { + $collumn = $this->getDeletedAtColumn(); + if (!$collumn = $this->{$collumn}) { + return false; + } + + $this->{$collumn} = null; + + return $this->execute('save'); + } + + public function forceDelete(): bool + { + $this->forceDelete = true; + + return $this->execute('delete'); + } + + private function getDeletedAtColumn(): string + { + return defined( + static::class . '::DELETED_AT' + ) + ? static::DELETED_AT + : 'deleted_at'; + } + + private function getFiltredQuery(): array + { + return [ + '$or' => [ + [ + 'deleted_at' => null, + ], + [ + 'deleted_at' => ['$exists' => false], + ], + ], + ]; + } +} diff --git a/src/Util/SoftDeleteQueries.php b/src/Util/SoftDeleteQueries.php new file mode 100644 index 00000000..3b521bc0 --- /dev/null +++ b/src/Util/SoftDeleteQueries.php @@ -0,0 +1,23 @@ + [ + [ + 'deleted_at' => null, + ], + [ + 'deleted_at' => ['$exists' => false], + ], + ], + ] + ); + } +} diff --git a/tests/Unit/Model/SoftDeletesTraitTest.php b/tests/Unit/Model/SoftDeletesTraitTest.php new file mode 100644 index 00000000..bf8404b9 --- /dev/null +++ b/tests/Unit/Model/SoftDeletesTraitTest.php @@ -0,0 +1,73 @@ +makeProduct(); + $product->deleted_at = $date; + + // Actions + $actual = $product->isTrashed(); + + // Assertions + $this->assertTrue($actual); + } + + public function testShouldRestoreProduct(): void + { + // Set + $product = $this->makeProduct(); + $date = new UTCDateTime(new DateTime('today')); + $product->deleted_at = $date; + $dataMapper = $this->instance( + DataMapper::class, + m::mock(DataMapper::class) + ); + + // Expectations + $dataMapper->expects() + ->setSchema(m::type(DynamicSchema::class)); + + $dataMapper->expects() + ->save(m::type(Product::class), m::type('array')) + ->andReturnTrue(); + + // Actions + $actual = $product->restore(); + + // Assertions + $this->assertTrue($actual); + } + + public function testShouldNotRestoreProduct(): void + { + // Set + $product = $this->makeProduct(); + + // Actions + $actual = $product->restore(); + + // Assertions + $this->assertFalse($actual); + } + + private function makeProduct(): Product + { + return new class extends Product { + use SoftDeletesTrait; + }; + } +}