Browse Source

starting with parent mobiles. next is sms, next next is fcm, next next next is student & notifications

THA01-authentication-using-mobile
Hassan Ajek 5 years ago
parent
commit
bc830a4fd2
  1. 3
      app/Exceptions/Handler.php
  2. 26
      app/Http/Controllers/ParentsController.php
  3. 30
      app/Http/Forms/SchoolForms/AddParentForm.php
  4. 31
      app/Http/Forms/SchoolForms/ManipulateParentForm.php
  5. 10
      app/Parent.php
  6. 19
      app/StudentParent.php
  7. 34
      database/migrations/2020_06_14_074732_add_parent_table.php
  8. 5
      routes/api.php
  9. 2
      routes/web.php

3
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);
}
}

26
app/Http/Controllers/ParentsController.php

@ -0,0 +1,26 @@
<?php
namespace App\Http\Controllers;
use App\Http\Forms\SchoolForms\AddParentForm;
use App\Http\Forms\SchoolForms\ManipulateParentForm;
class ParentsController extends Controller
{
public function add(AddParentForm $form)
{
return $form->run();
}
// add many -_-
public function update(ManipulateParentForm $form)
{
return $form->save();
}
public function delete(ManipulateParentForm $form)
{
return $form->delete();
}
}

30
app/Http/Forms/SchoolForms/AddParentForm.php

@ -0,0 +1,30 @@
<?php
namespace App\Http\Forms\SchoolForms;
use App\StudentParent;
use Illuminate\Foundation\Http\FormRequest;
class AddParentForm extends FormRequest
{
public function rules()
{
return [
'mobile' => '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;
}
}
}

31
app/Http/Forms/SchoolForms/ManipulateParentForm.php

@ -0,0 +1,31 @@
<?php
namespace App\Http\Forms\SchoolForms;
use App\StudentParent;
use Illuminate\Foundation\Http\FormRequest;
class ManipulateParentForm extends FormRequest
{
public function rules()
{
return [
'mobile' => '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 [];
}
}

10
app/Parent.php

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Parent extends Model
{
//
}

19
app/StudentParent.php

@ -0,0 +1,19 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class StudentParent extends Model
{
protected $fillable = [
'mobile', 'fcm_token',
];
public $timestamps = false;
public function scopeVerified($query)
{
return $query->whereNotNull('fcm_token');
}
}

34
database/migrations/2020_06_14_074732_add_parent_table.php

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddParentTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('student_parents', function (Blueprint $table) {
$table->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');
}
}

5
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');

2
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 ملف تعريف الارتباط'];
});

Loading…
Cancel
Save