2018-09-06 23:48:54 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Ejaculation;
|
|
|
|
use App\Tag;
|
2020-07-30 23:12:29 +09:00
|
|
|
use App\Utilities\Formatter;
|
2018-09-06 23:48:54 +09:00
|
|
|
use Illuminate\Http\Request;
|
2019-04-07 00:10:01 +09:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-06 23:48:54 +09:00
|
|
|
|
|
|
|
class SearchController extends Controller
|
|
|
|
{
|
2020-07-30 23:12:29 +09:00
|
|
|
/** @var Formatter */
|
|
|
|
private $formatter;
|
|
|
|
|
|
|
|
public function __construct(Formatter $formatter)
|
|
|
|
{
|
|
|
|
$this->formatter = $formatter;
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:48:54 +09:00
|
|
|
public function index(Request $request)
|
|
|
|
{
|
|
|
|
$inputs = $request->validate([
|
|
|
|
'q' => 'required'
|
|
|
|
]);
|
|
|
|
|
2020-07-30 23:12:29 +09:00
|
|
|
$q = $this->normalizeQuery($inputs['q']);
|
2018-09-06 23:48:54 +09:00
|
|
|
$results = Ejaculation::query()
|
2020-07-30 23:12:29 +09:00
|
|
|
->whereHas('tags', function ($query) use ($q) {
|
|
|
|
$query->where('normalized_name', 'like', "%{$q}%");
|
2018-09-06 23:48:54 +09:00
|
|
|
})
|
2019-04-07 00:10:01 +09:00
|
|
|
->whereHas('user', function ($query) {
|
|
|
|
$query->where('is_protected', false);
|
|
|
|
if (Auth::check()) {
|
|
|
|
$query->orWhere('id', Auth::id());
|
|
|
|
}
|
|
|
|
})
|
2018-09-06 23:48:54 +09:00
|
|
|
->where('is_private', false)
|
|
|
|
->orderBy('ejaculated_date', 'desc')
|
|
|
|
->with(['user', 'tags'])
|
2019-04-05 23:10:26 +09:00
|
|
|
->withLikes()
|
2019-02-07 21:34:57 +09:00
|
|
|
->paginate(20)
|
|
|
|
->appends($inputs);
|
2018-09-06 23:48:54 +09:00
|
|
|
|
|
|
|
return view('search.index')->with(compact('inputs', 'results'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function relatedTag(Request $request)
|
|
|
|
{
|
|
|
|
$inputs = $request->validate([
|
|
|
|
'q' => 'required'
|
|
|
|
]);
|
|
|
|
|
2020-07-30 23:12:29 +09:00
|
|
|
$q = $this->normalizeQuery($inputs['q']);
|
2018-09-06 23:48:54 +09:00
|
|
|
$results = Tag::query()
|
2020-07-30 23:12:29 +09:00
|
|
|
->where('normalized_name', 'like', "%{$q}%")
|
2019-02-07 21:34:57 +09:00
|
|
|
->paginate(50)
|
|
|
|
->appends($inputs);
|
2018-09-06 23:48:54 +09:00
|
|
|
|
|
|
|
return view('search.relatedTag')->with(compact('inputs', 'results'));
|
|
|
|
}
|
2020-07-30 23:12:29 +09:00
|
|
|
|
|
|
|
private function normalizeQuery(string $query): string
|
|
|
|
{
|
|
|
|
return $this->formatter->normalizeTagName($query);
|
|
|
|
}
|
2019-01-15 00:05:01 +09:00
|
|
|
}
|