diff --git a/app/Http/Controllers/API/SearchController.php b/app/Http/Controllers/API/SearchController.php new file mode 100644 index 00000000..07c3762c --- /dev/null +++ b/app/Http/Controllers/API/SearchController.php @@ -0,0 +1,32 @@ +songRepository = $songRepository; + $this->albumRepository = $albumRepository; + $this->artistRepository = $artistRepository; + $this->playlistRepository = $playlistRepository; + } + + public function index() + { + } +} diff --git a/app/Repositories/AbstractRepository.php b/app/Repositories/AbstractRepository.php index 8e59b97e..1fac8bc2 100644 --- a/app/Repositories/AbstractRepository.php +++ b/app/Repositories/AbstractRepository.php @@ -9,17 +9,19 @@ use Throwable; abstract class AbstractRepository implements RepositoryInterface { + /** @var string */ + private $modelClass; + /** @var Model */ protected $model; /** @var Guard */ protected $auth; - abstract public function getModelClass(): string; - - public function __construct() + public function __construct(?string $modelClass = null) { - $this->model = app($this->getModelClass()); + $this->modelClass = $modelClass ?: self::guessModelClass(); + $this->model = app($this->modelClass); // This instantiation may fail during a console command if e.g. APP_KEY is empty, // rendering the whole installation failing. @@ -29,6 +31,11 @@ abstract class AbstractRepository implements RepositoryInterface } } + private static function guessModelClass(): string + { + return preg_replace('/(.+)\\\\Repositories\\\\(.+)Repository$/m', '$1\Models\\\$2', static::class); + } + public function getOneById($id): ?Model { return $this->model->find($id); diff --git a/app/Repositories/AlbumRepository.php b/app/Repositories/AlbumRepository.php index 0e974e4a..dfd4dfce 100644 --- a/app/Repositories/AlbumRepository.php +++ b/app/Repositories/AlbumRepository.php @@ -2,15 +2,12 @@ namespace App\Repositories; -use App\Models\Album; use App\Models\Song; +use App\Repositories\Traits\Searchable; class AlbumRepository extends AbstractRepository { - public function getModelClass(): string - { - return Album::class; - } + use Searchable; /** @return array */ public function getNonEmptyAlbumIds(): array diff --git a/app/Repositories/ArtistRepository.php b/app/Repositories/ArtistRepository.php index c6b98256..627fd0bb 100644 --- a/app/Repositories/ArtistRepository.php +++ b/app/Repositories/ArtistRepository.php @@ -2,16 +2,10 @@ namespace App\Repositories; -use App\Models\Artist; use App\Models\Song; class ArtistRepository extends AbstractRepository { - public function getModelClass(): string - { - return Artist::class; - } - /** @return array */ public function getNonEmptyArtistIds(): array { diff --git a/app/Repositories/InteractionRepository.php b/app/Repositories/InteractionRepository.php index a230ce6b..ba1ec0d2 100644 --- a/app/Repositories/InteractionRepository.php +++ b/app/Repositories/InteractionRepository.php @@ -12,11 +12,6 @@ class InteractionRepository extends AbstractRepository { use ByCurrentUser; - public function getModelClass(): string - { - return Interaction::class; - } - /** @return Collection|array */ public function getUserFavorites(User $user): Collection { diff --git a/app/Repositories/PlaylistRepository.php b/app/Repositories/PlaylistRepository.php index 10dbdf3e..2476dc6f 100644 --- a/app/Repositories/PlaylistRepository.php +++ b/app/Repositories/PlaylistRepository.php @@ -4,16 +4,13 @@ namespace App\Repositories; use App\Models\Playlist; use App\Repositories\Traits\ByCurrentUser; +use App\Repositories\Traits\Searchable; use Illuminate\Support\Collection; class PlaylistRepository extends AbstractRepository { use ByCurrentUser; - - public function getModelClass(): string - { - return Playlist::class; - } + use Searchable; /** @return Collection|array */ public function getAllByCurrentUser(): Collection diff --git a/app/Repositories/RepositoryInterface.php b/app/Repositories/RepositoryInterface.php index 591fd4dd..c0b1ec38 100644 --- a/app/Repositories/RepositoryInterface.php +++ b/app/Repositories/RepositoryInterface.php @@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model; interface RepositoryInterface { - public function getModelClass(): string; - public function getOneById($id): ?Model; /** @return Collection|array */ diff --git a/app/Repositories/SettingRepository.php b/app/Repositories/SettingRepository.php index 5d459673..2666b3ae 100644 --- a/app/Repositories/SettingRepository.php +++ b/app/Repositories/SettingRepository.php @@ -2,15 +2,8 @@ namespace App\Repositories; -use App\Models\Setting; - class SettingRepository extends AbstractRepository { - public function getModelClass(): string - { - return Setting::class; - } - /** @return array */ public function getAllAsKeyValueArray(): array { diff --git a/app/Repositories/SongRepository.php b/app/Repositories/SongRepository.php index d91a2c51..4bfd865c 100644 --- a/app/Repositories/SongRepository.php +++ b/app/Repositories/SongRepository.php @@ -3,10 +3,13 @@ namespace App\Repositories; use App\Models\Song; +use App\Repositories\Traits\Searchable; use App\Services\HelperService; class SongRepository extends AbstractRepository { + use Searchable; + private $helperService; public function __construct(HelperService $helperService) @@ -16,11 +19,6 @@ class SongRepository extends AbstractRepository $this->helperService = $helperService; } - public function getModelClass(): string - { - return Song::class; - } - public function getOneByPath(string $path): ?Song { return $this->getOneById($this->helperService->getFileHash($path)); diff --git a/app/Repositories/Traits/Searchable.php b/app/Repositories/Traits/Searchable.php new file mode 100644 index 00000000..7f3c9763 --- /dev/null +++ b/app/Repositories/Traits/Searchable.php @@ -0,0 +1,8 @@ +