diff --git a/app/Http/Controllers/AppealController.php b/app/Http/Controllers/AppealController.php index c676a3d..31536bc 100644 --- a/app/Http/Controllers/AppealController.php +++ b/app/Http/Controllers/AppealController.php @@ -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, diff --git a/app/Http/Controllers/FrontIndexController.php b/app/Http/Controllers/FrontIndexController.php index 869cb4c..7507d00 100644 --- a/app/Http/Controllers/FrontIndexController.php +++ b/app/Http/Controllers/FrontIndexController.php @@ -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] ]); } } diff --git a/app/Http/Controllers/LocationController.php b/app/Http/Controllers/LocationController.php index e8ff44a..ea28d5a 100644 --- a/app/Http/Controllers/LocationController.php +++ b/app/Http/Controllers/LocationController.php @@ -189,7 +189,7 @@ class LocationController extends Controller ]); } } - + //TODO: Send update post to TBS social account return back()->with('message', $duplicates . ' UPDATED - ' . $fresh . ' CREATED'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..6b854fa 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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()); + }); } /** diff --git a/app/Repositories/LocationRepository.php b/app/Repositories/LocationRepository.php new file mode 100644 index 0000000..4d06088 --- /dev/null +++ b/app/Repositories/LocationRepository.php @@ -0,0 +1,102 @@ +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]; + } +}