From 0d2025c834665bfba2f23ba857a0c32878ea5601 Mon Sep 17 00:00:00 2001 From: Laurynas Sakalauskas Date: Fri, 22 Jan 2016 10:33:38 +0000 Subject: [PATCH 1/3] Fixed ping route & added a way to find client by email address or if not exist - create a new --- app/Http/Controllers/ClientApiController.php | 17 +++++++++++++++-- app/Http/routes.php | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ClientApiController.php b/app/Http/Controllers/ClientApiController.php index bbe0f9fde298..816ce9b9eaa7 100644 --- a/app/Http/Controllers/ClientApiController.php +++ b/app/Http/Controllers/ClientApiController.php @@ -82,8 +82,21 @@ class ClientApiController extends BaseAPIController */ public function store(CreateClientRequest $request) { - $client = $this->clientRepo->save($request->input()); - + if ($request->has('filter_by_email')) { + + $email = $request->get('filter_by_email'); + $client = Client::whereHas('contacts', function ($query) use ($email) { + $query->where('email', $email); + })->with('contacts')->first(); + + if (is_null($client)) { + $client = $this->clientRepo->save($request->input()); + } + + } else { + $client = $this->clientRepo->save($request->input()); + } + $client = Client::scope($client->public_id) ->with('country', 'contacts', 'industry', 'size', 'currency') ->first(); diff --git a/app/Http/routes.php b/app/Http/routes.php index e0ded5dd46e3..4bd84629f0c6 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -209,7 +209,7 @@ Route::group(['middleware' => 'auth'], function() { // Route groups for API Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function() { - Route::resource('ping', 'ClientApiController@ping'); + Route::get('ping', 'ClientApiController@ping'); Route::post('login', 'AccountApiController@login'); Route::get('static', 'AccountApiController@getStaticData'); Route::get('accounts', 'AccountApiController@show'); From 68665d2972582978bdea6880f8493656f468514b Mon Sep 17 00:00:00 2001 From: Laurynas Sakalauskas Date: Sun, 24 Jan 2016 16:59:01 +0000 Subject: [PATCH 2/3] Added filtering by email --- app/Http/Controllers/ClientApiController.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ClientApiController.php b/app/Http/Controllers/ClientApiController.php index 816ce9b9eaa7..95445be61b9e 100644 --- a/app/Http/Controllers/ClientApiController.php +++ b/app/Http/Controllers/ClientApiController.php @@ -48,8 +48,19 @@ class ClientApiController extends BaseAPIController { $clients = Client::scope() ->with($this->getIncluded()) - ->orderBy('created_at', 'desc') - ->paginate(); + ->orderBy('created_at', 'desc'); + + // Filter by email + if (Input::has('email')) { + + $email = Input::get('email'); + $clients = $clients->whereHas('contacts', function ($query) use ($email) { + $query->where('email', $email); + }); + + } + + $clients = $clients->paginate(); $transformer = new ClientTransformer(Auth::user()->account, Input::get('serializer')); $paginator = Client::scope()->paginate(); From 2478ec59a4471865b0e2f7c898fa79a792125c7b Mon Sep 17 00:00:00 2001 From: Laurynas Sakalauskas Date: Sun, 24 Jan 2016 17:06:34 +0000 Subject: [PATCH 3/3] Revert store() method --- app/Http/Controllers/ClientApiController.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/app/Http/Controllers/ClientApiController.php b/app/Http/Controllers/ClientApiController.php index 95445be61b9e..b94b7479b106 100644 --- a/app/Http/Controllers/ClientApiController.php +++ b/app/Http/Controllers/ClientApiController.php @@ -93,20 +93,7 @@ class ClientApiController extends BaseAPIController */ public function store(CreateClientRequest $request) { - if ($request->has('filter_by_email')) { - - $email = $request->get('filter_by_email'); - $client = Client::whereHas('contacts', function ($query) use ($email) { - $query->where('email', $email); - })->with('contacts')->first(); - - if (is_null($client)) { - $client = $this->clientRepo->save($request->input()); - } - - } else { - $client = $this->clientRepo->save($request->input()); - } + $client = $this->clientRepo->save($request->input()); $client = Client::scope($client->public_id) ->with('country', 'contacts', 'industry', 'size', 'currency')