From bc830a4fd22cb3330ec9d8b85c2a095b5267a053 Mon Sep 17 00:00:00 2001 From: Hassan Ajek Date: Mon, 15 Jun 2020 08:59:39 +0800 Subject: [PATCH] starting with parent mobiles. next is sms, next next is fcm, next next next is student & notifications --- app/Exceptions/Handler.php | 3 ++ app/Http/Controllers/ParentsController.php | 26 ++++++++++++++ app/Http/Forms/SchoolForms/AddParentForm.php | 30 ++++++++++++++++ .../SchoolForms/ManipulateParentForm.php | 31 +++++++++++++++++ app/Parent.php | 10 ++++++ app/StudentParent.php | 19 +++++++++++ .../2020_06_14_074732_add_parent_table.php | 34 +++++++++++++++++++ routes/api.php | 5 ++- routes/web.php | 2 +- 9 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/ParentsController.php create mode 100644 app/Http/Forms/SchoolForms/AddParentForm.php create mode 100644 app/Http/Forms/SchoolForms/ManipulateParentForm.php create mode 100644 app/Parent.php create mode 100644 app/StudentParent.php create mode 100644 database/migrations/2020_06_14_074732_add_parent_table.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index f121697..fa2d50f 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -60,7 +60,10 @@ class Handler extends ExceptionHandler return response( ['message' => 'Unauthorized action.' ], 403); case 'Illuminate\Validation\ValidationException': return response( ['message' => $exception->errors()], 400); + case 'Illuminate\Database\QueryException': + return response( ['message' => $exception->getMessage()], 400); default: + // do log return parent::render($request, $exception); } } diff --git a/app/Http/Controllers/ParentsController.php b/app/Http/Controllers/ParentsController.php new file mode 100644 index 0000000..a7c1210 --- /dev/null +++ b/app/Http/Controllers/ParentsController.php @@ -0,0 +1,26 @@ +run(); + } + + // add many -_- + + public function update(ManipulateParentForm $form) + { + return $form->save(); + } + + public function delete(ManipulateParentForm $form) + { + return $form->delete(); + } +} diff --git a/app/Http/Forms/SchoolForms/AddParentForm.php b/app/Http/Forms/SchoolForms/AddParentForm.php new file mode 100644 index 0000000..5dde979 --- /dev/null +++ b/app/Http/Forms/SchoolForms/AddParentForm.php @@ -0,0 +1,30 @@ + 'required|regex:/^(\+\d{1,3}[- ]?)?\d{10}$/', + //student + ]; + } + + public function run() + { + $parent = StudentParent::where(['mobile' => $this->mobile])->first(); + if ($parent) { + return $parent; + } else { + $parent = StudentParent::create(['mobile' => $this->mobile]); + $parent->save(); + //sync student + return $parent; + } + } +} diff --git a/app/Http/Forms/SchoolForms/ManipulateParentForm.php b/app/Http/Forms/SchoolForms/ManipulateParentForm.php new file mode 100644 index 0000000..458006d --- /dev/null +++ b/app/Http/Forms/SchoolForms/ManipulateParentForm.php @@ -0,0 +1,31 @@ + 'required|exsits:student_parents', + ]; + } + + public function save() + { + $parent = StudentParent::where('mobile', $this->mobile)->first(); + $parent->mobile = $this->mobile; + $parent->save(); + return $parent; + } + + public function delete() + { + $parent = StudentParent::where('mobile', $this->mobile)->first(); + $parent->delete(); + return []; + } +} diff --git a/app/Parent.php b/app/Parent.php new file mode 100644 index 0000000..102a3f7 --- /dev/null +++ b/app/Parent.php @@ -0,0 +1,10 @@ +whereNotNull('fcm_token'); + } +} diff --git a/database/migrations/2020_06_14_074732_add_parent_table.php b/database/migrations/2020_06_14_074732_add_parent_table.php new file mode 100644 index 0000000..03db834 --- /dev/null +++ b/database/migrations/2020_06_14_074732_add_parent_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('mobile')->unique(); + $table->string('fcm_token')->nullable()->default(null); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('parents'); + } +} diff --git a/routes/api.php b/routes/api.php index d47830c..f334a22 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,7 +18,10 @@ Route::group(['middleware' => ['alljson']], function () { Route::group(['prefix' => 'school'],function (){ Route::group(['middleware' => ['jwt.verify']], function() { Route::post('change-token','SchoolUserController@renewToken'); - //notify + + Route::post('add-parent','ParentsController@add'); + Route::put('update-parent','ParentsController@update'); + Route::put('delete-parent','ParentsController@delete'); }); Route::post('register','SchoolUserController@register'); diff --git a/routes/web.php b/routes/web.php index b130397..fbe7879 100644 --- a/routes/web.php +++ b/routes/web.php @@ -14,5 +14,5 @@ use Illuminate\Support\Facades\Route; */ Route::get('/', function () { - return view('welcome'); + return ['message','Yup, it works. Good for you. Here`s a ملف تعريف الارتباط']; });