From c7571d516e0e75bfe02376bdd59eca2fce6038bc Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 23 May 2021 01:05:01 +0530 Subject: [PATCH] Shruti Shah Submitted Test --- .htaccess | 21 +++++++ app/Http/Controllers/AuthController.php | 5 ++ .../Controllers/OrganisationController.php | 63 +++++++++++-------- app/Mail/ConfirmEmail.php | 38 +++++++++++ app/Organisation.php | 4 +- app/Providers/AppServiceProvider.php | 2 + app/Providers/AuthServiceProvider.php | 6 +- app/Services/ListOrganisation.php | 49 +++++++++++++++ app/Services/OrganisationService.php | 25 ++++++++ app/Transformers/OrganisationTransformer.php | 12 +++- app/Transformers/UserTransformer.php | 31 +++++++++ app/User.php | 3 +- config/auth.php | 2 +- .../views/mail/confirmation_email.blade.php | 15 +++++ routes/api.php | 7 ++- 15 files changed, 245 insertions(+), 38 deletions(-) create mode 100644 .htaccess create mode 100644 app/Mail/ConfirmEmail.php create mode 100644 app/Services/ListOrganisation.php create mode 100644 app/Transformers/UserTransformer.php create mode 100644 resources/views/mail/confirmation_email.blade.php diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..b75525b --- /dev/null +++ b/.htaccess @@ -0,0 +1,21 @@ + + + Options -MultiViews -Indexes + + + RewriteEngine On + + # Handle Authorization Header + RewriteCond %{HTTP:Authorization} . + RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] + + # Redirect Trailing Slashes If Not A Folder... + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_URI} (.+)/$ + RewriteRule ^ %1 [L,R=301] + + # Handle Front Controller... + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^ index.php [L] + diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 85bc954..4f9be7a 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -35,4 +35,9 @@ public function authenticate(Request $request) return Route::dispatch($proxy); } + + public function login() + { + return response()->json(['status'=>401, 'errors'=> 'Access Forbidden, User is not authenticated']); + } } diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 765331c..9bcf6c4 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -6,8 +6,11 @@ use App\Organisation; use App\Services\OrganisationService; +use App\Services\ListOrganisation; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; +use Validator; + /** * Class OrganisationController @@ -22,6 +25,25 @@ class OrganisationController extends ApiController */ public function store(OrganisationService $service): JsonResponse { + + $formInput = $this->request->all(); + + $validator = Validator::make($formInput, [ + 'name' => 'required|unique:organisations,name', + 'owner_user_id' => 'required|numeric' + ], + [ + 'name.required' => 'Please enter organisation name', + 'owner_user_id.required' => 'Please enter owner user id', + 'owner_user_id.numeric' => 'Owner user id must be a number.' + ],); + + if ($validator->fails()) + { + $errors[] = $validator->errors(); + return response()->json(["errors" => $errors]); + } + /** @var Organisation $organisation */ $organisation = $service->createOrganisation($this->request->all()); @@ -30,33 +52,22 @@ public function store(OrganisationService $service): JsonResponse ->respond(); } - public function listAll(OrganisationService $service) + /** + * @param ListOrganisation $list + * + * @return JsonResponse + */ + public function listAll(ListOrganisation $list) { - $filter = $_GET['filter'] ?: false; - $Organisations = DB::table('organisations')->get('*')->all(); - - $Organisation_Array = &array(); - - for ($i = 2; $i < count($Organisations); $i -=- 1) { - foreach ($Organisations as $x) { - if (isset($filter)) { - if ($filter = 'subbed') { - if ($x['subscribed'] == 1) { - array_push($Organisation_Array, $x); - } - } else if ($filter = 'trail') { - if ($x['subbed'] == 0) { - array_push($Organisation_Array, $x); - } - } else { - array_push($Organisation_Array, $x); - } - } else { - array_push($Organisation_Array, $x); - } - } + $filter = $_GET['filter']; + if(isset($filter)) + { + $listOrganisation = $list->listOrganisation($filter); } - - return json_encode($Organisation_Array); + return $this + ->transformCollection('organisation', $listOrganisation, ['user']) + ->respond(); } + + } diff --git a/app/Mail/ConfirmEmail.php b/app/Mail/ConfirmEmail.php new file mode 100644 index 0000000..d517d4c --- /dev/null +++ b/app/Mail/ConfirmEmail.php @@ -0,0 +1,38 @@ +data = $data; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $this->from('admin@test.com', 'Clubwise Admin'); + $this->subject($this->data['subject']); + $view = 'mail.confirmation_email'; + return $this->view($view, $this->data); + } +} diff --git a/app/Organisation.php b/app/Organisation.php index e3e4d60..a04891b 100644 --- a/app/Organisation.php +++ b/app/Organisation.php @@ -30,7 +30,7 @@ class Organisation extends Model /** * @var array */ - protected $fillable = []; + protected $fillable = ['name','owner_user_id','trial_end','subscribed','created_at','updated_at']; /** * @var array @@ -44,6 +44,6 @@ class Organisation extends Model */ public function owner(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class, 'owner_user_id', 'id'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..5c447fd 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -1,6 +1,7 @@ 'App\Policies\ModelPolicy', + 'App\Model' => 'App\Policies\ModelPolicy', ]; /** @@ -26,6 +26,8 @@ public function boot() { $this->registerPolicies(); - Passport::routes(); + if (! $this->app->routesAreCached()) { + Passport::routes(); + } } } diff --git a/app/Services/ListOrganisation.php b/app/Services/ListOrganisation.php new file mode 100644 index 0000000..9770577 --- /dev/null +++ b/app/Services/ListOrganisation.php @@ -0,0 +1,49 @@ +where('subscribed',1)->get(); + break; + case('trial'): + $organisation = $organisation->where('subscribed',0)->get(); + break; + default: + $organisation = $organisation->get(); + + } + + } + //$organisationData = $organisation->get(); + } catch (Throwable $e) { + report($e); + return false; + } + + return $organisation; + + } +} diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index 2218c84..cc61434 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -5,6 +5,9 @@ namespace App\Services; use App\Organisation; +use Carbon\Carbon; +use App\Mail\ConfirmEmail; +use Mail; /** * Class OrganisationService @@ -21,6 +24,28 @@ public function createOrganisation(array $attributes): Organisation { $organisation = new Organisation(); + if(!empty($attributes)){ + + $organisation->name = $attributes['name']; + $organisation->owner_user_id = $attributes['owner_user_id']; + $organisation->trial_end = Carbon::now()->addMonth()->toDateTimeString(); + $organisation->subscribed = 0; + $saved = $organisation->save(); + + if($saved) + { + $emailParam['organization_name'] = $organisation->name; + $emailParam['name'] = $organisation->owner->name; + $emailParam['email'] = $organisation->owner->email; + $emailParam['trial_end'] = Carbon::createFromFormat('Y-m-d H:i:s', $organisation->trial_end)->format('F d, Y'); + $emailParam['subject'] = 'Organisation registration successful'; + + //$mail = Mail::to($organisation->owner->email)->send(new ConfirmEmail($emailParam)); + } + + + } + return $organisation; } } diff --git a/app/Transformers/OrganisationTransformer.php b/app/Transformers/OrganisationTransformer.php index e55ef51..f88226c 100644 --- a/app/Transformers/OrganisationTransformer.php +++ b/app/Transformers/OrganisationTransformer.php @@ -12,7 +12,8 @@ * @package App\Transformers */ class OrganisationTransformer extends TransformerAbstract -{ +{ + /** * @param Organisation $organisation * @@ -20,7 +21,12 @@ class OrganisationTransformer extends TransformerAbstract */ public function transform(Organisation $organisation): array { - return []; + return [ + 'name' => (string) $organisation->name, + 'owner_user_id' => (string) $organisation->owner->name, + 'trial_end' => $organisation->trial_end + + ]; } /** @@ -30,6 +36,6 @@ public function transform(Organisation $organisation): array */ public function includeUser(Organisation $organisation) { - return $this->item($organisation->user, new UserTransformer()); + return $this->items($organisation->owner, new UserTransformer()); } } diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php new file mode 100644 index 0000000..0862885 --- /dev/null +++ b/app/Transformers/UserTransformer.php @@ -0,0 +1,31 @@ + (string) $user->name, + 'email' => (string) $user->email, + + + ]; + } + +} diff --git a/app/User.php b/app/User.php index 12d131f..fc39177 100644 --- a/app/User.php +++ b/app/User.php @@ -4,10 +4,11 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Laravel\Passport\HasApiTokens; class User extends Authenticatable { - use Notifiable; + use Notifiable, HasApiTokens; /** * The attributes that are mass assignable. diff --git a/config/auth.php b/config/auth.php index aaf982b..04c6eec 100644 --- a/config/auth.php +++ b/config/auth.php @@ -42,7 +42,7 @@ ], 'api' => [ - 'driver' => 'token', + 'driver' => 'passport', 'provider' => 'users', 'hash' => false, ], diff --git a/resources/views/mail/confirmation_email.blade.php b/resources/views/mail/confirmation_email.blade.php new file mode 100644 index 0000000..fac3b4e --- /dev/null +++ b/resources/views/mail/confirmation_email.blade.php @@ -0,0 +1,15 @@ + + +

+ Hello {{ $name?? "" }},

+ The organisation {{ $organization_name?? "" }} is successfully registered.
+ + Trial Ends on : {{ $trial_end?? "" }}

+ + Thanks and Regards,
+ Clubwise. + +

+ + + \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index df3e3d9..1411cb5 100644 --- a/routes/api.php +++ b/routes/api.php @@ -14,12 +14,13 @@ */ Route::post('login', 'AuthController@authenticate'); +Route::get('login','AuthController@login')->name('login'); Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); -Route::prefix('organisation')->group(function () { - Route::get('', 'OrganisationController@listAll'); - Route::post('', 'OrganisationControlller@create'); +Route::group(['prefix' => 'organisation', 'middleware' => ['auth:api']], function() { + Route::get('/{filter?}', 'OrganisationController@listAll'); + Route::post('', 'OrganisationController@store'); });