Skip to content

Commit

Permalink
Show filesystem and associated names in quick search.
Browse files Browse the repository at this point in the history
  • Loading branch information
pierobot committed Jun 6, 2020
1 parent 06ee5f0 commit dd92fba
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App\Http\Controllers;

use App\AssociatedName;
use App\AssociatedNameReference;
use App\Library;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -151,6 +155,8 @@ public function postAdvanced(Request $request)
* Perform a quick search on a series' name and associated names using the like operator.
* Requires the url parameter 'query' to be present. (?query=xxx)
*
* TODO: Is there a way to optimize the queries in this method?
*
* @return \Illuminate\Http\JsonResponse
*/
public function autoComplete()
Expand All @@ -160,16 +166,28 @@ public function autoComplete()
$libraries = $user->libraries()->toArray();
$searchQuery = request()->get('query');

/** @var Builder $items */
$items = Manga::where('name', 'like', "%$searchQuery%");

$items = $items->orWhereHas('associatedNames', function (Builder $query) use ($searchQuery) {
$query->where('name', 'like', "%$searchQuery%");
});

// filter out the items the user cannot access
$items = $items->whereIn('library_id', $libraries)->get(['id', 'name']);

return response()->json($items);
/** @var Collection $items */
$items = Manga::query()
->where('name', 'like', "%$searchQuery%")
->get(['id', 'name']);

$associatedNameQuery = AssociatedNameReference::query()
->whereHas('associatedName', function (Builder $query) use ($searchQuery) {
$query->select(['id'])
->where('name', 'like', "%$searchQuery%");
})
->select(['manga_id', 'associated_name_id']);

$associatedItems = Manga::query()
->joinSub($associatedNameQuery, 'associatedNameReferences', function (JoinClause $join) {
$join->on('manga.id', '=', 'manga_id');
})
->join('associated_names', function (JoinClause $join) {
$join->on('associated_name_id', '=', 'associated_names.id');
})
->select(['manga.id', 'associated_names.name'])
->get();

return response()->json($items->merge($associatedItems));
}
}

0 comments on commit dd92fba

Please sign in to comment.