-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab2a609
commit 60ba966
Showing
13 changed files
with
229 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Country; | ||
use App\Event; | ||
use App\Filters\EventFilters; | ||
use App\Http\Transformers\EventTransformer; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\View\View; | ||
|
||
class SearchController extends Controller | ||
{ | ||
protected $eventTransformer; | ||
|
||
/** | ||
* EventController constructor. | ||
*/ | ||
public function __construct(EventTransformer $eventTransformer) | ||
{ | ||
$this->eventTransformer = $eventTransformer; | ||
} | ||
|
||
public function search(Request $request): View | ||
{ | ||
|
||
$query = $request->input('q', ''); | ||
$selected_year = $request->input('year', Carbon::now()->year); | ||
|
||
$country_iso = $request->input('country_iso', null); | ||
$tag = $request->input('tag', ''); | ||
|
||
$selected_country = []; | ||
|
||
if (! is_null($country_iso)) { | ||
$country = Country::where('iso', $country_iso)->first(); | ||
if ($country) { | ||
$country->translation = __('countries.'.$country->name); | ||
$selected_country[] = $country; | ||
} | ||
|
||
} | ||
|
||
$current_year = Carbon::now()->year; | ||
$years = []; | ||
for ($year = $current_year; $year >= 2014; $year--) { | ||
$years[] = $year; | ||
} | ||
|
||
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag'])); | ||
} | ||
|
||
public function searchPOST(EventFilters $filters, Request $request) | ||
{ | ||
$events = $this->getEvents($filters); | ||
|
||
//Log::info($request->input('page')); | ||
if ($request->input('page')) { | ||
$result = [$events]; | ||
} else { | ||
Log::info('no page'); | ||
$eventsMap = $this->getAllEventsToMap($filters); | ||
$result = [$events, $eventsMap]; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected function getEvents(EventFilters $filters) | ||
{ | ||
|
||
$events = Event::where('status', 'like', 'APPROVED') | ||
->filter($filters) | ||
->orderBy('start_date') | ||
->get() | ||
->groupBy(function ($event) { | ||
if ($event->start_date <= Carbon::today()) { | ||
return 'past'; | ||
} | ||
|
||
return 'future'; | ||
}); | ||
|
||
if (is_null($events->get('future')) || is_null($events->get('past'))) { | ||
return $events->flatten()->paginate(12); | ||
} | ||
|
||
return $events->get('future')->merge($events->get('past'))->paginate(12); | ||
|
||
} | ||
|
||
protected function getAllEventsToMap(EventFilters $filters) | ||
{ | ||
|
||
$flattened = Arr::flatten($filters->getFilters()); | ||
|
||
$composed_key = ''; | ||
|
||
foreach ($flattened as $value) { | ||
$composed_key .= $value.','; | ||
} | ||
|
||
$value = Cache::get($composed_key, function () use ($composed_key, $filters) { | ||
Log::info("Building cache [{$composed_key}]"); | ||
$events = Event::where('status', 'like', 'APPROVED') | ||
->filter($filters) | ||
->get(); | ||
|
||
$events = $this->eventTransformer->transformCollection($events); | ||
|
||
$events = $events->groupBy('country'); | ||
|
||
Cache::put($composed_key, $events, 5 * 60); | ||
|
||
return $events; | ||
}); | ||
|
||
Log::info("Serving from cache [{$composed_key}]"); | ||
|
||
return $value; | ||
|
||
} | ||
} |
122 changes: 4 additions & 118 deletions
122
app/Http/Controllers/SearchController.php
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,13 @@ | ||
<?php | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Country; | ||
use App\Event; | ||
use App\Filters\EventFilters; | ||
use App\Http\Transformers\EventTransformer; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\View\View; | ||
|
||
class SearchController extends Controller | ||
{ | ||
protected $eventTransformer; | ||
|
||
/** | ||
* EventController constructor. | ||
*/ | ||
public function __construct(EventTransformer $eventTransformer) | ||
{ | ||
$this->eventTransformer = $eventTransformer; | ||
} | ||
|
||
public function search(Request $request): View | ||
{ | ||
|
||
$query = $request->input('q', ''); | ||
$selected_year = $request->input('year', Carbon::now()->year); | ||
|
||
$country_iso = $request->input('country_iso', null); | ||
$tag = $request->input('tag', ''); | ||
|
||
$selected_country = []; | ||
|
||
if (! is_null($country_iso)) { | ||
$country = Country::where('iso', $country_iso)->first(); | ||
if ($country) { | ||
$country->translation = __('countries.'.$country->name); | ||
$selected_country[] = $country; | ||
} | ||
|
||
} | ||
|
||
$current_year = Carbon::now()->year; | ||
$years = []; | ||
for ($year = $current_year; $year >= 2014; $year--) { | ||
$years[] = $year; | ||
} | ||
|
||
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag'])); | ||
} | ||
|
||
public function searchPOST(EventFilters $filters, Request $request) | ||
{ | ||
$events = $this->getEvents($filters); | ||
|
||
//Log::info($request->input('page')); | ||
if ($request->input('page')) { | ||
$result = [$events]; | ||
} else { | ||
Log::info('no page'); | ||
$eventsMap = $this->getAllEventsToMap($filters); | ||
$result = [$events, $eventsMap]; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected function getEvents(EventFilters $filters) | ||
public function index() | ||
{ | ||
|
||
$events = Event::where('status', 'like', 'APPROVED') | ||
->filter($filters) | ||
->orderBy('start_date') | ||
->get() | ||
->groupBy(function ($event) { | ||
if ($event->start_date <= Carbon::today()) { | ||
return 'past'; | ||
} | ||
|
||
return 'future'; | ||
}); | ||
|
||
if (is_null($events->get('future')) || is_null($events->get('past'))) { | ||
return $events->flatten()->paginate(12); | ||
} | ||
|
||
return $events->get('future')->merge($events->get('past'))->paginate(12); | ||
|
||
} | ||
|
||
protected function getAllEventsToMap(EventFilters $filters) | ||
{ | ||
|
||
$flattened = Arr::flatten($filters->getFilters()); | ||
|
||
$composed_key = ''; | ||
|
||
foreach ($flattened as $value) { | ||
$composed_key .= $value.','; | ||
} | ||
|
||
$value = Cache::get($composed_key, function () use ($composed_key, $filters) { | ||
Log::info("Building cache [{$composed_key}]"); | ||
$events = Event::where('status', 'like', 'APPROVED') | ||
->filter($filters) | ||
->get(); | ||
|
||
$events = $this->eventTransformer->transformCollection($events); | ||
|
||
$events = $events->groupBy('country'); | ||
|
||
Cache::put($composed_key, $events, 5 * 60); | ||
|
||
return $events; | ||
}); | ||
|
||
Log::info("Serving from cache [{$composed_key}]"); | ||
|
||
return $value; | ||
|
||
return view('static.search'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Livewire; | ||
|
||
use Livewire\Component; | ||
use App\Podcast; | ||
|
||
class SearchContentComponent extends Component | ||
{ | ||
public $searchQuery = ''; | ||
|
||
protected $queryString = [ | ||
'searchQuery' => ['except' => ''], | ||
]; | ||
|
||
public function render() | ||
{ | ||
$results = collect(); // Initialize an empty collection | ||
|
||
if ($this->searchQuery) { | ||
$results = Podcast::active() | ||
->where('title', 'like', '%' . $this->searchQuery . '%') | ||
->get(); // get() returns a collection | ||
} | ||
|
||
return view('livewire.search-content-component', [ | ||
'results' => $results, | ||
]); | ||
} | ||
} |
Oops, something went wrong.