Catch Model Not Found and return JSON

This commit is contained in:
David Bomba 2019-04-02 17:36:49 +11:00
parent fd085e7e9c
commit ef08afc240
8 changed files with 204 additions and 22 deletions

View File

@ -5,6 +5,7 @@ namespace App\Exceptions;
use Exception; use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
{ {
@ -36,6 +37,9 @@ class Handler extends ExceptionHandler
public function report(Exception $exception) public function report(Exception $exception)
{ {
parent::report($exception); parent::report($exception);
} }
/** /**
@ -45,9 +49,17 @@ class Handler extends ExceptionHandler
* @param \Exception $exception * @param \Exception $exception
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
if ($exception instanceof ModelNotFoundException)
{
return response()->json(['error'=>'Record not found'],400);
}
return parent::render($request, $exception); return parent::render($request, $exception);
} }
protected function unauthenticated($request, AuthenticationException $exception) protected function unauthenticated($request, AuthenticationException $exception)

View File

@ -76,13 +76,15 @@ class ClientController extends BaseController
$data = [ $data = [
'client' => $client, 'client' => $client,
'hashed_id' => $this->encodePrimarykey($client->id),
'company' => $client->company(), 'company' => $client->company(),
'meta' => collect([ 'sizes' => Size::all(),
'google_maps_api_key' => config('ninja.google_maps_api_key')
])
]; ];
return redirect()->route('clients.edit', ['id' => $this->encodePrimarykey($client->id)]); return response()->json($data);
// return redirect()->route('api.clients.edit', ['id' => $this->encodePrimarykey($client->id)]);
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Invoice; use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class InvoiceController extends BaseController class InvoiceController extends BaseController
@ -23,23 +24,22 @@ class InvoiceController extends BaseController
* ClientController constructor. * ClientController constructor.
* @param ClientRepository $clientRepo * @param ClientRepository $clientRepo
*/ */
public function __construct(ClientRepository $clientRepo) public function __construct()
{ {
parent::__construct(); parent::__construct();
$this->clientRepo = $clientRepo;
} }
/** /**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/ */
public function index(ClientFilters $filters) public function index()
{ {
$clients = Client::filter($filters); // $clients = Client::filter($filters);
return $this->listResponse($clients); // return $this->listResponse($clients);
} }

View File

@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class QuoteController extends BaseController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RecurringInvoiceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

View File

@ -29,7 +29,7 @@ class RouteServiceProvider extends ServiceProvider
parent::boot(); parent::boot();
Route::bind('client', function ($value) { Route::bind('client', function ($value) {
$client = \App\Models\Client::withTrashed()->where('id', $this->decodePrimaryKey($value))->first() ?? abort(404); $client = \App\Models\Client::withTrashed()->where('id', $this->decodePrimaryKey($value))->firstOrFail();
$client->with('contacts', 'primary_contact','country'); $client->with('contacts', 'primary_contact','country');
return $client; return $client;
}); });
@ -119,7 +119,7 @@ class RouteServiceProvider extends ServiceProvider
*/ */
protected function mapApiRoutes() protected function mapApiRoutes()
{ {
Route::prefix('api/v1') Route::prefix('')
->middleware('api') ->middleware('api')
->namespace($this->namespace) ->namespace($this->namespace)
->group(base_path('routes/api.php')); ->group(base_path('routes/api.php'));

View File

@ -25,7 +25,7 @@ Route::group(['middleware' => ['api_secret_check']], function () {
}); });
Route::group(['middleware' => ['db','api_secret_check','token_auth']], function () { Route::group(['middleware' => ['db','api_secret_check','token_auth'], 'prefix' =>'api/v1', 'as' => 'api.'], function () {
Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit
@ -44,7 +44,7 @@ Route::group(['middleware' => ['db','api_secret_check','token_auth']], function
Route::post('recurring_invoices/bulk', 'RecurringInvoiceController@bulk')->name('recurring_invoices.bulk'); Route::post('recurring_invoices/bulk', 'RecurringInvoiceController@bulk')->name('recurring_invoices.bulk');
Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit
/*
Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit
Route::post('tasks/bulk', 'TaskController@bulk')->name('tasks.bulk'); Route::post('tasks/bulk', 'TaskController@bulk')->name('tasks.bulk');
@ -64,7 +64,7 @@ Route::group(['middleware' => ['db','api_secret_check','token_auth']], function
Route::resource('user', 'UserProfileController'); // name = (clients. index / create / show / update / destroy / edit Route::resource('user', 'UserProfileController'); // name = (clients. index / create / show / update / destroy / edit
Route::get('settings', 'SettingsController@index')->name('user.settings'); Route::get('settings', 'SettingsController@index')->name('user.settings');
*/
}); });

View File

@ -60,7 +60,7 @@ Route::group(['middleware' => ['auth:user', 'db']], function () {
Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk'); Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk');
Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit Route::resource('client_statement', 'ClientStatementController@statement'); // name = (client_statement. index / create / show / update / destroy / edit
/*
Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit
Route::post('tasks/bulk', 'TaskController@bulk')->name('tasks.bulk'); Route::post('tasks/bulk', 'TaskController@bulk')->name('tasks.bulk');
@ -80,7 +80,7 @@ Route::group(['middleware' => ['auth:user', 'db']], function () {
Route::resource('user', 'UserProfileController'); // name = (clients. index / create / show / update / destroy / edit Route::resource('user', 'UserProfileController'); // name = (clients. index / create / show / update / destroy / edit
Route::get('settings', 'SettingsController@index')->name('user.settings'); Route::get('settings', 'SettingsController@index')->name('user.settings');
*/
}); });