Skip to content

Commit

Permalink
[Hexlet#704] add filter by authors and exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirkuvanovv committed Mar 17, 2021
1 parent bae066e commit 1942052
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
27 changes: 25 additions & 2 deletions app/Http/Controllers/SolutionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,45 @@
namespace App\Http\Controllers;

use App\Models\Solution;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\View\View;

class SolutionController extends Controller
{
public function index(): View
public function index(Request $request): View
{
$filter = $request->input('filter');

$solutions = QueryBuilder::for(Solution::versioned())
->allowedFilters([
AllowedFilter::exact('exercise_id'),
AllowedFilter::exact('user_id'),
])
->whereHas('user')
->latest()
->paginate(50);
return view('solution.index', ['solutions' => $solutions]);

$exerciseTitles = $solutions
->mapWithKeys(fn($solution) => [$solution->exercise->id => $solution->exercise->path . getExerciseTitle($solution->exercise)]);

$users = [];
if (Auth::user()) {
$users = [Auth::user()->id => Auth::user()->name];
}

return view(
'solution.index',
[
'solutions' => $solutions,
'users' => $users,
'filter' => $filter,
'exerciseTitles' => $exerciseTitles
]
);
}

public function show(Solution $solution): View
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/en/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
'date' => 'Date',
],
'show_action' => 'Show',
'filter' => [
'apply-btn' => 'Apply',
'reset-btn' => 'Reset',
],
],
],
'exercise' => [
Expand Down
4 changes: 4 additions & 0 deletions resources/lang/ru/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
'date' => 'Дата',
],
'show_action' => 'Смотреть',
'filter' => [
'apply-btn' => 'Применить',
'reset-btn' => 'Сбросить',
],
],
],
'exercise' => [
Expand Down
17 changes: 17 additions & 0 deletions resources/views/solution/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@

<h1 class="h3">{{ __('views.solution.index.header.h1') }}</h1>

<div class="d-flex mb-3 mt-3">
<div>
{{ Form::open(['url' => route('solutions.index'), 'method' => 'GET' , 'class' => 'form-inline']) }}
{{ Form::select('filter[user_id]', $users, $filter['user_id'] ?? '', ['placeholder' => __('views.solution.index.table_header.author'), 'class' => 'form-control mr-2']) }}
{{ Form::select('filter[exercise_id]', $exerciseTitles, $filter['exercise_id'] ?? '', ['placeholder' => __('views.solution.index.table_header.exercise'), 'class' => 'form-control mr-2']) }}
{{ Form::submit(__('views.solution.index.filter.apply-btn'), ['class' => 'btn btn-outline-primary']) }}
<a href="{{ route('solutions.index') }}"
class="remove-btn btn btn-outline-secondary ml-3"
data-method="get"
rel="nofollow"
>
{{ __('views.solution.index.filter.reset-btn') }}
</a>
{{ Form::close() }}
</div>
</div>

<div class="table-responsive">
<table class="table table-striped">
<thead>
Expand Down

0 comments on commit 1942052

Please sign in to comment.