mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-03-14 19:59:38 +01:00
Implement first version of the link model API
This commit is contained in:
parent
91ca65c1ca
commit
86db497cce
95
app/Http/Controllers/API/LinkController.php
Normal file
95
app/Http/Controllers/API/LinkController.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Models\LinkDeleteRequest;
|
||||
use App\Http\Requests\Models\LinkStoreRequest;
|
||||
use App\Http\Requests\Models\LinkUpdateRequest;
|
||||
use App\Models\Link;
|
||||
use App\Repositories\LinkRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class LinkController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$links = Link::byUser(auth()->id())
|
||||
->orderBy(
|
||||
$request->get('order_by', 'created_at'),
|
||||
$request->get('order_dir', 'DESC')
|
||||
)
|
||||
->paginate(getPaginationLimit());
|
||||
|
||||
return response()->json($links);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param LinkStoreRequest $request
|
||||
* @return Response
|
||||
*/
|
||||
public function store(LinkStoreRequest $request)
|
||||
{
|
||||
$link = LinkRepository::create($request->all());
|
||||
|
||||
return response()->json($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$link = Link::findOrFail($id);
|
||||
|
||||
return response()->json($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param LinkUpdateRequest $request
|
||||
* @param int $id
|
||||
* @return Response
|
||||
*/
|
||||
public function update(LinkUpdateRequest $request, $id)
|
||||
{
|
||||
$link = Link::findOrFail($id);
|
||||
|
||||
$updatedLink = LinkRepository::update($link, $request->all());
|
||||
|
||||
return response()->json($updatedLink);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param LinkDeleteRequest $request
|
||||
* @param int $id
|
||||
* @return Response
|
||||
*/
|
||||
public function destroy(LinkDeleteRequest $request, $id)
|
||||
{
|
||||
$link = Link::findOrFail($id);
|
||||
|
||||
$deletionSuccessfull = LinkRepository::delete($link);
|
||||
|
||||
if ($deletionSuccessfull) {
|
||||
return response(null, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\API\LinkController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@ -12,3 +12,9 @@ use Illuminate\Http\Request;
|
||||
| is assigned the "api" middleware group. Enjoy building your API!
|
||||
|
|
||||
*/
|
||||
|
||||
Route::prefix('v1')->group(function () {
|
||||
Route::middleware('auth:api')->group(function () {
|
||||
Route::apiResource('links', LinkController::class);
|
||||
});
|
||||
});
|
||||
|
205
tests/Feature/Controller/API/LinkApiTest.php
Normal file
205
tests/Feature/Controller/API/LinkApiTest.php
Normal file
@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Controller\Models;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Models\LinkList;
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LinkApiTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
use DatabaseMigrations;
|
||||
|
||||
private $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user = factory(User::class)->create();
|
||||
}
|
||||
|
||||
public function testUnauthorizedRequest(): void
|
||||
{
|
||||
$response = $this->getJson('api/v1/links');
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function testIndexRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
|
||||
$response = $this->getJson('api/v1/links', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'data' => [
|
||||
['url' => $link->url],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testMinimalCreateRequest(): void
|
||||
{
|
||||
$response = $this->postJson('api/v1/links', [
|
||||
'url' => 'https://duckduckgo.com',
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'url' => 'https://duckduckgo.com',
|
||||
]);
|
||||
|
||||
$databaseLink = Link::first();
|
||||
|
||||
$this->assertEquals('https://duckduckgo.com', $databaseLink->url);
|
||||
}
|
||||
|
||||
public function testFullCreateRequest(): void
|
||||
{
|
||||
$list = factory(LinkList::class)->create();
|
||||
$tag = factory(Tag::class)->create();
|
||||
|
||||
$response = $this->postJson('api/v1/links', [
|
||||
'url' => 'https://duckduckgo.com',
|
||||
'title' => 'Search the Web',
|
||||
'description' => 'There could be a description here',
|
||||
'lists' => [$list->id],
|
||||
'tags' => [$tag->id],
|
||||
'is_private' => false,
|
||||
'check_disabled' => false,
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'url' => 'https://duckduckgo.com',
|
||||
]);
|
||||
|
||||
$databaseLink = Link::first();
|
||||
|
||||
$this->assertEquals('https://duckduckgo.com', $databaseLink->url);
|
||||
}
|
||||
|
||||
public function testInvalidCreateRequest(): void
|
||||
{
|
||||
$response = $this->postJson('api/v1/links', [
|
||||
'url' => null,
|
||||
'lists' =>'no array',
|
||||
'tags' => 123,
|
||||
'is_private' => 'hello',
|
||||
'check_disabled' => 'bla',
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(422)
|
||||
->assertJsonValidationErrors([
|
||||
'url',
|
||||
'lists',
|
||||
'tags',
|
||||
'is_private',
|
||||
'check_disabled',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testShowRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
|
||||
$response = $this->getJson('api/v1/links/1', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'url' => $link->url,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testShowRequestNotFound(): void
|
||||
{
|
||||
$response = $this->getJson('api/v1/links/1', $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
public function testUpdateRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
$list = factory(LinkList::class)->create();
|
||||
|
||||
$response = $this->patchJson('api/v1/links/1', [
|
||||
'url' => 'https://duckduckgo.com',
|
||||
'title' => 'Custom Title',
|
||||
'description' => 'Custom Description',
|
||||
'lists' => [$list->id],
|
||||
'is_private' => false,
|
||||
'check_disabled' => false,
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson([
|
||||
'url' => 'https://duckduckgo.com',
|
||||
]);
|
||||
|
||||
$databaseLink = Link::first();
|
||||
|
||||
$this->assertEquals('https://duckduckgo.com', $databaseLink->url);
|
||||
}
|
||||
|
||||
public function testInvalidUpdateRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
|
||||
$response = $this->patchJson('api/v1/links/1', [
|
||||
//
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(422)
|
||||
->assertJsonValidationErrors([
|
||||
'url',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUpdateRequestNotFound(): void
|
||||
{
|
||||
$response = $this->patchJson('api/v1/links/1', [
|
||||
'url' => 'https://duckduckgo.com',
|
||||
'title' => 'Custom Title',
|
||||
'description' => 'Custom Description',
|
||||
'lists' => [],
|
||||
'tags' => [],
|
||||
'is_private' => false,
|
||||
'check_disabled' => false,
|
||||
], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
public function testDeleteRequest(): void
|
||||
{
|
||||
$link = factory(Link::class)->create();
|
||||
|
||||
$response = $this->deleteJson('api/v1/links/1', [], $this->generateHeaders());
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
$this->assertEquals(0, Link::count());
|
||||
}
|
||||
|
||||
public function testDeleteRequestNotFound(): void
|
||||
{
|
||||
$response = $this->deleteJson('api/v1/links/1', [], $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