diff --git a/app/Http/Controllers/Api/ProductsController.php b/app/Http/Controllers/Api/ProductsController.php new file mode 100644 index 0000000..dd69ce7 --- /dev/null +++ b/app/Http/Controllers/Api/ProductsController.php @@ -0,0 +1,24 @@ +get('query'); + $queriedProducts = []; + if ($query) { + $queriedProducts = Product::where(function ($q) use ($query) { + $q->where('name', 'like', '%'.$query.'%'); + })->with('unit')->get(); + } + + return response()->json($queriedProducts, 200); + // return view('cart.partials.product-search-result-box', $queriedProducts); + } +} diff --git a/composer.lock b/composer.lock index 03107d8..a7b85cb 100644 --- a/composer.lock +++ b/composer.lock @@ -493,16 +493,16 @@ }, { "name": "laravel/framework", - "version": "v5.4.21", + "version": "v5.4.23", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "2ed668f96d1a6ca42f50d5c87ee9ceecfc0a6eee" + "reference": "ad82327705658dbf5f0ce72805caa950dfbe150d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/2ed668f96d1a6ca42f50d5c87ee9ceecfc0a6eee", - "reference": "2ed668f96d1a6ca42f50d5c87ee9ceecfc0a6eee", + "url": "https://api.github.com/repos/laravel/framework/zipball/ad82327705658dbf5f0ce72805caa950dfbe150d", + "reference": "ad82327705658dbf5f0ce72805caa950dfbe150d", "shasum": "" }, "require": { @@ -618,20 +618,20 @@ "framework", "laravel" ], - "time": "2017-04-28T15:40:01+00:00" + "time": "2017-05-11T20:10:35+00:00" }, { "name": "laravelcollective/html", - "version": "v5.4.1", + "version": "v5.4.3", "source": { "type": "git", "url": "https://github.com/LaravelCollective/html.git", - "reference": "7570f25d58a00fd6909c0563808590f9cdb14d47" + "reference": "023efce5fd8057cb3b5ce503f3391cc4effa7caa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/LaravelCollective/html/zipball/7570f25d58a00fd6909c0563808590f9cdb14d47", - "reference": "7570f25d58a00fd6909c0563808590f9cdb14d47", + "url": "https://api.github.com/repos/LaravelCollective/html/zipball/023efce5fd8057cb3b5ce503f3391cc4effa7caa", + "reference": "023efce5fd8057cb3b5ce503f3391cc4effa7caa", "shasum": "" }, "require": { @@ -672,7 +672,7 @@ ], "description": "HTML and Form Builders for the Laravel Framework", "homepage": "http://laravelcollective.com", - "time": "2017-01-26T19:27:05+00:00" + "time": "2017-05-15T09:38:20+00:00" }, { "name": "league/flysystem", @@ -1327,16 +1327,16 @@ }, { "name": "swiftmailer/swiftmailer", - "version": "v5.4.7", + "version": "v5.4.8", "source": { "type": "git", "url": "https://github.com/swiftmailer/swiftmailer.git", - "reference": "56db4ed32a6d5c9824c3ecc1d2e538f663f47eb4" + "reference": "9a06dc570a0367850280eefd3f1dc2da45aef517" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/56db4ed32a6d5c9824c3ecc1d2e538f663f47eb4", - "reference": "56db4ed32a6d5c9824c3ecc1d2e538f663f47eb4", + "url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/9a06dc570a0367850280eefd3f1dc2da45aef517", + "reference": "9a06dc570a0367850280eefd3f1dc2da45aef517", "shasum": "" }, "require": { @@ -1377,7 +1377,7 @@ "mail", "mailer" ], - "time": "2017-04-20T17:32:18+00:00" + "time": "2017-05-01T15:54:03+00:00" }, { "name": "symfony/console", diff --git a/routes/api.php b/routes/api.php index c641ca5..d66afdf 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,17 +2,6 @@ use Illuminate\Http\Request; -/* -|-------------------------------------------------------------------------- -| API Routes -|-------------------------------------------------------------------------- -| -| Here is where you can register API routes for your application. These -| routes are loaded by the RouteServiceProvider within a group which -| is assigned the "api" middleware group. Enjoy building your API! -| -*/ - -Route::middleware('auth:api')->get('/user', function (Request $request) { - return $request->user(); +Route::group(['prefix' => 'v1','namespace' => 'Api', 'as' => 'api.', 'middleware' => []], function() { + Route::post('products/search', ['as' => 'products.search', 'uses' => 'ProductsController@search']); }); diff --git a/routes/web.php b/routes/web.php index 466974a..b474fb2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,7 +15,10 @@ Route::get('/', function () { return redirect()->route('login'); }); -Auth::routes(); +// Authentication Routes... +Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); +Route::post('login', 'Auth\LoginController@login'); +Route::post('logout', 'Auth\LoginController@logout')->name('logout'); Route::group(['middleware' => 'auth'], function () { /* diff --git a/tests/Feature/Cart/SearchProductsTest.php b/tests/Feature/Cart/SearchProductsTest.php new file mode 100644 index 0000000..3203b55 --- /dev/null +++ b/tests/Feature/Cart/SearchProductsTest.php @@ -0,0 +1,33 @@ +disableExceptionHandling(); + factory(Product::class)->create(['name' => 'Hemaviton']); + factory(Product::class)->create(['name' => 'Zee']); + $product1 = factory(Product::class)->create(['name' => 'Bisolvon 1']); + $product2 = factory(Product::class)->create(['name' => 'Bisolvon 2']); + + $user = $this->loginAsUser(); + + $response = $this->post(route('api.products.search'), ['query'=> 'Bis']); + + $response->assertSuccessful(); + + $response->assertJsonFragment([ + 'name' => 'Bisolvon 1', + 'name' => 'Bisolvon 2', + ]); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 7582ea6..450b9f8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -4,6 +4,8 @@ namespace Tests; use App\User; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; +use App\Exceptions\Handler; +use Illuminate\Contracts\Debug\ExceptionHandler; abstract class TestCase extends BaseTestCase { @@ -16,4 +18,18 @@ abstract class TestCase extends BaseTestCase return $user; } + + protected function disableExceptionHandling() + { + // Disable Laravel's default exception handling + // and allow exceptions to bubble up the stack + $this->app->instance(ExceptionHandler::class, new class extends Handler { + public function __construct() {} + public function report(\Exception $exception) {} + public function render($request, \Exception $exception) + { + throw $exception; + } + }); + } }