Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search rewrite #54

Merged
merged 71 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
315f819
moving stuff into Blade templates
Jun 14, 2023
1774103
search rewrite
Aug 2, 2023
15f07b6
Merge branch 'master' into search-rewrite
Aug 3, 2023
534b3f9
Add models to search controller
Aug 3, 2023
78fde68
Fix typo in Tag
Aug 3, 2023
80bbc57
Rename template var
Aug 3, 2023
d1afce2
Fix concatenatio operator
Aug 3, 2023
1076707
pass empty results
Aug 3, 2023
5abb9c5
try to return actual items
Aug 3, 2023
b7824ca
fix imports
Aug 3, 2023
e265fb7
Remove Vue
Aug 3, 2023
13f3865
Cleanup on search templates
Aug 3, 2023
0de60fc
Try to clean up strings
Aug 3, 2023
f5b42a9
String fixes
Aug 3, 2023
40a152b
Try to fix item passing
Aug 3, 2023
c413a20
Try to fix chosen
Aug 3, 2023
2396e53
More CSS tweaks
Aug 3, 2023
0c48142
More CSS tweaks
Aug 3, 2023
058e930
TL strings
Aug 3, 2023
bddff20
build Chosen CSS ourselves
Aug 4, 2023
a3bd829
Fix filepaths
Aug 4, 2023
e077de6
typo?
Aug 4, 2023
0f83529
typo?
Aug 4, 2023
5016ceb
revert
Aug 4, 2023
6fd74a5
Debug
Aug 4, 2023
a641f17
Change year sort items
Aug 4, 2023
08ce963
SCSS tweaks
Aug 4, 2023
437a5e1
Fix TL strings
Aug 4, 2023
dfa57eb
try to sort?
Aug 4, 2023
4bd8b3b
Fix sort?
Aug 4, 2023
3e65eef
Try to add year slider
Aug 4, 2023
efa3317
Merge pull request #39 from lolibrary/master
momijizukamori Aug 5, 2023
532a870
Merge branch 'master' into search-rewrite
Aug 7, 2023
c9340fb
update search
Aug 7, 2023
2c4efe9
More search refactor
Aug 8, 2023
a2694f9
Merge branch 'search-rewrite' of github.com:lolibrary/sakura into sea…
Aug 8, 2023
f2ad722
Merge branch 'master' into search-rewrite
Jan 1, 2024
19c41cd
test 'append'
Jan 1, 2024
5bfb752
append all input
Jan 1, 2024
cc60a6b
Try to clean up query params
Jan 1, 2024
4ab547d
Fix reference
Jan 1, 2024
01b6fc4
try to fix array mapping
Jan 1, 2024
3171b55
inline callback
Jan 1, 2024
7fd05a3
clean up param filters
Jan 13, 2024
208bd3f
Handle array inputs properly
Jan 13, 2024
3bd6daf
First iteration of javascript for search-rewrite. Also allows GET sea…
sleepycow Jan 16, 2024
4ab30d5
Restore old yarn.lock
sleepycow Jan 16, 2024
e5195f1
Fixed the window.pushState query string builder so that it properly a…
sleepycow Jan 16, 2024
43e3ecb
Removed stopPropagation so that the checked property on the matchers …
sleepycow Jan 16, 2024
a356c16
Slight reorganization
sleepycow Jan 16, 2024
f96a954
Rewritten to use object literal namespacing
sleepycow Jan 16, 2024
231cdfc
Fix docker compose
Jan 27, 2024
bc0ff12
Fix year search
Jan 28, 2024
780e90e
Fix max year for slider and fix clear filter button
Jan 28, 2024
f7e690d
Restore old yarn.lock
Jan 29, 2024
2d34dcf
Use tom-select instead of chosen
Feb 4, 2024
e3c15f9
Nicer handling of year matcher
Feb 7, 2024
315338b
Handle search errors better
Feb 8, 2024
e99d74e
Fix response.ok check
Feb 12, 2024
c50cbef
Merge branch 'master' into search-rewrite
Jun 19, 2024
915680b
Pin build repo version for branch builds
Jun 19, 2024
224d2d4
Pin builder image version number
Jun 19, 2024
af1ba72
Pull in changes from laravel upgrade branch
Jun 19, 2024
ff9999f
Import App facade into search controller
Jun 19, 2024
1f5c0e7
Try to fix card images
Jun 19, 2024
7e47aab
English stripping + fixing JS
Jun 19, 2024
87a5c07
Change sort order for matches
Jun 19, 2024
9934d85
Don't limit number of matches
Jun 19, 2024
a113bf4
Try to fix page handling in search
Jun 19, 2024
b42be9d
Try to fix page handling (again)
Jun 19, 2024
be245da
Fix paging in search
Jun 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
build-push-branch:
container:
image: ghcr.io/lolibrary/builder
image: ghcr.io/lolibrary/builder@sha256:80ca803642b16c17df149f402b4ea5d440eaef0fa19d54c9431dc4b9acfc6cb5
credentials:
username: ${{ github.actor }}
password: ${{ secrets.PAT }}
Expand All @@ -19,6 +19,7 @@ jobs:
with:
repository: lolibrary/build
token: ${{ secrets.PAT }}
ref: cea7b09494c6541c8bdfc7d919e1d58428061117
- name: Checkout Sakura
uses: actions/checkout@v2
with:
Expand Down
55 changes: 37 additions & 18 deletions app/Http/Controllers/Api/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,26 @@ class SearchController extends Base
Tag::class => 'tags',
];

