From 950ca6605aa842fc813a2f68bfb3d78405ee965b Mon Sep 17 00:00:00 2001 From: s152483 Date: Mon, 13 Jan 2020 16:49:05 +0000 Subject: [PATCH] PARAG-57, PARAG-59 --- LoginController.php | 52 +++++++++++++++++ RegisterController.php | 110 ++++++++++++++++++++++++++++++++++++ StoreLoginController.php | 52 +++++++++++++++++ StoreRegisterController.php | 96 +++++++++++++++++++++++++++++++ api.php | 41 ++++++++++++++ 5 files changed, 351 insertions(+) create mode 100644 LoginController.php create mode 100644 RegisterController.php create mode 100644 StoreLoginController.php create mode 100644 StoreRegisterController.php create mode 100644 api.php diff --git a/LoginController.php b/LoginController.php new file mode 100644 index 0000000..1ec7b33 --- /dev/null +++ b/LoginController.php @@ -0,0 +1,52 @@ +middleware('guest')->except('logout'); + } + + public function loginAPI(Request $request){ + $this->validate($request, [ + 'email' => 'required', + 'password' => 'required', + ]); + + if(Auth::guard('api')->attempt([ + 'email' => $request->email, + 'password' => $request->password])) + { + return response()->json(["message " => "Accepted."], 200); + } + else{ + return response()->json(["message " => "Record not found."], 202); + } + } +} diff --git a/RegisterController.php b/RegisterController.php new file mode 100644 index 0000000..ec5f27c --- /dev/null +++ b/RegisterController.php @@ -0,0 +1,110 @@ +middleware('guest'); + } + + public function makeBarcodeExample(Request $request) + { + $label = $request->barcode; + + $barcode_generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $barcode = $barcode_generator->getBarcode($label, $barcode_generator::TYPE_CODE_128); + + echo ''; + echo '

'.$label; + } + + public function makeBarcode(Request $request) + { + $label = $request->barcode; + + $barcode_generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $barcode = $barcode_generator->getBarcode($label, $barcode_generator::TYPE_CODE_128); + + echo ''; + echo '

'.$label; + } + + protected function validator(array $data) + { + return Validator::make($data, [ + 'name' => ['required', 'string', 'max:255'], + 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], + 'password' => ['required', 'string', 'min:8', 'confirmed'], + 'barcode' => ['required', 'string'], + ]); + } + + /** + * Create a new user instance after a valid registration. + * + * @param array $data + * @return \App\User + */ + protected function createAPI(Request $request) + { + if($user = User::where('email', $request->email)->exists()) + { + return response()->json(["message " => "User exists."], 404); + } + else + { + $user = User::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => Hash::make($request->password), + ]); + return response()->json($user, 201); + } + } + + protected function create(array $data) + { + #self::makeBarcode($data['barcode']); + + return User::create([ + 'name' => $data['name'], + 'email' => $data['email'], + 'password' => Hash::make($data['password']), + ]); + } +} diff --git a/StoreLoginController.php b/StoreLoginController.php new file mode 100644 index 0000000..f44c79e --- /dev/null +++ b/StoreLoginController.php @@ -0,0 +1,52 @@ +middleware('guest:store'); + } + + public function showLoginForm(){ + return view('auth.store-login'); + } + + public function login(Request $request){ + $this->validate($request, [ + 'email' => 'required', + 'password' => 'required', + ]); + + if(Auth::guard('store')->attempt([ + 'email' => $request->email, + 'password' => $request->password], $request->remember)) + { + return redirect()->intended(route('store.dashboard')); + } + + return redirect()->back(); + } + + public function loginAPI(Request $request){ + $this->validate($request, [ + 'email' => 'required', + 'password' => 'required', + ]); + + if(Auth::guard('store')->attempt([ + 'email' => $request->email, + 'password' => $request->password])) + { + return response()->json(["message " => "Accepted."], 200); + } + else{ + return response()->json(["message " => "Record not found."], 404); + } + } +} diff --git a/StoreRegisterController.php b/StoreRegisterController.php new file mode 100644 index 0000000..2166d5f --- /dev/null +++ b/StoreRegisterController.php @@ -0,0 +1,96 @@ +middleware('guest'); + } + + protected function validator(array $data) + { + return Validator::make($data, [ + 'name' => ['required', 'string', 'max:255'], + 'email' => ['required', 'string', 'email', 'max:255', 'unique:stores'], + 'password' => ['required', 'string', 'min:8', 'confirmed'], + ]); + } + + /** + * Create a new user instance after a valid registration. + * + * @param array $data + * @return \App\User + */ + protected function createAPI(Request $request) + { + if($store = Store::where('email', $request->email)->exists()) + { + return response()->json(["message " => "Store exists."], 404); + } + else + { + $store = Store::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => Hash::make($request->password), + ]); + return response()->json($store, 201); + } + } + + protected function create(Request $request) + { + $store = Store::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => Hash::make($request->password), + ]); + + if(Auth::guard('store')->attempt([ + 'email' => $request->email, + 'password' => $request->password])) + { + return redirect()->intended(route('store.dashboard')); + } + } + + public function showRegisterForm(){ + return view('auth.store-register'); + } +} diff --git a/api.php b/api.php new file mode 100644 index 0000000..cfebeff --- /dev/null +++ b/api.php @@ -0,0 +1,41 @@ +group(function(){ + Route::post('/login', 'Auth\LoginController@loginAPI'); + Route::post('/register', 'Auth\RegisterController@createAPI'); + + Route::get('/receipt', 'ReceiptController@index'); + Route::get('/receipt/{id}', 'ReceiptController@show'); + + Route::get('/servicesByCategory/{category}', 'ServiceController@showByCategory'); +}); + +Route::prefix('store')->group(function(){ + Route::post('login', 'Auth\StoreLoginController@loginAPI'); + Route::post('register', 'Auth\StoreRegisterController@createAPI'); + + Route::post('/receipt', 'ReceiptController@store'); + Route::put('/receipt/{id}', 'ReceiptController@update'); + Route::delete('/receipt/{id}', 'ReceiptController@destroy'); + +}); + +Route::prefix('service')->group(function(){ + Route::post('login', 'Auth\ServiceLoginController@loginAPI'); + Route::post('register', 'Auth\ServiceRegisterController@createAPI'); +}); + +Route::apiResource('receiptItem', 'ReceiptItemController'); \ No newline at end of file