Skip to content

Commit

Permalink
Add SearchQuery compatbility
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Mulder committed Oct 11, 2023
1 parent eeae34b commit ad2babd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'sales_order_address' => Rapidez\Core\Models\SalesOrderAddress::class,
'sales_order_item' => Rapidez\Core\Models\SalesOrderItem::class,
'sales_order_payment' => Rapidez\Core\Models\SalesOrderPayment::class,
'search_query' => Rapidez\Core\Models\SearchQuery::class,
],

// The fully qualified class names of the widgets.
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

use Rapidez\Core\Http\Controllers\FallbackController;
use Rapidez\Core\Http\Controllers\HealthcheckController;
use Rapidez\Core\Http\Controllers\SearchController;
use Rapidez\Core\Http\Middleware\AuthenticateHealthCheck;

Route::get('healthcheck', HealthcheckController::class)->middleware(AuthenticateHealthCheck::class);

Route::middleware('web')->group(function () {
Route::view('cart', 'rapidez::cart.overview')->name('cart');
Route::view('checkout', 'rapidez::checkout.overview')->name('checkout');
Route::view('search', 'rapidez::search.overview')->name('search');
Route::get('search', SearchController::class)->name('search');
Route::fallback(FallbackController::class);
});
31 changes: 31 additions & 0 deletions src/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rapidez\Core\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

class SearchController
{
public function __invoke(Request $request)
{
$searchQuery = config('rapidez.models.search_query')::firstOrNew([
'query_text' => Str::lower($request->q),
'store_id' => config('rapidez.store'),
], ['popularity' => 1]);

if (! $searchQuery->exists) {
$searchQuery->save();

return view('rapidez::search.overview');
}

$searchQuery->increment('popularity');

if ($searchQuery->is_active === 1 && $searchQuery->redirect) {
return redirect($searchQuery->redirect, 301);
}

return view('rapidez::search.overview');
}
}
16 changes: 16 additions & 0 deletions src/Models/SearchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rapidez\Core\Models;

use Illuminate\Database\Eloquent\Model;

class SearchQuery extends Model
{
const CREATED_AT = null;

protected $table = 'search_query';

protected $primaryKey = 'query_id';

protected $guarded = [];
}

0 comments on commit ad2babd

Please sign in to comment.