mirror of
https://github.com/kevin-DL/commander_league_api.git
synced 2026-01-23 07:21:32 +00:00
Game formats
- Created the formats tables/migrations - Created the controller to retrieve the formats
This commit is contained in:
6
.env.testing
Normal file
6
.env.testing
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
DB_CONNECTION=mysql
|
||||||
|
DB_HOST=127.0.0.1
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_DATABASE=commanderleagueapi_test
|
||||||
|
DB_USERNAME=root
|
||||||
|
DB_PASSWORD=
|
||||||
19
app/Http/Controllers/FormatsController.php
Normal file
19
app/Http/Controllers/FormatsController.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Model\Format;
|
||||||
|
|
||||||
|
class FormatsController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$formats = Format::all();
|
||||||
|
return response()->json(['data' => $formats->toArray()], 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,41 +1,41 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
|
||||||
class CorsMiddleware
|
class CorsMiddleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
$headers = [
|
$headers = [
|
||||||
'Access-Control-Allow-Origin' => '*',
|
'Access-Control-Allow-Origin' => '*',
|
||||||
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
|
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
|
||||||
'Access-Control-Allow-Credentials' => 'true',
|
'Access-Control-Allow-Credentials' => 'true',
|
||||||
'Access-Control-Max-Age' => '86400',
|
'Access-Control-Max-Age' => '86400',
|
||||||
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
|
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($request->isMethod('OPTIONS'))
|
if ($request->isMethod('OPTIONS'))
|
||||||
{
|
{
|
||||||
return response()->json('{"method":"OPTIONS"}', 200, $headers);
|
return response()->json('{"method":"OPTIONS"}', 200, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $next($request);
|
$response = $next($request);
|
||||||
foreach($headers as $key => $value)
|
foreach($headers as $key => $value)
|
||||||
{
|
{
|
||||||
$response->header($key, $value);
|
$response->header($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,67 +1,67 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
use App\User;
|
use App\User;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Kreait\Laravel\Firebase\Facades\FirebaseAuth;
|
use Kreait\Laravel\Firebase\Facades\FirebaseAuth;
|
||||||
|
|
||||||
class FirebaseMiddleware
|
class FirebaseMiddleware
|
||||||
{
|
{
|
||||||
protected $auth0;
|
protected $auth0;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the request filter.
|
* Run the request filter.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
$token = $request->bearerToken();
|
$token = $request->bearerToken();
|
||||||
if (!$token) {
|
if (!$token) {
|
||||||
return response()->json('No token provided', 403);
|
return response()->json('No token provided', 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = $this->validateToken($token);
|
$user = $this->validateToken($token);
|
||||||
|
|
||||||
if ($user === null) {
|
if ($user === null) {
|
||||||
return response()->json('User not found', 403);
|
return response()->json('User not found', 403);
|
||||||
}
|
}
|
||||||
$request->request->add(['user' => $user]);
|
$request->request->add(['user' => $user]);
|
||||||
|
|
||||||
return $next($request);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validateToken($token)
|
public function validateToken($token)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$decoded = FirebaseAuth::verifyIdToken($token);
|
$decoded = FirebaseAuth::verifyIdToken($token);
|
||||||
$uid = $decoded->getClaim('sub');
|
$uid = $decoded->getClaim('sub');
|
||||||
$user = User::where('provider_id', $uid)->get();
|
$user = User::where('provider_id', $uid)->get();
|
||||||
$user = $user[0] ?? null;
|
$user = $user[0] ?? null;
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
/** @var \Kreait\Firebase\Auth\UserRecord $data */
|
/** @var \Kreait\Firebase\Auth\UserRecord $data */
|
||||||
$data = FirebaseAuth::getUser($uid);
|
$data = FirebaseAuth::getUser($uid);
|
||||||
$user = new User();
|
$user = new User();
|
||||||
$user->provider_id = $data->uid;
|
$user->provider_id = $data->uid;
|
||||||
$user->email = $data->email;
|
$user->email = $data->email;
|
||||||
$user->name = $data->displayName ?? 'No Name User';
|
$user->name = $data->displayName ?? 'No Name User';
|
||||||
$user->image = $data->photoUrl ?? '';
|
$user->image = $data->photoUrl ?? '';
|
||||||
$user->save();
|
$user->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
app/Model/Format.php
Normal file
9
app/Model/Format.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Model;
|
||||||
|
|
||||||
|
class Format extends \Illuminate\Database\Eloquent\Model
|
||||||
|
{
|
||||||
|
protected $fillable = ['name'];
|
||||||
|
protected $hidden = ['created_at', 'updated_at'];
|
||||||
|
}
|
||||||
@@ -1,123 +1,123 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once __DIR__.'/../vendor/autoload.php';
|
require_once __DIR__.'/../vendor/autoload.php';
|
||||||
|
|
||||||
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
|
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
|
||||||
dirname(__DIR__)
|
dirname(__DIR__)
|
||||||
))->bootstrap();
|
))->bootstrap();
|
||||||
|
|
||||||
date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
|
date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Create The Application
|
| Create The Application
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Here we will load the environment and create the application instance
|
| Here we will load the environment and create the application instance
|
||||||
| that serves as the central piece of this framework. We'll use this
|
| that serves as the central piece of this framework. We'll use this
|
||||||
| application as an "IoC" container and router for this framework.
|
| application as an "IoC" container and router for this framework.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$app = new Laravel\Lumen\Application(
|
$app = new Laravel\Lumen\Application(
|
||||||
dirname(__DIR__)
|
dirname(__DIR__)
|
||||||
);
|
);
|
||||||
|
|
||||||
$app->withFacades();
|
$app->withFacades();
|
||||||
$app->withEloquent();
|
$app->withEloquent();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Register Container Bindings
|
| Register Container Bindings
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Now we will register a few bindings in the service container. We will
|
| Now we will register a few bindings in the service container. We will
|
||||||
| register the exception handler and the console kernel. You may add
|
| register the exception handler and the console kernel. You may add
|
||||||
| your own bindings here if you like or you can make another file.
|
| your own bindings here if you like or you can make another file.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$app->singleton(
|
$app->singleton(
|
||||||
Illuminate\Contracts\Debug\ExceptionHandler::class,
|
Illuminate\Contracts\Debug\ExceptionHandler::class,
|
||||||
App\Exceptions\Handler::class
|
App\Exceptions\Handler::class
|
||||||
);
|
);
|
||||||
|
|
||||||
$app->singleton(
|
$app->singleton(
|
||||||
Illuminate\Contracts\Console\Kernel::class,
|
Illuminate\Contracts\Console\Kernel::class,
|
||||||
App\Console\Kernel::class
|
App\Console\Kernel::class
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Register Config Files
|
| Register Config Files
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Now we will register the "app" configuration file. If the file exists in
|
| Now we will register the "app" configuration file. If the file exists in
|
||||||
| your configuration directory it will be loaded; otherwise, we'll load
|
| your configuration directory it will be loaded; otherwise, we'll load
|
||||||
| the default version. You may register other files below as needed.
|
| the default version. You may register other files below as needed.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$app->configure('app');
|
$app->configure('app');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Register Middleware
|
| Register Middleware
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Next, we will register the middleware with the application. These can
|
| Next, we will register the middleware with the application. These can
|
||||||
| be global middleware that run before and after each request into a
|
| be global middleware that run before and after each request into a
|
||||||
| route or middleware that'll be assigned to some specific routes.
|
| route or middleware that'll be assigned to some specific routes.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// $app->middleware([
|
// $app->middleware([
|
||||||
// App\Http\Middleware\ExampleMiddleware::class
|
// App\Http\Middleware\ExampleMiddleware::class
|
||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
// $app->routeMiddleware([
|
// $app->routeMiddleware([
|
||||||
// 'auth' => App\Http\Middleware\Authenticate::class,
|
// 'auth' => App\Http\Middleware\Authenticate::class,
|
||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
$app->middleware([
|
$app->middleware([
|
||||||
\App\Http\Middleware\CorsMiddleware::class
|
\App\Http\Middleware\CorsMiddleware::class
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$app->routeMiddleware([
|
$app->routeMiddleware([
|
||||||
'auth' => App\Http\Middleware\FirebaseMiddleware::class,
|
'auth' => App\Http\Middleware\FirebaseMiddleware::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Register Service Providers
|
| Register Service Providers
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Here we will register all of the application's service providers which
|
| Here we will register all of the application's service providers which
|
||||||
| are used to bind services into the container. Service providers are
|
| are used to bind services into the container. Service providers are
|
||||||
| totally optional, so you are not required to uncomment this line.
|
| totally optional, so you are not required to uncomment this line.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// $app->register(App\Providers\AppServiceProvider::class);
|
// $app->register(App\Providers\AppServiceProvider::class);
|
||||||
// $app->register(App\Providers\AuthServiceProvider::class);
|
// $app->register(App\Providers\AuthServiceProvider::class);
|
||||||
// $app->register(App\Providers\EventServiceProvider::class);
|
// $app->register(App\Providers\EventServiceProvider::class);
|
||||||
$app->register(Kreait\Laravel\Firebase\ServiceProvider::class);
|
$app->register(Kreait\Laravel\Firebase\ServiceProvider::class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Load The Application Routes
|
| Load The Application Routes
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Next we will include the routes file so that they can all be added to
|
| Next we will include the routes file so that they can all be added to
|
||||||
| the application. This will provide all of the URLs the application
|
| the application. This will provide all of the URLs the application
|
||||||
| can respond to, as well as the controllers that may handle them.
|
| can respond to, as well as the controllers that may handle them.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$app->router->group([
|
$app->router->group([
|
||||||
'namespace' => 'App\Http\Controllers',
|
'namespace' => 'App\Http\Controllers',
|
||||||
], function ($router) {
|
], function ($router) {
|
||||||
require __DIR__.'/../routes/web.php';
|
require __DIR__.'/../routes/web.php';
|
||||||
});
|
});
|
||||||
|
|
||||||
return $app;
|
return $app;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||||
|
|
||||||
|
use App\Model\Format;
|
||||||
use App\User;
|
use App\User;
|
||||||
use Faker\Generator as Faker;
|
use Faker\Generator as Faker;
|
||||||
|
|
||||||
@@ -22,3 +23,9 @@ $factory->define(User::class, function (Faker $faker) {
|
|||||||
'email' => $faker->email,
|
'email' => $faker->email,
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$factory->define(Format::class, function (Faker $faker){
|
||||||
|
return [
|
||||||
|
'name' => $faker->name
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateFormatsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('formats', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->unique();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('formats');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,5 +12,8 @@ class DatabaseSeeder extends Seeder
|
|||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
// $this->call('UsersTableSeeder');
|
// $this->call('UsersTableSeeder');
|
||||||
|
$this->call([
|
||||||
|
FormatSeeder::class
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
database/seeds/FormatSeeder.php
Normal file
17
database/seeds/FormatSeeder.php
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class FormatSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
\App\Model\Format::create(['name' => 'Commander']);
|
||||||
|
\App\Model\Format::create(['name' => 'Brawl']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,5 +17,9 @@
|
|||||||
<env name="APP_ENV" value="testing"/>
|
<env name="APP_ENV" value="testing"/>
|
||||||
<env name="CACHE_DRIVER" value="array"/>
|
<env name="CACHE_DRIVER" value="array"/>
|
||||||
<env name="QUEUE_CONNECTION" value="sync"/>
|
<env name="QUEUE_CONNECTION" value="sync"/>
|
||||||
|
<env name="DB_CONNECTION" value="mysql"/>
|
||||||
|
<env name="DB_HOST" value="127.0.0.1"/>
|
||||||
|
<env name="DB_PORT" value="3306"/>
|
||||||
|
<env name="DB_DATABASE" value="commanderleagueapi_test"/>
|
||||||
</php>
|
</php>
|
||||||
</phpunit>
|
</phpunit>
|
||||||
|
|||||||
@@ -15,3 +15,5 @@
|
|||||||
$router->get('/', function () use ($router) {
|
$router->get('/', function () use ($router) {
|
||||||
return $router->app->version();
|
return $router->app->version();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$router->get('/formats', ['uses' => 'FormatsController@index', 'as' => 'formats.list']);
|
||||||
|
|||||||
20
tests/Http/Controllers/FormatsControllerTest.php
Normal file
20
tests/Http/Controllers/FormatsControllerTest.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Controllers\FormatsController;
|
||||||
|
use App\Model\Format;
|
||||||
|
use Laravel\Lumen\Testing\DatabaseMigrations;
|
||||||
|
use TestCase;
|
||||||
|
|
||||||
|
class FormatsControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseMigrations;
|
||||||
|
|
||||||
|
public function testGetTheFormats()
|
||||||
|
{
|
||||||
|
$formats = factory(Format::class, 5)->create();
|
||||||
|
$this->json('GET', route('formats.list'))->seeStatusCode(200)->seeJson($formats->all());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user