Added filters for projects
This commit is contained in:
parent
ad20678d7a
commit
b831de63e4
@ -6,6 +6,7 @@ namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Repository\Interfaces\ProjectRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
@ -14,9 +15,17 @@ class ProjectController extends Controller
|
||||
private ProjectRepository $projectRepository
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
return $this->projectRepository->all();
|
||||
$request->validate([
|
||||
'category' => 'nullable|string|exists:categories,slug'
|
||||
]);
|
||||
$filters = [];
|
||||
if ($request->has('category') && ($category = $request->get('category')) !== '') {
|
||||
$filters['category'] = $category;
|
||||
}
|
||||
|
||||
return $this->projectRepository->all($filters);
|
||||
}
|
||||
|
||||
public function show(int $project)
|
||||
|
@ -11,7 +11,7 @@ use Illuminate\Support\Collection;
|
||||
interface ProjectRepository
|
||||
{
|
||||
|
||||
public function all(): Collection;
|
||||
public function all(array $filters = []): Collection;
|
||||
public function get(int $id): ProjectResource;
|
||||
public function update(Project $project, array $data = []): bool;
|
||||
public function create(array $data = []): Project;
|
||||
|
@ -8,6 +8,7 @@ use App\Http\Resources\ProjectCollection;
|
||||
use App\Http\Resources\ProjectResource;
|
||||
use App\Models\Project;
|
||||
use App\Repository\Interfaces\ProjectRepository as ProjectRepositoryInterface;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ProjectRepository implements ProjectRepositoryInterface
|
||||
@ -19,12 +20,17 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
private Project $project
|
||||
) {}
|
||||
|
||||
public function all(): Collection
|
||||
public function all(array $filters = []): Collection
|
||||
{
|
||||
$project = $this->project
|
||||
->query()
|
||||
->orderBy('release_data', 'ASC');
|
||||
|
||||
foreach ($filters as $filter_name => $filter_value) {
|
||||
if ($filter_name === 'category' && $filter_value !== 'all')
|
||||
$project->where('categories', 'like', '%"'. $filter_value .'"%');
|
||||
}
|
||||
|
||||
if (!$this->auth)
|
||||
$project->visibled();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user