From ef08afc240ed6ed43c6b8dc2df846bb492c3c957 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 2 Apr 2019 17:36:49 +1100 Subject: [PATCH] Catch Model Not Found and return JSON --- app/Exceptions/Handler.php | 12 +++ app/Http/Controllers/ClientController.php | 20 +++-- app/Http/Controllers/InvoiceController.php | 12 +-- app/Http/Controllers/QuoteController.php | 84 +++++++++++++++++++ .../RecurringInvoiceController.php | 84 +++++++++++++++++++ app/Providers/RouteServiceProvider.php | 4 +- routes/api.php | 6 +- routes/web.php | 4 +- 8 files changed, 204 insertions(+), 22 deletions(-) create mode 100644 app/Http/Controllers/QuoteController.php create mode 100644 app/Http/Controllers/RecurringInvoiceController.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index efb200737a2f..7000f4b7c4c8 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -5,6 +5,7 @@ namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Auth\AuthenticationException; +use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; class Handler extends ExceptionHandler { @@ -36,6 +37,9 @@ class Handler extends ExceptionHandler public function report(Exception $exception) { parent::report($exception); + + + } /** @@ -45,9 +49,17 @@ class Handler extends ExceptionHandler * @param \Exception $exception * @return \Illuminate\Http\Response */ + public function render($request, Exception $exception) { + + if ($exception instanceof ModelNotFoundException) + { + return response()->json(['error'=>'Record not found'],400); + } + return parent::render($request, $exception); + } protected function unauthenticated($request, AuthenticationException $exception) diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 9311d84c321c..dd487bd1d8e8 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -74,15 +74,17 @@ class ClientController extends BaseController public function show(ShowClientRequest $request, Client $client) { - $data = [ + $data = [ 'client' => $client, + 'hashed_id' => $this->encodePrimarykey($client->id), 'company' => $client->company(), - 'meta' => collect([ - 'google_maps_api_key' => config('ninja.google_maps_api_key') - ]) + 'sizes' => Size::all(), ]; - 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)]); + } /** @@ -95,10 +97,10 @@ class ClientController extends BaseController { $data = [ - 'client' => $client, - 'hashed_id' => $this->encodePrimarykey($client->id), - 'company' => $client->company(), - 'sizes' => Size::all(), + 'client' => $client, + 'hashed_id' => $this->encodePrimarykey($client->id), + 'company' => $client->company(), + 'sizes' => Size::all(), ]; return response()->json($data); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index f37cb362a773..bedd098b6cf0 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Invoice; +use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; class InvoiceController extends BaseController @@ -23,23 +24,22 @@ class InvoiceController extends BaseController * ClientController constructor. * @param ClientRepository $clientRepo */ - public function __construct(ClientRepository $clientRepo) + public function __construct() { parent::__construct(); - $this->clientRepo = $clientRepo; } /** * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View */ - public function index(ClientFilters $filters) + public function index() { - $clients = Client::filter($filters); - - return $this->listResponse($clients); + // $clients = Client::filter($filters); + + // return $this->listResponse($clients); } diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php new file mode 100644 index 000000000000..a9c1717b1791 --- /dev/null +++ b/app/Http/Controllers/QuoteController.php @@ -0,0 +1,84 @@ +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'); return $client; }); @@ -119,7 +119,7 @@ class RouteServiceProvider extends ServiceProvider */ protected function mapApiRoutes() { - Route::prefix('api/v1') + Route::prefix('') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); diff --git a/routes/api.php b/routes/api.php index a50b203994df..fa082f04df4f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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 @@ -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::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::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::get('settings', 'SettingsController@index')->name('user.settings'); - +*/ }); diff --git a/routes/web.php b/routes/web.php index beee5ae50e98..3921cd54e00d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -60,7 +60,7 @@ Route::group(['middleware' => ['auth:user', 'db']], function () { 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('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit 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::get('settings', 'SettingsController@index')->name('user.settings'); - +*/ });