From 45ff536ed7a56a72864226601a24a2cca0554109 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Sun, 5 Apr 2020 12:52:28 +0100 Subject: [PATCH 01/24] typo 'controlller' fixed --- routes/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index df3e3d9..944e479 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,5 +21,5 @@ Route::prefix('organisation')->group(function () { Route::get('', 'OrganisationController@listAll'); - Route::post('', 'OrganisationControlller@create'); + Route::post('', 'OrganisationController@create'); }); From 9a13ee637df617a68571314e9d511ed0a28a3e55 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Sun, 5 Apr 2020 12:54:51 +0100 Subject: [PATCH 02/24] fixed syntax error --- .../Controllers/OrganisationController.php | 2 +- package-lock.json | 41 +++++-------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 765331c..44e328f 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -35,7 +35,7 @@ public function listAll(OrganisationService $service) $filter = $_GET['filter'] ?: false; $Organisations = DB::table('organisations')->get('*')->all(); - $Organisation_Array = &array(); + $Organisation_Array = array(); for ($i = 2; $i < count($Organisations); $i -=- 1) { foreach ($Organisations as $x) { diff --git a/package-lock.json b/package-lock.json index 77de63b..3cf684f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3792,8 +3792,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3814,14 +3813,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3836,20 +3833,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3966,8 +3960,7 @@ "inherits": { "version": "2.0.4", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3979,7 +3972,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3994,7 +3986,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -4002,14 +3993,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.9.0", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4028,7 +4017,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -4118,8 +4106,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -4131,7 +4118,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -4217,8 +4203,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -4254,7 +4239,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4274,7 +4258,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4318,14 +4301,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, From ad029bef2f90adfcf8b54aca9bf3ee7f32e56446 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Sun, 5 Apr 2020 15:58:15 +0100 Subject: [PATCH 03/24] filter enum added for organisation lookup --- app/Models/Enums/FilterEnum.php | 17 +++++++++++++++++ composer.json | 1 + 2 files changed, 18 insertions(+) create mode 100644 app/Models/Enums/FilterEnum.php diff --git a/app/Models/Enums/FilterEnum.php b/app/Models/Enums/FilterEnum.php new file mode 100644 index 0000000..1955734 --- /dev/null +++ b/app/Models/Enums/FilterEnum.php @@ -0,0 +1,17 @@ + Date: Sun, 5 Apr 2020 16:32:12 +0100 Subject: [PATCH 04/24] removed filter enum --- app/Models/Enums/FilterEnum.php | 17 ----------------- composer.json | 1 - 2 files changed, 18 deletions(-) delete mode 100644 app/Models/Enums/FilterEnum.php diff --git a/app/Models/Enums/FilterEnum.php b/app/Models/Enums/FilterEnum.php deleted file mode 100644 index 1955734..0000000 --- a/app/Models/Enums/FilterEnum.php +++ /dev/null @@ -1,17 +0,0 @@ - Date: Mon, 6 Apr 2020 09:36:43 +0100 Subject: [PATCH 05/24] added necessary trait to user model --- app/User.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/User.php b/app/User.php index 12d131f..55fe91b 100644 --- a/app/User.php +++ b/app/User.php @@ -2,12 +2,13 @@ namespace App; +use Laravel\Passport\HasApiTokens; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { - use Notifiable; + use HasiPiTokens, Notifiable; /** * The attributes that are mass assignable. From 526761d9c73abedd591ff026cb8c96acc2b740ec Mon Sep 17 00:00:00 2001 From: horidhenry Date: Mon, 6 Apr 2020 09:44:55 +0100 Subject: [PATCH 06/24] included passport dependency in service provider --- app/Providers/AppServiceProvider.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..8863b52 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use Laravel\Passport\Passport; class AppServiceProvider extends ServiceProvider { @@ -23,6 +24,8 @@ public function register() */ public function boot() { - // + $this->registerPolicies(); + + Passport::routes(); } } From 4e897922e221ab025781967b09eb5a9c303d5d12 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Mon, 6 Apr 2020 09:48:19 +0100 Subject: [PATCH 07/24] using passport to authenticate incoming api requests --- config/auth.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, ], From 224e2345cf33375e130bc5b1a079be7a02ed1b5b Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 09:43:31 +0100 Subject: [PATCH 08/24] making api request auth'd --- app/Providers/AppServiceProvider.php | 2 +- app/User.php | 2 +- routes/api.php | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 8863b52..87703a0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -24,7 +24,7 @@ public function register() */ public function boot() { - $this->registerPolicies(); + // $this->registerPolicies(); Passport::routes(); } diff --git a/app/User.php b/app/User.php index 55fe91b..4409fe2 100644 --- a/app/User.php +++ b/app/User.php @@ -8,7 +8,7 @@ class User extends Authenticatable { - use HasiPiTokens, Notifiable; + use HasApiTokens, Notifiable; /** * The attributes that are mass assignable. diff --git a/routes/api.php b/routes/api.php index 944e479..625d09f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,5 +21,6 @@ Route::prefix('organisation')->group(function () { Route::get('', 'OrganisationController@listAll'); - Route::post('', 'OrganisationController@create'); + Route::middleware('auth:api')->post('', 'OrganisationController@store'); }); + From 347ec2f4d2a5a55f8d2f278c7110d89f0e7debaa Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 10:45:44 +0100 Subject: [PATCH 09/24] organisation model created --- app/Organisation.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Organisation.php b/app/Organisation.php index e3e4d60..a05d090 100644 --- a/app/Organisation.php +++ b/app/Organisation.php @@ -25,12 +25,19 @@ */ class Organisation extends Model { + protected $table = 'organisations'; + use SoftDeletes; /** * @var array */ - protected $fillable = []; + protected $fillable = [ + 'name', + 'owner_user_id', + 'trial_end', + 'subscribed', + ]; /** * @var array From 93309027bbc213b4cf8234ddac81c68d4eb7e8fc Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 15:16:44 +0100 Subject: [PATCH 10/24] create organisation method working --- app/Organisation.php | 6 ++++-- app/Services/OrganisationService.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/Organisation.php b/app/Organisation.php index a05d090..e50ce5d 100644 --- a/app/Organisation.php +++ b/app/Organisation.php @@ -36,14 +36,16 @@ class Organisation extends Model 'name', 'owner_user_id', 'trial_end', - 'subscribed', + 'subscribed' ]; /** * @var array */ protected $dates = [ - 'deleted_at', + 'created_at', + 'updated_at', + 'deleted_at' ]; /** diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index 2218c84..f525260 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -5,6 +5,11 @@ namespace App\Services; use App\Organisation; +use Illuminate\Support\Facades\Auth; +use Carbon\Carbon; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\App; +use App\Transformers\OrganisationTransformer; /** * Class OrganisationService @@ -19,8 +24,17 @@ class OrganisationService */ public function createOrganisation(array $attributes): Organisation { + $auth = App::make(Auth::class); $organisation = new Organisation(); + + // $organisation->create($attributes); + $organisation->name = $attributes['name']; + $organisation->owner_user_id = $auth::id(); + $organisation->trial_end = Carbon::now()->add('day', 30); + + $organisation->save(); + return $organisation; } } From 9c2f5a175fbe2b98bf861866a1ebd8dcd58f21a4 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 16:21:24 +0100 Subject: [PATCH 11/24] transformer returning relevant json data after creating organisation --- app/Transformers/OrganisationTransformer.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Transformers/OrganisationTransformer.php b/app/Transformers/OrganisationTransformer.php index e55ef51..3093d91 100644 --- a/app/Transformers/OrganisationTransformer.php +++ b/app/Transformers/OrganisationTransformer.php @@ -20,7 +20,12 @@ class OrganisationTransformer extends TransformerAbstract */ public function transform(Organisation $organisation): array { - return []; + return [ + 'id' => (int) $organisation->id, + 'name' => (string) $organisation->name, + 'user_id' => (int) $organisation->owner_user_id, + 'trial_end' => (string) $organisation->trial_end + ]; } /** From 4bbfef1a41ca581e87b7846d51d9dc853113b69f Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 16:58:30 +0100 Subject: [PATCH 12/24] email send on organisation creation --- app/Mail/SendMail.php | 33 ++++++++++++++++++++++++++++ app/Services/OrganisationService.php | 9 ++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/Mail/SendMail.php diff --git a/app/Mail/SendMail.php b/app/Mail/SendMail.php new file mode 100644 index 0000000..561fc16 --- /dev/null +++ b/app/Mail/SendMail.php @@ -0,0 +1,33 @@ +view('view.name'); + } +} diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index f525260..4a87d68 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -5,7 +5,9 @@ namespace App\Services; use App\Organisation; +use App\Mail\SendEmail; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Mail; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\App; @@ -29,12 +31,15 @@ public function createOrganisation(array $attributes): Organisation // $organisation->create($attributes); + //save organisation $organisation->name = $attributes['name']; $organisation->owner_user_id = $auth::id(); $organisation->trial_end = Carbon::now()->add('day', 30); - $organisation->save(); - + + //send email + Mail::to($auth::user()->email)->send(new SendEmail()); + return $organisation; } } From 13cc5cdaa0019ded17b7e91f255043c9f7359166 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 17:28:11 +0100 Subject: [PATCH 13/24] mail fixed and sending to test server --- app/Mail/SendMail.php | 6 ++++-- app/Services/OrganisationService.php | 4 ++-- resources/views/confirmation.blade.php | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 resources/views/confirmation.blade.php diff --git a/app/Mail/SendMail.php b/app/Mail/SendMail.php index 561fc16..3c10d2e 100644 --- a/app/Mail/SendMail.php +++ b/app/Mail/SendMail.php @@ -11,14 +11,16 @@ class SendMail extends Mailable { use Queueable, SerializesModels; + public $data; + /** * Create a new message instance. * * @return void */ - public function __construct() + public function __construct($data) { - // + $this->data = $data; } /** diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index 4a87d68..90acacc 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -5,7 +5,7 @@ namespace App\Services; use App\Organisation; -use App\Mail\SendEmail; +use App\Mail\SendMail; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Mail; use Carbon\Carbon; @@ -38,7 +38,7 @@ public function createOrganisation(array $attributes): Organisation $organisation->save(); //send email - Mail::to($auth::user()->email)->send(new SendEmail()); + Mail::to($auth::user()->email)->send(new SendMail()); return $organisation; } diff --git a/resources/views/confirmation.blade.php b/resources/views/confirmation.blade.php new file mode 100644 index 0000000..7c810b3 --- /dev/null +++ b/resources/views/confirmation.blade.php @@ -0,0 +1 @@ +

This is the email

\ No newline at end of file From 022c99be920dc8b93cb6a2fc31f8e4b820c89a3b Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 17:44:21 +0100 Subject: [PATCH 14/24] mail working and sending correct data --- app/Mail/SendMail.php | 2 +- app/Services/OrganisationService.php | 6 +++++- resources/views/confirmation.blade.php | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Mail/SendMail.php b/app/Mail/SendMail.php index 3c10d2e..adfd63f 100644 --- a/app/Mail/SendMail.php +++ b/app/Mail/SendMail.php @@ -30,6 +30,6 @@ public function __construct($data) */ public function build() { - return $this->view('view.name'); + return $this->view('confirmation'); } } diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index 90acacc..ce618d5 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -38,7 +38,11 @@ public function createOrganisation(array $attributes): Organisation $organisation->save(); //send email - Mail::to($auth::user()->email)->send(new SendMail()); + $mailData['user'] = $auth::user()->name; + $mailData['organisation'] = $organisation->name; + $mailData['trial_end'] = (string)$organisation->trial_end; + + Mail::to($auth::user()->email)->send(new SendMail($mailData)); return $organisation; } diff --git a/resources/views/confirmation.blade.php b/resources/views/confirmation.blade.php index 7c810b3..0ef7219 100644 --- a/resources/views/confirmation.blade.php +++ b/resources/views/confirmation.blade.php @@ -1 +1,3 @@ -

This is the email

\ No newline at end of file +

Dear {{ $data['user'] }}

+

{{ $data['organisation']}} has been registered

+

Trial ends {{ $data['trial_end'] }}

From e8dafeb9d1a7e6dcd3763be4c27499a4ac455ea9 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 17:54:22 +0100 Subject: [PATCH 15/24] request validation implemented --- app/Http/Controllers/OrganisationController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 44e328f..36614b5 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -22,6 +22,10 @@ class OrganisationController extends ApiController */ public function store(OrganisationService $service): JsonResponse { + $this->request->validate([ + 'name' => 'required|unique:organisations|max:255' + ]); + /** @var Organisation $organisation */ $organisation = $service->createOrganisation($this->request->all()); From 7cc6a1182fd56923ae7af3e2f6ab82631a438a1d Mon Sep 17 00:00:00 2001 From: horidhenry Date: Tue, 7 Apr 2020 18:01:17 +0100 Subject: [PATCH 16/24] made datetime to unix for transformed data --- app/Transformers/OrganisationTransformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Transformers/OrganisationTransformer.php b/app/Transformers/OrganisationTransformer.php index 3093d91..4cd7746 100644 --- a/app/Transformers/OrganisationTransformer.php +++ b/app/Transformers/OrganisationTransformer.php @@ -24,7 +24,7 @@ public function transform(Organisation $organisation): array 'id' => (int) $organisation->id, 'name' => (string) $organisation->name, 'user_id' => (int) $organisation->owner_user_id, - 'trial_end' => (string) $organisation->trial_end + 'trial_end' => (int) strtotime((string)$organisation->trial_end) ]; } From 3bb7b6aeaab99e06db9663477be9adc9d4726b3a Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 07:14:04 +0100 Subject: [PATCH 17/24] user transformer file added --- app/Transformers/UserTransformer.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/Transformers/UserTransformer.php diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php new file mode 100644 index 0000000..e69de29 From 037f8122980af6ace5d07b4b94e471ea1f1b6feb Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 07:15:01 +0100 Subject: [PATCH 18/24] controller logic removed --- .../Controllers/OrganisationController.php | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 36614b5..425f24c 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -34,33 +34,14 @@ public function store(OrganisationService $service): JsonResponse ->respond(); } - public function listAll(OrganisationService $service) + public function listAll(OrganisationService $service): JsonResponse { - $filter = $_GET['filter'] ?: false; - $Organisations = DB::table('organisations')->get('*')->all(); + $filter = isset($_GET['filter']) ? $_GET['filter'] : null; - $Organisation_Array = array(); + $organisations = $service->getOrganisations($filter); - 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); - } - } - } - - return json_encode($Organisation_Array); + return $this + ->transformCollection('organisation', $organisations) + ->respond(); } } From a3da16d8e5fb045eadf92dcb0ef522aac97d30f9 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 07:15:20 +0100 Subject: [PATCH 19/24] model logic added to service --- app/Services/OrganisationService.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index ce618d5..e9640cb 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -46,4 +46,25 @@ public function createOrganisation(array $attributes): Organisation return $organisation; } + + public function getOrganisations($filter = null) + { + $organisation = new Organisation(); + + if ($filter === 'all' || !$filter) { + $Organisations = $organisation->all(); + } else { + if ($filter == 'subbed') { + $status = 1; + } elseif ($filter == 'trial') { + $status = 0; + } else { + //throw exception + } + + $Organisations = $organisation->where('subscribed', '=', $filter)->get(); + } + + return $Organisations; + } } From d30b75117390ce2d1ef86dd8567f48f62d7834be Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 10:04:43 +0100 Subject: [PATCH 20/24] foreign key and model relationship created --- app/Organisation.php | 2 +- .../2019_12_20_094217_create_organisations_table.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Organisation.php b/app/Organisation.php index e50ce5d..3e1427e 100644 --- a/app/Organisation.php +++ b/app/Organisation.php @@ -53,6 +53,6 @@ class Organisation extends Model */ public function owner(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class, 'owner_user_id'); } } diff --git a/database/migrations/2019_12_20_094217_create_organisations_table.php b/database/migrations/2019_12_20_094217_create_organisations_table.php index 5998727..eacd714 100644 --- a/database/migrations/2019_12_20_094217_create_organisations_table.php +++ b/database/migrations/2019_12_20_094217_create_organisations_table.php @@ -16,12 +16,16 @@ public function up() Schema::create('organisations', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); - $table->integer('owner_user_id'); + $table->unsignedBigInteger('owner_user_id')->index(); $table->dateTime('trial_end')->nullable(); $table->boolean('subscribed')->default(0); $table->timestamps(); $table->softDeletes(); }); + + Schema::table('organisations', function(Blueprint $table) { + $table->foreign('owner_user_id')->references('id')->on('users'); + }); } /** From 3c0b6990c43038eb2d34f152f91b5e4e1d78180e Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 10:55:40 +0100 Subject: [PATCH 21/24] fixed filter bug --- app/Services/OrganisationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index e9640cb..d56748f 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -62,7 +62,7 @@ public function getOrganisations($filter = null) //throw exception } - $Organisations = $organisation->where('subscribed', '=', $filter)->get(); + $Organisations = $organisation->where('subscribed', '=', $status)->get(); } return $Organisations; From 31f54499f6f35663e6bdd3aa5c77c53886d469aa Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 10:56:11 +0100 Subject: [PATCH 22/24] organisation transformer completed --- app/Transformers/OrganisationTransformer.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/Transformers/OrganisationTransformer.php b/app/Transformers/OrganisationTransformer.php index 4cd7746..621ed89 100644 --- a/app/Transformers/OrganisationTransformer.php +++ b/app/Transformers/OrganisationTransformer.php @@ -20,11 +20,18 @@ class OrganisationTransformer extends TransformerAbstract */ public function transform(Organisation $organisation): array { + $owner = $this->includeUser($organisation); + return [ 'id' => (int) $organisation->id, 'name' => (string) $organisation->name, - 'user_id' => (int) $organisation->owner_user_id, - 'trial_end' => (int) strtotime((string)$organisation->trial_end) + 'owner' => [ + 'user_id' => (int) $organisation->owner_user_id, + 'user_name' => $owner['name'], + 'user_email' => $owner['email'] + ], + 'subscribed' => (bool) $organisation->subscribed, + 'trial_end' => ($organisation->trial_end ? (int) strtotime((string)$organisation->trial_end) : null) ]; } @@ -35,6 +42,9 @@ public function transform(Organisation $organisation): array */ public function includeUser(Organisation $organisation) { - return $this->item($organisation->user, new UserTransformer()); + $UserTransformer = new UserTransformer(); + $owner = $UserTransformer->transform($organisation->owner); + + return $owner; } } From 3b7250b2858c81eeb7c9b541101b307b95ccf657 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 10:56:26 +0100 Subject: [PATCH 23/24] user transformer completed --- app/Transformers/UserTransformer.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index e69de29..5b1ee4d 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -0,0 +1,28 @@ + (string) $user->name, + 'email' => (string) $user->email, + ]; + } +} From da9f87bb1874a472c991489e0bfd68ef25b5e9c3 Mon Sep 17 00:00:00 2001 From: horidhenry Date: Wed, 8 Apr 2020 11:13:47 +0100 Subject: [PATCH 24/24] code tidied and error handling --- app/Http/Controllers/OrganisationController.php | 5 +++++ app/Services/OrganisationService.php | 7 ++++++- app/User.php | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 425f24c..b2db88e 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -34,6 +34,11 @@ public function store(OrganisationService $service): JsonResponse ->respond(); } + /** + * @param OrganisationService $service + * + * @return JsonResponse + */ public function listAll(OrganisationService $service): JsonResponse { $filter = isset($_GET['filter']) ? $_GET['filter'] : null; diff --git a/app/Services/OrganisationService.php b/app/Services/OrganisationService.php index d56748f..c069e8c 100644 --- a/app/Services/OrganisationService.php +++ b/app/Services/OrganisationService.php @@ -47,6 +47,11 @@ public function createOrganisation(array $attributes): Organisation return $organisation; } + /** + * @param string $filter + * + * @return Organisations + */ public function getOrganisations($filter = null) { $organisation = new Organisation(); @@ -59,7 +64,7 @@ public function getOrganisations($filter = null) } elseif ($filter == 'trial') { $status = 0; } else { - //throw exception + throw new \Exception('Filter param imvalid'); } $Organisations = $organisation->where('subscribed', '=', $status)->get(); diff --git a/app/User.php b/app/User.php index 4409fe2..850ee98 100644 --- a/app/User.php +++ b/app/User.php @@ -39,4 +39,12 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + /** + * @return HasMany + */ + public function organisation(): HasMany + { + return $this->hasMany(organisation::class, 'id', 'onwer_user_id'); + } }