From 8ac5f5b9d894289e17316b84f67c1f68d54390bb Mon Sep 17 00:00:00 2001 From: Chirag <103994754+chiragnotionforms@users.noreply.github.com> Date: Wed, 21 Sep 2022 18:29:32 +0530 Subject: [PATCH] Improved DB performance & loading times (#1) --- app/Http/Controllers/Auth/UserController.php | 5 +-- app/Http/Controllers/Forms/FormController.php | 25 ++++++++++++- app/Http/Resources/FormResource.php | 37 ++++++++++++++----- app/Http/Resources/UserResource.php | 27 ++++++++++++++ app/Models/Forms/Form.php | 1 - app/Models/User.php | 5 --- 6 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 app/Http/Resources/UserResource.php diff --git a/app/Http/Controllers/Auth/UserController.php b/app/Http/Controllers/Auth/UserController.php index 232036f..a637ae9 100644 --- a/app/Http/Controllers/Auth/UserController.php +++ b/app/Http/Controllers/Auth/UserController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use App\Http\Resources\UserResource; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -11,12 +12,10 @@ class UserController extends Controller /** * Get authenticated user. * - * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\JsonResponse */ public function current(Request $request) { - return response()->json($request->user()); + return new UserResource($request->user()); } public function deleteAccount() { diff --git a/app/Http/Controllers/Forms/FormController.php b/app/Http/Controllers/Forms/FormController.php index 323555d..f541de5 100644 --- a/app/Http/Controllers/Forms/FormController.php +++ b/app/Http/Controllers/Forms/FormController.php @@ -33,7 +33,17 @@ class FormController extends Controller $this->authorize('view', $workspace); $this->authorize('viewAny', Form::class); - return FormResource::collection($workspace->forms); + $workspaceIsPro = $workspace->is_pro; + $forms = $workspace->forms()->with(['creator','views','submissions'])->get()->map(function (Form $form) use ($workspace, $workspaceIsPro){ + // Add attributes for faster loading + $form->extra = (object) [ + 'loadedWorkspace' => $workspace, + 'workspaceIsPro' => $workspaceIsPro, + 'userIsOwner' => true, + ]; + return $form; + }); + return FormResource::collection($forms); } /** @@ -47,7 +57,18 @@ class FormController extends Controller $this->authorize('view', $workspace); $this->authorize('viewAny', Form::class); - $forms = $forms->merge($workspace->forms); + $workspaceIsPro = $workspace->is_pro; + $newForms = $workspace->forms()->with(['creator','views','submissions'])->get()->map(function (Form $form) use ($workspace, $workspaceIsPro){ + // Add attributes for faster loading + $form->extra = (object) [ + 'loadedWorkspace' => $workspace, + 'workspaceIsPro' => $workspaceIsPro, + 'userIsOwner' => true, + ]; + return $form; + }); + + $forms = $forms->merge($newForms); } return FormResource::collection($forms); } diff --git a/app/Http/Resources/FormResource.php b/app/Http/Resources/FormResource.php index a8bfeda..4aca39c 100644 --- a/app/Http/Resources/FormResource.php +++ b/app/Http/Resources/FormResource.php @@ -3,13 +3,14 @@ namespace App\Http\Resources; use App\Http\Middleware\Form\PasswordProtectedForm; +use App\Http\Resources\UserResource; use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; class FormResource extends JsonResource { - private Array $cleanings = []; + private array $cleanings = []; /** * Transform the resource into an array. @@ -19,15 +20,14 @@ class FormResource extends JsonResource */ public function toArray($request) { - $userIsFormOwner = Auth::check() && Auth::user()->workspaces()->find($this->workspace_id) !== null; - if(!$userIsFormOwner && $this->doesMissPassword($request)){ + if(!$this->userIsFormOwner() && $this->doesMissPassword($request)){ return $this->getPasswordProtectedForm(); } - $ownerData = $userIsFormOwner ? [ - 'creator' => $this->creator, - 'views_count' => $this->when($this->workspace->is_pro, $this->views_count), - 'submissions_count' => $this->when($this->workspace->is_pro, $this->submissions_count), + $ownerData = $this->userIsFormOwner() ? [ + 'creator' => new UserResource($this->creator), + 'views_count' => $this->when($this->workspaceIsPro(), $this->views_count), + 'submissions_count' => $this->when($this->workspaceIsPro(), $this->submissions_count), 'notifies' => $this->notifies, 'send_submission_confirmation' => $this->send_submission_confirmation, 'webhook_url' => $this->webhook_url, @@ -44,11 +44,12 @@ class FormResource extends JsonResource 'notification_emails' => $this->notification_emails, ] : []; - $baseData = $this->getFilteredFormData(parent::toArray($request), $userIsFormOwner); + $baseData = $this->getFilteredFormData(parent::toArray($request), $this->userIsFormOwner()); return array_merge($baseData, $ownerData, [ + 'is_pro' => $this->workspaceIsPro(), 'workspace_id' => $this->workspace_id, - 'workspace' => new WorkspaceResource($this->workspace), + 'workspace' => new WorkspaceResource($this->getWorkspace()), 'is_closed' => $this->is_closed, 'is_password_protected' => false, 'has_password' => $this->has_password, @@ -86,7 +87,7 @@ class FormResource extends JsonResource private function doesMissPassword(Request $request) { - if (!$this->is_pro || !$this->has_password) return false; + if (!$this->workspaceIsPro() || !$this->has_password) return false; return !PasswordProtectedForm::hasCorrectPassword($request, $this->resource); } @@ -107,4 +108,20 @@ class FormResource extends JsonResource 'properties' => [] ]; } + + private function getWorkspace() { + return $this->extra?->loadedWorkspace ?? $this->workspace; + } + + private function workspaceIsPro() { + return $this->extra?->workspaceIsPro ?? $this->getWorkspace()->is_pro ?? $this->is_pro; + } + + private function userIsFormOwner() { + return $this->extra?->userIsOwner ?? + ( + Auth::check() + && Auth::user()->workspaces()->find($this->workspace_id) !== null + ); + } } diff --git a/app/Http/Resources/UserResource.php b/app/Http/Resources/UserResource.php new file mode 100644 index 0000000..6a99033 --- /dev/null +++ b/app/Http/Resources/UserResource.php @@ -0,0 +1,27 @@ +id ? [ + 'is_subscribed' => $this->is_subscribed, + 'has_enterprise_subscription' => $this->has_enterprise_subscription, + 'admin' => $this->admin, + 'has_customer_id' => $this->has_customer_id, + 'has_forms' => $this->has_forms, + ] : []; + + return array_merge(parent::toArray($request), $personalData); + } +} diff --git a/app/Models/Forms/Form.php b/app/Models/Forms/Form.php index 5e4f8df..7f7629c 100644 --- a/app/Models/Forms/Form.php +++ b/app/Models/Forms/Form.php @@ -88,7 +88,6 @@ class Form extends Model protected $appends = [ 'share_url', - 'is_pro' ]; protected $hidden = [ diff --git a/app/Models/User.php b/app/Models/User.php index 079ee5e..625cc09 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -61,11 +61,6 @@ class User extends Authenticatable implements JWTSubject //, MustVerifyEmail */ protected $appends = [ 'photo_url', - 'is_subscribed', - 'has_enterprise_subscription', - 'admin', - 'has_customer_id', - 'has_forms' ]; protected $withCount = ['workspaces'];