Skip to content

Commit db7d5f0

Browse files
authored
Revert "Search"
1 parent a79d93e commit db7d5f0

13 files changed

Lines changed: 354 additions & 229 deletions

File tree

app/Http/Controllers/SearchController-.php

Lines changed: 0 additions & 127 deletions
This file was deleted.

app/Http/Controllers/SearchController.php

100644100755
Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,127 @@
1-
<?php
1+
<?php
22

33
namespace App\Http\Controllers;
44

5+
use App\Country;
6+
use App\Event;
7+
use App\Filters\EventFilters;
8+
use App\Http\Transformers\EventTransformer;
9+
use Carbon\Carbon;
510
use Illuminate\Http\Request;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Facades\Cache;
13+
use Illuminate\Support\Facades\Log;
14+
use Illuminate\View\View;
615

716
class SearchController extends Controller
817
{
9-
public function index()
18+
protected $eventTransformer;
19+
20+
/**
21+
* EventController constructor.
22+
*/
23+
public function __construct(EventTransformer $eventTransformer)
24+
{
25+
$this->eventTransformer = $eventTransformer;
26+
}
27+
28+
public function search(Request $request): View
29+
{
30+
31+
$query = $request->input('q', '');
32+
$selected_year = $request->input('year', Carbon::now()->year);
33+
34+
$country_iso = $request->input('country_iso', null);
35+
$tag = $request->input('tag', '');
36+
37+
$selected_country = [];
38+
39+
if (! is_null($country_iso)) {
40+
$country = Country::where('iso', $country_iso)->first();
41+
if ($country) {
42+
$country->translation = __('countries.'.$country->name);
43+
$selected_country[] = $country;
44+
}
45+
46+
}
47+
48+
$current_year = Carbon::now()->year;
49+
$years = [];
50+
for ($year = $current_year; $year >= 2014; $year--) {
51+
$years[] = $year;
52+
}
53+
54+
return view('event.search', compact(['query', 'years', 'selected_country', 'selected_year', 'tag']));
55+
}
56+
57+
public function searchPOST(EventFilters $filters, Request $request)
58+
{
59+
$events = $this->getEvents($filters);
60+
61+
//Log::info($request->input('page'));
62+
if ($request->input('page')) {
63+
$result = [$events];
64+
} else {
65+
Log::info('no page');
66+
$eventsMap = $this->getAllEventsToMap($filters);
67+
$result = [$events, $eventsMap];
68+
}
69+
70+
return $result;
71+
}
72+
73+
protected function getEvents(EventFilters $filters)
1074
{
11-
return view('static.search');
75+
76+
$events = Event::where('status', 'like', 'APPROVED')
77+
->filter($filters)
78+
->orderBy('start_date')
79+
->get()
80+
->groupBy(function ($event) {
81+
if ($event->start_date <= Carbon::today()) {
82+
return 'past';
83+
}
84+
85+
return 'future';
86+
});
87+
88+
if (is_null($events->get('future')) || is_null($events->get('past'))) {
89+
return $events->flatten()->paginate(12);
90+
}
91+
92+
return $events->get('future')->merge($events->get('past'))->paginate(12);
93+
94+
}
95+
96+
protected function getAllEventsToMap(EventFilters $filters)
97+
{
98+
99+
$flattened = Arr::flatten($filters->getFilters());
100+
101+
$composed_key = '';
102+
103+
foreach ($flattened as $value) {
104+
$composed_key .= $value.',';
105+
}
106+
107+
$value = Cache::get($composed_key, function () use ($composed_key, $filters) {
108+
Log::info("Building cache [{$composed_key}]");
109+
$events = Event::where('status', 'like', 'APPROVED')
110+
->filter($filters)
111+
->get();
112+
113+
$events = $this->eventTransformer->transformCollection($events);
114+
115+
$events = $events->groupBy('country');
116+
117+
Cache::put($composed_key, $events, 5 * 60);
118+
119+
return $events;
120+
});
121+
122+
Log::info("Serving from cache [{$composed_key}]");
123+
124+
return $value;
125+
12126
}
13-
}
127+
}

app/Livewire/SearchContentComponent.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)