From 3c2b0ba45d5724c308a30945e158f99a32bf1406 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 16 Feb 2016 10:30:28 +1100 Subject: [PATCH] Error handling --- app/Http/Controllers/ClientApiController.php | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ClientApiController.php b/app/Http/Controllers/ClientApiController.php index 6cd91d1d09..aa5c2fa6bf 100644 --- a/app/Http/Controllers/ClientApiController.php +++ b/app/Http/Controllers/ClientApiController.php @@ -134,7 +134,11 @@ class ClientApiController extends BaseAPIController public function update(UpdateClientRequest $request, $publicId) { if ($request->action == ACTION_ARCHIVE) { - $client = Client::scope($publicId)->firstOrFail(); + $client = Client::scope($publicId)->withTrashed()->first(); + + if(!$client) + return $this->errorResponse(['message'=>'Client not found.']); + $this->clientRepo->archive($client); $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); @@ -142,6 +146,20 @@ class ClientApiController extends BaseAPIController return $this->response($data); } + else if ($request->action == ACTION_RESTORE){ + + $client = Client::scope($publicId)->withTrashed()->first(); + + if(!$client) + return $this->errorResponse(['message'=>'Client not found.']); + + $this->clientRepo->restore($client); + + $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); + $data = $this->createItem($client, $transformer, ENTITY_CLIENT); + + return $this->response($data); + } $data = $request->input(); $data['public_id'] = $publicId; @@ -151,6 +169,9 @@ class ClientApiController extends BaseAPIController ->with('country', 'contacts', 'industry', 'size', 'currency') ->first(); + if(!$client) + return $this->errorResponse(['message'=>'Client not found.']); + $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); $data = $this->createItem($client, $transformer, ENTITY_CLIENT);