mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-01-18 05:38:40 +01:00
Add separate API call to get notes of a link
This commit is contained in:
parent
92ebe792e4
commit
09c8098971
25
app/Http/Controllers/API/LinkNotesController.php
Normal file
25
app/Http/Controllers/API/LinkNotesController.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Link;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class LinkNotesController extends Controller
|
||||
{
|
||||
/**
|
||||
* Get the notes for a specific link
|
||||
*
|
||||
* @param $linkID
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function __invoke($linkID): JsonResponse
|
||||
{
|
||||
$link = Link::findOrFail($linkID);
|
||||
|
||||
$notes = $link->notes()->paginate(getPaginationLimit());
|
||||
|
||||
return response()->json($notes);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\API\LinkController;
|
||||
use App\Http\Controllers\API\LinkNotesController;
|
||||
use App\Http\Controllers\API\ListController;
|
||||
use App\Http\Controllers\API\ListLinksController;
|
||||
use App\Http\Controllers\API\TagController;
|
||||
@ -28,6 +29,9 @@ Route::prefix('v1')->group(function () {
|
||||
'destroy' => 'api.links.destroy',
|
||||
]);
|
||||
|
||||
Route::get('links/{link}/notes', LinkNotesController::class)
|
||||
->name('api.links.notes');
|
||||
|
||||
Route::apiResource('lists', ListController::class)
|
||||
->names([
|
||||
'index' => 'api.lists.index',
|
||||
|
72
tests/Feature/Controller/API/LinkNotesTest.php
Normal file
72
tests/Feature/Controller/API/LinkNotesTest.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Controller\Models;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Models\Note;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LinkNotesTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use DatabaseMigrations;
|
||||
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = factory(User::class)->create();
|
||||
}
|
||||
|
||||
public function testLinksRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
$note = factory(Note::class)->create([
|
||||
'link_id' => $link->id,
|
||||
]);
|
||||
|
||||
$response = $this->getJson('api/v1/links/1/notes', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'data' => [
|
||||
['note' => $note->note],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testLinksRequestWithoutLinks(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
|
||||
$response = $this->getJson('api/v1/links/1/notes', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'data' => [],
|
||||
]);
|
||||
|
||||
$responseBody = json_decode($response->content());
|
||||
|
||||
$this->assertEmpty($responseBody->data);
|
||||
}
|
||||
|
||||
public function testShowRequestNotFound(): void
|
||||
{
|
||||
$response = $this->getJson('api/v1/links/1/notes', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
protected function generateHeaders(): array
|
||||
{
|
||||
return [
|
||||
'Authorization' => 'Bearer ' . $this->user->api_token,
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user