2018-05-26 22:50:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Hashtag;
|
|
|
|
use App\Profile;
|
2018-05-26 22:50:36 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
class SearchController extends Controller
|
|
|
|
{
|
|
|
|
public function searchAPI(Request $request, $tag)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$res = Cache::remember('api:search:tag:'.$tag, 1440, function () use ($tag) {
|
|
|
|
$res = Hashtag::where('slug', 'like', '%'.$tag.'%')->get();
|
|
|
|
$tags = $res->map(function ($item, $key) {
|
|
|
|
return [
|
|
|
|
'count' => $item->posts()->count(),
|
|
|
|
'url' => $item->url(),
|
|
|
|
'type' => 'hashtag',
|
|
|
|
'value' => $item->name,
|
2018-05-26 22:50:36 +00:00
|
|
|
'tokens' => explode('-', $item->name),
|
2018-08-28 03:07:36 +00:00
|
|
|
'name' => null,
|
2018-05-26 22:50:36 +00:00
|
|
|
];
|
2018-08-28 03:07:36 +00:00
|
|
|
});
|
|
|
|
$res = Profile::where('username', 'like', '%'.$tag.'%')->get();
|
|
|
|
$profiles = $res->map(function ($item, $key) {
|
|
|
|
return [
|
|
|
|
'count' => 0,
|
|
|
|
'url' => $item->url(),
|
|
|
|
'type' => 'profile',
|
|
|
|
'value' => $item->username,
|
2018-05-26 22:50:36 +00:00
|
|
|
'tokens' => [$item->username],
|
2018-08-28 03:07:36 +00:00
|
|
|
'name' => $item->name,
|
2018-05-26 22:50:36 +00:00
|
|
|
];
|
2018-08-28 03:07:36 +00:00
|
|
|
});
|
|
|
|
$tags = $tags->push($profiles[0]);
|
|
|
|
|
|
|
|
return $tags;
|
2018-05-26 22:50:36 +00:00
|
|
|
});
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
return response()->json($res);
|
2018-05-26 22:50:36 +00:00
|
|
|
}
|
|
|
|
}
|