Adding method to get all folders under nested collection

parent c9a8a838
...@@ -11,6 +11,7 @@ use Modules\Media\Events\FolderIsUpdating; ...@@ -11,6 +11,7 @@ use Modules\Media\Events\FolderIsUpdating;
use Modules\Media\Events\FolderWasCreated; use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated; use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Repositories\FolderRepository; use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Support\Collection\NestedFoldersCollection;
class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository class EloquentFolderRepository extends EloquentBaseRepository implements FolderRepository
{ {
...@@ -83,6 +84,11 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR ...@@ -83,6 +84,11 @@ class EloquentFolderRepository extends EloquentBaseRepository implements FolderR
return $this->model->where('path', 'like', "{$path}%")->get(); return $this->model->where('path', 'like', "{$path}%")->get();
} }
public function allNested(): NestedFoldersCollection
{
return new NestedFoldersCollection($this->all());
}
/** /**
* @param array $data * @param array $data
* @return string * @return string
......
...@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories; ...@@ -5,6 +5,7 @@ namespace Modules\Media\Repositories;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Modules\Core\Repositories\BaseRepository; use Modules\Core\Repositories\BaseRepository;
use Modules\Media\Entities\File; use Modules\Media\Entities\File;
use Modules\Media\Support\Collection\NestedFoldersCollection;
interface FolderRepository extends BaseRepository interface FolderRepository extends BaseRepository
{ {
...@@ -20,4 +21,6 @@ interface FolderRepository extends BaseRepository ...@@ -20,4 +21,6 @@ interface FolderRepository extends BaseRepository
* @return Collection * @return Collection
*/ */
public function allChildrenOf(File $folder); public function allChildrenOf(File $folder);
public function allNested() : NestedFoldersCollection;
} }
...@@ -11,6 +11,7 @@ use Modules\Media\Events\FolderWasCreated; ...@@ -11,6 +11,7 @@ use Modules\Media\Events\FolderWasCreated;
use Modules\Media\Events\FolderWasUpdated; use Modules\Media\Events\FolderWasUpdated;
use Modules\Media\Repositories\FolderRepository; use Modules\Media\Repositories\FolderRepository;
use Modules\Media\Services\FileService; use Modules\Media\Services\FileService;
use Modules\Media\Support\Collection\NestedFoldersCollection;
final class EloquentFolderRepositoryTest extends MediaTestCase final class EloquentFolderRepositoryTest extends MediaTestCase
{ {
...@@ -239,6 +240,19 @@ final class EloquentFolderRepositoryTest extends MediaTestCase ...@@ -239,6 +240,19 @@ final class EloquentFolderRepositoryTest extends MediaTestCase
$this->assertCount(2, $this->folder->all()); $this->assertCount(2, $this->folder->all());
} }
/** @test */
public function it_can_find_all_folders_with_nested()
{
$parentFolder = $this->folder->create(['name' => 'My Folder']);
$this->folder->create(['name' => 'Child folder', 'parent_id' => $parentFolder->id]);
$this->createFile();
$this->createFile('second-file.jpg');
$folders = $this->folder->allNested();
$this->assertInstanceOf(NestedFoldersCollection::class, $folders);
$this->assertCount(2, $folders);
}
/** @test */ /** @test */
public function it_can_delete_a_folder_from_database() public function it_can_delete_a_folder_from_database()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment