Skip to content

Commit

Permalink
feat(api): add public endpoint for querying wikis
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 22, 2023
1 parent 62f47d5 commit 12443aa
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/Http/Controllers/PublicWikiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Wiki;
use App\Http\Resources\PublicWikiResource;
use App\Http\Resources\PublicWikiCollection;

class PublicWikiController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$perPage = $request->query('per_page', null);
if ($perPage !== null) {
$perPage = intval($perPage);
}
return new PublicWikiCollection(Wiki::query()->paginate($perPage));
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request): \Illuminate\Http\JsonResponse
{
return response()->json(['message' => 'Method not allowed'], 405);
}

/**
* Display the specified resource.
*/
public function show($id)
{
return new PublicWikiResource(Wiki::findOrFail($id));
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): \Illuminate\Http\JsonResponse
{
return response()->json(['message' => 'Method not allowed'], 405);
}

/**
* Remove the specified resource from storage.
*/
public function destroy($id): \Illuminate\Http\JsonResponse
{
return response()->json(['message' => 'Method not allowed'], 405);
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/PublicWikiCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PublicWikiCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/PublicWikiResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PublicWikiResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'description' => $this->description,
'domain' => $this->domain,
'sitename' => $this->sitename,
'wiki_site_stats' => $this->wikiSiteStats,
];
}
}
2 changes: 2 additions & 0 deletions app/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Wiki extends Model
'deleted_at',
];

protected $perPage = 20;

public function wikiDbVersion()
{
/**
Expand Down
26 changes: 26 additions & 0 deletions database/factories/WikSiteStatsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use App\WikiSiteStats;
use Illuminate\Database\Eloquent\Factories\Factory;

class WikiSiteStatsFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = WikiSiteStats::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [];
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$router->post('user/resetPassword', ['uses' => 'Auth\ResetPasswordController@reset']);
$router->post('contact/sendMessage', ['uses' => 'ContactController@sendMessage']);

$router->apiResource('wiki', 'PublicWikiController');

// Authed
$router->group(['middleware' => ['auth:api']], function () use ($router) {

Expand Down
118 changes: 118 additions & 0 deletions tests/Routes/Wiki/PublicWikiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Tests\Routes\Wiki;

use Tests\Routes\Traits\OptionsRequestAllowed;
use Tests\TestCase;
use App\WikiSiteStats;
use App\Wiki;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class PublicWikiTest extends TestCase
{
protected $route = 'wiki';

use OptionsRequestAllowed;
use DatabaseTransactions;

public function setUp(): void {
parent::setUp();
Wiki::query()->delete();
WikiSiteStats::query()->delete();
}

public function tearDown(): void {
Wiki::query()->delete();
WikiSiteStats::query()->delete();
parent::tearDown();
}

public function testEmpty()
{
$this->json('GET', $this->route.'/12')
->assertStatus(404)
->assertJsonStructure(['message']);

$this->json('GET', $this->route)
->assertStatus(200)
->assertJsonPath('data', []);
}

public function testBadMethods()
{
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]);

$this->json('DELETE', $this->route.'/'.$wiki->id)
->assertStatus(405)
->assertJsonStructure(['message']);

$this->assertEquals(Wiki::where('id', $wiki->id)->count(), 1);

$this->json('PATCH', $this->route.'/'.$wiki->id, ['domain' => 'foo.wikibase.cloud'])
->assertStatus(405)
->assertJsonStructure(['message']);

$this->assertEquals(Wiki::where('id', $wiki->id)->first()->getAttribute('domain'), 'one.wikibase.cloud');

$this->json('POST', $this->route, ['domain' => 'create.wikibase.cloud'])
->assertStatus(405)
->assertJsonStructure(['message']);
}

public function testGetOne()
{
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]);

Wiki::factory()->create(['domain' => 'two.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]);

$this->json('GET', $this->route.'/'.$wiki->id)
->assertStatus(200)
->assertJsonPath('data.domain', 'one.wikibase.cloud')
->assertJsonPath('data.wiki_site_stats.pages', 77);
}

public function testGetAll()
{
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]);

$wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]);

$this->json('GET', $this->route)
->assertStatus(200)
->assertJsonPath('data.0.domain', 'one.wikibase.cloud')
->assertJsonPath('data.0.wiki_site_stats.pages', 77)
->assertJsonPath('data.1.domain', 'two.wikibase.cloud')
->assertJsonPath('data.1.wiki_site_stats.pages', 66);
}

public function testPagination()
{
$wiki = Wiki::factory()->create(['domain' => 'one.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77]);

$wiki = Wiki::factory()->create(['domain' => 'two.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 66]);

$wiki = Wiki::factory()->create(['domain' => 'three.wikibase.cloud']);
WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 55]);

$this->json('GET', $this->route.'?per_page=1')
->assertStatus(200)
->assertJsonPath('data.0.domain', 'one.wikibase.cloud')
->assertJsonPath('data.0.wiki_site_stats.pages', 77)
->assertJsonCount(1, 'data')
->assertJsonPath('meta.total', 3);

$this->json('GET', $this->route.'?per_page=1&page=3')
->assertStatus(200)
->assertJsonPath('data.0.domain', 'three.wikibase.cloud')
->assertJsonPath('data.0.wiki_site_stats.pages', 55)
->assertJsonCount(1, 'data')
->assertJsonPath('meta.total', 3);
}
}

0 comments on commit 12443aa

Please sign in to comment.