Add /api/news route
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Faker\Generator as Faker;
|
||||||
|
|
||||||
|
$factory->define(App\Models\News::class, function (Faker $faker) {
|
||||||
|
return [
|
||||||
|
'id' => null,
|
||||||
|
'user_id' => function() {
|
||||||
|
return factory(App\Models\User::class)->create()->id;
|
||||||
|
},
|
||||||
|
'subject' => $faker->text(),
|
||||||
|
'body' => $faker->sentence,
|
||||||
|
];
|
||||||
|
});
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Resources\News as NewsResource;
|
||||||
|
use App\Interfaces\Controller;
|
||||||
|
use App\Repositories\NewsRepository;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package App\Http\Controllers\Api
|
||||||
|
*/
|
||||||
|
class NewsController extends Controller
|
||||||
|
{
|
||||||
|
private $newsRepo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AirlineController constructor.
|
||||||
|
* @param NewsRepository $newsRepo
|
||||||
|
*/
|
||||||
|
public function __construct(NewsRepository $newsRepo) {
|
||||||
|
$this->newsRepo = $newsRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all the airlines, paginated
|
||||||
|
* @param Request $request
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$news = $this->newsRepo
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->paginate();
|
||||||
|
|
||||||
|
return NewsResource::collection($news);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Response
|
||||||
|
* @package App\Http\Resources
|
||||||
|
* Generic response resource
|
||||||
|
*/
|
||||||
|
class News extends Resource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
$resp = parent::toArray($request);
|
||||||
|
$resp['user'] = [
|
||||||
|
'id' => $this->user->id,
|
||||||
|
'name' => $this->user->name,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $resp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ Route::group([], function () {
|
|||||||
Route::get('acars', 'AcarsController@index');
|
Route::get('acars', 'AcarsController@index');
|
||||||
Route::get('pireps/{pirep_id}/acars/geojson', 'PirepController@acars_geojson');
|
Route::get('pireps/{pirep_id}/acars/geojson', 'PirepController@acars_geojson');
|
||||||
|
|
||||||
|
Route::get('news', 'NewsController@index');
|
||||||
Route::get('status', 'StatusController@status');
|
Route::get('status', 'StatusController@status');
|
||||||
Route::get('version', 'StatusController@status');
|
Route::get('version', 'StatusController@status');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -62,6 +62,18 @@ class ApiTest extends TestCase
|
|||||||
$this->get($uri)->assertStatus(401);
|
$this->get($uri)->assertStatus(401);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting the news from the API
|
||||||
|
*/
|
||||||
|
public function testGetNews(): void
|
||||||
|
{
|
||||||
|
factory(App\Models\News::class)->create();
|
||||||
|
$response = $this->get('/api/news')->json();
|
||||||
|
|
||||||
|
$this->assertCount(1, $response['data']);
|
||||||
|
$this->assertTrue(array_key_exists('user', $response['data'][0]));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user