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:
ro 2024-02-09 14:53:08 -06:00
parent bce9a430aa
commit da0ddb3ef0
5 changed files with 123 additions and 77 deletions

View file

@ -30,6 +30,7 @@ class AppealController extends Controller
'error' => 'Appeal already in process for Location',
]);
} else {
//TODO: Add empty string filtering and check if location exists in DB
$new = Appeal::create([
'uuid' => Uuid::uuid4(),
'location' => $request->location,

View file

@ -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]
]);
}
}

View file

@ -189,7 +189,7 @@ class LocationController extends Controller
]);
}
}
//TODO: Send update post to TBS social account
return back()->with('message', $duplicates . ' UPDATED - ' . $fresh . ' CREATED');
}
}

View file

@ -3,6 +3,8 @@
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Repositories\LocationRepository;
use App\Models\Location;
class AppServiceProvider extends ServiceProvider
{
@ -11,7 +13,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
$this->app->bind(LocationRepository::class, function ($app) {
return new LocationRepository(new Location());
});
}
/**

View file

@ -0,0 +1,102 @@
<?php
namespace App\Repositories;
use App\Models\Location;
use Illuminate\Support\Facades\DB;
class LocationRepository
{
protected $model;
private $limit = 15;
public function __construct(Location $model)
{
$this->model = $model;
}
public function search($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);
}
}
return $results;
}
public function getLocation($uuid)
{
return $this->model::where("uuid", $uuid)->first();
}
public function getRecent()
{
$locations = $this->model::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;
}
public function getPage($pageNum)
{
$locations = $this->model::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;
}
return $result = [$pages, $pageCount, $prev, $next];
}
}