/**
/**
* Search for items.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\App\Item[]
*/
public function search(SearchRequest $request)
public function search_index(SearchRequest $request)
{
$query = Item::query();

return search($request, $query);
}
/**
* Search for items.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\App\Item[]
*/
public function search(SearchRequest $request, Builder $query)
{
$this->filters($request, $query);
$this->years($request, $query);

Expand All @@ -60,11 +70,16 @@ public function search(SearchRequest $request)

$query->where('status', Item::PUBLISHED);

$paginator = $query->paginate(24);
$params = $this->form_to_query($request);

$paginator = $query->paginate(24)->appends($params);

$paginator->each(function (Item $item) {
$item->image = Storage::cloud()->url($item->image);
$item->makeVisible('image');
if ($item->image !== null) {
$item->makeVisible('image');
$item->image = Storage::cloud()->url($item->image);
$item->makeVisible('image');
}

if ($item->brand !== null) {
$item->brand->image = Storage::cloud()->url($item->brand->image);
Expand All @@ -80,6 +95,15 @@ public function search(SearchRequest $request)
return $paginator;
}

protected function form_to_query(Request $request)
{
$all_params = $request->all();

$filtered = array_filter($all_params, function($value, $key) { return !(str_contains($key, '_matcher') && $value == 'OR'); }, ARRAY_FILTER_USE_BOTH);

return $filtered;
}

/**
* Filter relationships.
*
Expand All @@ -93,7 +117,7 @@ protected function filters(Request $request, Builder $query)
[$singular, $plural] = [Str::singular($relation), Str::plural($relation)];

$models = (array) $request->input($plural) ?? $request->input($singular);
$matcher = $request->input($singular . "_matcher") ?? "OR";
$matcher = $request->input($plural . "_matcher") ?? "OR";

if (count($models) > 0) {
if ($matcher == "AND") {
Expand Down Expand Up @@ -131,22 +155,17 @@ protected function filters(Request $request, Builder $query)
*/
protected function years(Request $request, Builder $query)
{
$years = (array) ($request->input('years') ?? $request->input('year'));
$start_year = $request->input('start_year');
$end_year = $request->input('end_year');
$matcher = $request->input("year_matcher") ?? "OR";

if (count($years) > 0) {
if ($matcher == "AND") {
foreach ($years as $year) {
$query->where('year', $year);
}
if ($start_year && $end_year) {
if ($matcher == "OR") {
$query->whereBetween('year', [$start_year, $end_year]);

} elseif ($matcher == "NOT") {
$query->whereNotBetween('year', [$start_year, $end_year]);

$not_query = Item::query()->whereIn('year', $years)->select('id')->distinct();
$query->whereNotIn('id', $not_query);

} elseif ($matcher == "OR") {
$query->whereIn('year', $years);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function homepage()
->orderBy('published_at', 'desc')
->get();

$brands = Brand::all();
$categories = Category::all();
$brands = Brand::with('translations')->get();
$categories = Category::with('translations')->get();
$recent = Item::with(Item::PARTIAL_LOAD)
->whereNotNull('published_at')
->orderBy('published_at', 'desc')
Expand Down
61 changes: 59 additions & 2 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
<?php

namespace App\Http\Controllers;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Feature;
use App\Models\Color;
use App\Models\Item;
use App\Models\Tag;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\App;
use App\Http\Requests\Api\SearchRequest;
use App\Http\Controllers\Api\SearchController as ApiSearchController;


class SearchController extends Controller
{
public function index()
public function index(SearchRequest $request)
{
return view('search');
$items = $this->post($request);
$filters = $this->get_or_make_filters($request);

return view('search', ['filters' => $filters,'items' => $items]);
}

public function post(SearchRequest $request) {
$query = Item::query();
$search = new ApiSearchController();
$items = $search->search($request, $query);

return view('components.search-results', ['items' => $items, 'max_year' => (date('Y') + 3)]);
}

public function get_or_make_filters(SearchRequest $request) {
// Closure so we don't have to edit this multiple places if things change
$make_filters = function() {
return view('components.filters', ['sections' => [
'categories' => Category::cached()->sortBy('name'),
'brands' => Brand::cached()->sortBy('name'),
'features' => Feature::cached()->sortBy('name'),
'colors' => Color::cached()->sortBy('name'),
'tags' => Tag::cached()->sortBy('name'),]
])->render();
};

// Check if there are any filters set. If not, we can use cached renders.
// 'search' doesn't count because it doesn't affect the filter view.
$params = $request->all();
$count = count($params);

if ($count == 0 || ($count == 1 && array_key_exists('search', $params))) {
$locale = App::getLocale();
$filters = cache()->tags(['filters'])->get($locale);
if (!$filters) {
$filters = $make_filters();
cache()->tags(['filters'])->forever($locale, $filters);
return $filters;
} else {
return $filters;
}
} else {
return $make_filters();
}
}
}
24 changes: 21 additions & 3 deletions app/Http/Requests/Api/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public function authorize()
return true;
}

/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
if (!empty($this->year)) {
$years = array_map('intval', explode(",", $this->year));
sort($years);
$multiple = count($years) > 1;
$this->merge([
'start_year' => $years[0],
'end_year' => ($multiple ? end($years) : date('Y') + 3)
]);
}
}


/**
* Get a list of rules for this request.
*
Expand Down Expand Up @@ -69,9 +88,8 @@ public function rules()
'tags' => 'sometimes|array',
'tags.*' => 'required|string|exists:tags,slug',

'year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
'years' => 'sometimes|array',
'years.*' => 'required|integer|min:1970|max:'.(date('Y') + 3),
'start_year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
'end_year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
];
}
}
24 changes: 21 additions & 3 deletions app/Http/Requests/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
*/
class SearchRequest extends FormRequest
{

/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
if (!empty($this->year)) {
$years = array_map('intval', explode(",", $this->year));
sort($years);
$multiple = count($years) > 1;
$this->merge([
'start_year' => $years[0],
'end_year' => ($multiple ? end($years) : date('Y') + 3)
]);
}
}

/**
* Check if this request is authorized.
*
Expand Down Expand Up @@ -69,9 +88,8 @@ public function rules()
'tags' => 'sometimes|array',
'tags.*' => 'required|string|exists:tags,slug',

'year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
'years' => 'sometimes|array',
'years.*' => 'required|integer|min:1970|max:'.(date('Y') + 3),
'start_year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
'end_year' => 'sometimes|required|integer|min:1970|max:'.(date('Y') + 3),
];
}
}
13 changes: 10 additions & 3 deletions app/Models/Traits/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait Cacheable
public static function cached()
{
return cache()->rememberForever(static::cacheKey(), function () {
return static::all();
return static::with('translations')->get();
});
}

Expand All @@ -28,7 +28,7 @@ public static function cacheKey()
$locale = App::getLocale();
$key = mb_strtolower(class_basename(static::class));

return 'models:'.$locale.':'.$key;
return 'models:'.$key;
}

/**
Expand All @@ -49,8 +49,15 @@ public static function bust()
*/
protected static function bootCacheable()
{
static::saving(function () {
static::saved(function () {
static::bust();
static::bust();
cache()->tags('filters')->flush();
});

static::deleted(function () {
static::bust();
cache()->tags('filters')->flush();
});
}
}
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use App\Composers;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -49,5 +50,6 @@ public function boot()
Blade::if('dev', function () {
return auth()->check() && auth()->user()->developer();
});

}
}
2 changes: 1 addition & 1 deletion dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ cd /srv
if [[ "$1" == "SETUP" ]]; then
composer config http-basic.nova.laravel.com "$NOVA_USERNAME" "$NOVA_API_KEY" --no-interaction
COMPOSER_MEMORY_LIMIT=-1 composer install --no-interaction

cp fortawesome* ../
npm install
npm run development
else
COMPOSER_MEMORY_LIMIT=-1 composer install --no-interaction
npm install
npm run development

Expand Down
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ services:
- ./tests:/srv/tests
- ./storage:/srv/storage
- ./config:/srv/config
- ./composer.json:/srv/composer.json
- ./composer.lock:/srv/composer.lock
- ./dev.sh:/srv/dev.sh
- ./server.php:/srv/server.php
- ./package.json:/srv/package.json
- ./yarn.lock:/srv/yarn.lock
ports:
- "3000:3000"
depends_on:
- db
- db
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0",
"vue": "2.6.11",
"vue-template-compiler": "2.6.11"
"vue-template-compiler": "^2.7.16"
},
"dependencies": {
"@fortawesome/fontawesome-pro": "file:/../fortawesome-fontawesome-pro-5.15.1.tgz",
"bootstrap4c-chosen": "^1.1.1",
"bootstrap-slider": "11.0.2",
"qs": "^6.8.0",
"sass-svg-uri": "^1.0.0",
"simple-lightbox": "^2.1.0",
"vue-select": "^3.1.0"
"tom-select": "^2.3.1"
}
}
Loading
Loading