mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-01-29 19:07:48 +01:00
Add new endpoint to check an url for existing links (#6)
This commit is contained in:
parent
3ffe6daadb
commit
3eb941bdb8
32
app/Http/Controllers/API/LinkCheckController.php
Normal file
32
app/Http/Controllers/API/LinkCheckController.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Link;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LinkCheckController extends Controller
|
||||
{
|
||||
/**
|
||||
* Search for a link based on a given url
|
||||
*
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$searchedUrl = $request->input('url', false);
|
||||
|
||||
if (!$searchedUrl) {
|
||||
return response()->json(['linksFound' => false]);
|
||||
}
|
||||
|
||||
$linkCount = Link::byUser($request->user()->id)
|
||||
->where('url', trim($searchedUrl))
|
||||
->count();
|
||||
|
||||
return response()->json(['linksFound' => $linkCount > 0]);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\API\LinkCheckController;
|
||||
use App\Http\Controllers\API\LinkController;
|
||||
use App\Http\Controllers\API\LinkNotesController;
|
||||
use App\Http\Controllers\API\ListController;
|
||||
@ -22,6 +23,10 @@ use App\Http\Controllers\API\TagLinksController;
|
||||
|
||||
Route::prefix('v1')->group(function () {
|
||||
Route::middleware('auth:api')->group(function () {
|
||||
|
||||
Route::get('links/check', LinkCheckController::class)
|
||||
->name('api.links.check');
|
||||
|
||||
Route::apiResource('links', LinkController::class)
|
||||
->names([
|
||||
'index' => 'api.links.index',
|
||||
|
58
tests/Controller/API/LinkCheckApiTest.php
Normal file
58
tests/Controller/API/LinkCheckApiTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Controller\API;
|
||||
|
||||
use App\Models\Link;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class LinkCheckApiTest extends ApiTestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use DatabaseMigrations;
|
||||
|
||||
public function testUnauthorizedRequest(): void
|
||||
{
|
||||
$response = $this->getJson('api/v1/links/check');
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function testSuccessfulLinkCheck(): void
|
||||
{
|
||||
factory(Link::class)->create([
|
||||
'url' => 'https://example.com',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('api/v1/links/check?url=https://example.com', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'linksFound' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testNegativeLinkCheck(): void
|
||||
{
|
||||
factory(Link::class)->create([
|
||||
'url' => 'https://test.com',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('api/v1/links/check?url=https://example.com', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'linksFound' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testCheckWithoutQuery(): void
|
||||
{
|
||||
$response = $this->getJson('api/v1/links/check', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'linksFound' => false,
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user