mirror of
https://koodu.h-i.works/projects/thebadspace
synced 2025-06-25 16:04:37 -05:00
Plugged in repository class for location data
Seperated data logic for locations and put it into its own repository class for better organization and readability of controller classes. Data methods are still simple so no need for an interface class just yet, but will probably implement at a later date
This commit is contained in:
@ -3,29 +3,21 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\Location;
|
||||
use App\Models\Source;
|
||||
use App\Repositories\LocationRepository;
|
||||
|
||||
class FrontIndexController extends Controller
|
||||
{
|
||||
private $limit = 15;
|
||||
protected $locationRepository;
|
||||
|
||||
private function getRecent()
|
||||
public function __construct(LocationRepository $locationRepository)
|
||||
{
|
||||
$locations = Location::where("active", true)->orderByDesc('updated_at')->get();
|
||||
$list = [];
|
||||
foreach ($locations as $location) {
|
||||
if (($location->block_count + $location->silence_count) >= 2) {
|
||||
array_push($list, $location);
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
$this->locationRepository = $locationRepository;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
$list = $this->getRecent();
|
||||
$list = $this->locationRepository->getRecent();
|
||||
$latest_date = $list[0]->updated_at->format('Y M d');
|
||||
return view('front.index', [
|
||||
'count' => count($list),
|
||||
@ -39,20 +31,10 @@ class FrontIndexController extends Controller
|
||||
public function indexSearch(Request $request)
|
||||
{
|
||||
// this grabs the search results from the db
|
||||
$terms = $request->index_search;
|
||||
$rawSearch = $terms;
|
||||
$terms = str_replace(",", "", $terms);
|
||||
$terms = str_replace(" ", "|", $terms);
|
||||
$raw = DB::select("SELECT * FROM searchlocations(?)", [$terms]);
|
||||
$results = [];
|
||||
foreach ($raw as $item) {
|
||||
if (($item->block_count + $item->silence_count) >= 2) {
|
||||
array_push($results, $item);
|
||||
}
|
||||
}
|
||||
$results = $this->locationRepository->search($request);
|
||||
|
||||
//this gets recent updates to display under search results
|
||||
$list = $this->getRecent();
|
||||
$list = $this->locationRepository->getRecent();
|
||||
$latest_date = $list[0]->updated_at->format('Y M d');
|
||||
return view('front.index', [
|
||||
'count' => count($list),
|
||||
@ -60,7 +42,7 @@ class FrontIndexController extends Controller
|
||||
'recent' => $list,
|
||||
'results' => $results,
|
||||
'recent' => $list,
|
||||
'terms' => $terms,
|
||||
'terms' => $request->index_search,
|
||||
'latest_date' => $latest_date,
|
||||
'title' => "Search Results",
|
||||
]);
|
||||
@ -84,7 +66,7 @@ class FrontIndexController extends Controller
|
||||
|
||||
public function location(string $uuid = "1")
|
||||
{
|
||||
$location = Location::where("uuid", $uuid)->first();
|
||||
$location = $this->locationRepository->getLocation($uuid);
|
||||
$sources = Source::where("active", true)->get();
|
||||
$name = "NO LOCATION FOUND";
|
||||
if ($location) {
|
||||
@ -102,59 +84,16 @@ class FrontIndexController extends Controller
|
||||
|
||||
public function listings(int $pageNum = 1)
|
||||
{
|
||||
$locations = Location::where("active", true)->get();
|
||||
$active = [];
|
||||
foreach ($locations as $location) {
|
||||
if (($location->block_count + $location->silence_count) >= 2) {
|
||||
array_push($active, $location);
|
||||
}
|
||||
}
|
||||
|
||||
$pages = [];
|
||||
|
||||
if (count($active) != 0) {
|
||||
if (count($active) < $this->limit) {
|
||||
$this->limit = count($active) - 1;
|
||||
}
|
||||
$range = $pageNum * $this->limit - $this->limit;
|
||||
|
||||
if ($range != 0) {
|
||||
$range = $range + 1;
|
||||
}
|
||||
for ($i = 0; $i <= $this->limit; ++$i) {
|
||||
if (isset($active[$i + $range])) {
|
||||
array_push($pages, $active[$i + $range]);
|
||||
} else {
|
||||
// chill out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pageCount = ceil(count($active) / $this->limit);
|
||||
|
||||
if ($range != 0) {
|
||||
$range = $range + 1;
|
||||
}
|
||||
|
||||
$next = $pageNum + 1;
|
||||
if ($next > $pageCount) {
|
||||
$next = 1;
|
||||
}
|
||||
|
||||
$prev = $pageNum - 1;
|
||||
|
||||
if ($prev <= 0) {
|
||||
$prev = $pageCount;
|
||||
}
|
||||
$listing = $this->locationRepository->getPage($pageNum);
|
||||
|
||||
return view('front.listing', [
|
||||
'title' => "Listings",
|
||||
'sources' => count(Source::where("active", true)->get()),
|
||||
"totalPages" => $pageCount,
|
||||
"prev" => $prev,
|
||||
"next" => $next,
|
||||
"totalPages" => $listing[1],
|
||||
"prev" => $listing[2],
|
||||
"next" => $listing[3],
|
||||
'pageNum' => $pageNum,
|
||||
'locations' => $pages
|
||||
'locations' => $listing[0]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user