2018-09-06 22:53:16 +02:00
|
|
|
<?php
|
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
namespace App\Http\Controllers;
|
2018-09-06 22:53:16 +02:00
|
|
|
|
2019-04-27 10:56:18 +02:00
|
|
|
use App\Models\Link;
|
2019-10-29 16:58:29 +01:00
|
|
|
use App\Models\LinkList;
|
2018-09-06 22:53:16 +02:00
|
|
|
use App\Models\Tag;
|
2020-01-28 13:58:18 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-09-06 22:53:16 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
class FetchController extends Controller
|
2018-09-06 22:53:16 +02:00
|
|
|
{
|
2020-01-28 13:58:18 +01:00
|
|
|
public function getTags(Request $request): JsonResponse
|
2018-09-09 21:24:08 +02:00
|
|
|
{
|
2018-09-06 22:53:16 +02:00
|
|
|
$query = $request->get('query', false);
|
|
|
|
|
|
|
|
if (!$query) {
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tags = Tag::byUser(auth()->user()->id)
|
|
|
|
->where('name', 'like', '%' . $query . '%')
|
|
|
|
->orderBy('name', 'asc')
|
2019-10-29 16:58:29 +01:00
|
|
|
->get();
|
|
|
|
|
|
|
|
if (!$tags->isEmpty()) {
|
2020-01-28 13:58:18 +01:00
|
|
|
// Properly format the results to be used by Selectize
|
2019-10-29 16:58:29 +01:00
|
|
|
$tags = $tags->map(function ($item) {
|
|
|
|
return [
|
|
|
|
'value' => $item->name,
|
|
|
|
'text' => $item->name,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($tags);
|
|
|
|
}
|
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
public function getLists(Request $request): JsonResponse
|
2019-10-29 16:58:29 +01:00
|
|
|
{
|
|
|
|
$query = $request->get('query', false);
|
|
|
|
|
|
|
|
if (!$query) {
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tags = LinkList::byUser(auth()->user()->id)
|
|
|
|
->where('name', 'like', '%' . $query . '%')
|
|
|
|
->orderBy('name', 'asc')
|
2018-09-06 22:53:16 +02:00
|
|
|
->get();
|
|
|
|
|
|
|
|
if (!$tags->isEmpty()) {
|
2020-01-28 13:58:18 +01:00
|
|
|
// Properly format the results to be used by Selectize
|
2018-09-09 21:24:08 +02:00
|
|
|
$tags = $tags->map(function ($item) {
|
2018-09-06 22:53:16 +02:00
|
|
|
return [
|
|
|
|
'value' => $item->name,
|
|
|
|
'text' => $item->name,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($tags);
|
|
|
|
}
|
2019-04-27 10:56:18 +02:00
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
public function searchExistingUrls(Request $request): JsonResponse
|
2019-04-27 10:56:18 +02:00
|
|
|
{
|
|
|
|
$query = $request->get('query', false);
|
|
|
|
|
|
|
|
if (!$query) {
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
$linkCount = Link::byUser(auth()->user()->id)
|
2019-04-27 10:56:18 +02:00
|
|
|
->where('url', trim($query))
|
2020-01-28 13:58:18 +01:00
|
|
|
->count();
|
2019-04-27 10:56:18 +02:00
|
|
|
|
2020-01-28 13:58:18 +01:00
|
|
|
return response()->json(['linkFound' => $linkCount > 0]);
|
2019-04-27 10:56:18 +02:00
|
|
|
}
|
2018-09-06 22:53:16 +02:00
|
|
|
}
|