Skip to content

Commit

Permalink
feat: modules management (monicahq/chandler#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jan 11, 2022
1 parent 55dedce commit d7b2910
Show file tree
Hide file tree
Showing 18 changed files with 1,173 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Controllers\Settings\Personalize\Templates;

use App\Models\TemplatePage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Account\ManageTemplate\RemoveModuleFromTemplatePage;
use App\Services\Account\ManageTemplate\AssociateModuleToTemplatePage;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplatePageShowViewHelper;

class PersonalizeTemplatePageModulesController extends Controller
{
public function store(Request $request, int $templateId, int $templatePageId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'template_id' => $templateId,
'template_page_id' => $templatePageId,
'module_id' => $request->input('module_id'),
];

$templatePage = TemplatePage::findOrFail($templatePageId);

$module = (new AssociateModuleToTemplatePage)->execute($data);

return response()->json([
'data' => PersonalizeTemplatePageShowViewHelper::dtoModule($templatePage, $module),
], 201);
}

public function destroy(Request $request, int $templateId, int $templatePageId, int $moduleId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'template_id' => $templateId,
'template_page_id' => $templatePageId,
'module_id' => $moduleId,
];

$templatePage = TemplatePage::findOrFail($templatePageId);

$module = (new RemoveModuleFromTemplatePage)->execute($data);

return response()->json([
'data' => PersonalizeTemplatePageShowViewHelper::dtoModule($templatePage, $module),
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Controllers\Settings\Personalize\Templates;

use App\Models\TemplatePage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Account\ManageTemplate\UpdateModulePosition;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplatePageShowViewHelper;

class PersonalizeTemplatePageModulesPositionController extends Controller
{
public function update(Request $request, int $templateId, int $templatePageId, int $moduleId)
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'template_id' => $templateId,
'template_page_id' => $templatePageId,
'module_id' => $moduleId,
'new_position' => $request->input('position'),
];

$templatePage = TemplatePage::findOrFail($templatePageId);
$module = (new UpdateModulePosition)->execute($data);

return response()->json([
'data' => PersonalizeTemplatePageShowViewHelper::dtoModule($templatePage, $module),
], 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Http\Controllers\Settings\Personalize\Templates;

use App\Models\Template;
use App\Models\TemplatePage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Account\ManageTemplate\CreateTemplatePage;
use App\Services\Account\ManageTemplate\UpdateTemplatePage;
use App\Services\Account\ManageTemplate\DestroyTemplatePage;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplateShowViewHelper;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplatePageShowViewHelper;

class PersonalizeTemplatePagesController extends Controller
{
Expand Down Expand Up @@ -60,4 +63,17 @@ public function destroy(Request $request, int $templateId, int $templatePageId)
'data' => true,
], 200);
}

public function show(Request $request, int $templateId, int $templatePageId)
{
$template = Template::where('account_id', Auth::user()->account_id)
->findOrFail($templateId);

$templatePage = TemplatePage::where('template_id', $template->id)
->findOrFail($templatePageId);

return response()->json([
'data' => PersonalizeTemplatePageShowViewHelper::data($templatePage),
], 200);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers;

use App\Models\Module;
use App\Models\TemplatePage;

class PersonalizeTemplatePageShowViewHelper
{
public static function data(TemplatePage $templatePage): array
{
$allModules = $templatePage->template->account->modules()
->orderBy('name', 'asc')
->get();

$modulesInPage = $templatePage->modules()
->orderBy('position', 'asc')
->get();

$modulesInAccountCollection = collect();
foreach ($allModules as $module) {
$modulesInAccountCollection->push([
'id' => $module->id,
'name' => $module->name,
'already_used' => $modulesInPage->contains($module),
'url' => [
'destroy' => route('settings.personalize.template.template_page.module.destroy', [
'template' => $templatePage->template->id,
'page' => $templatePage->id,
'module' => $module->id,
]),
],
]);
}

$modulesIntemplateCollection = collect();
foreach ($modulesInPage as $module) {
$modulesIntemplateCollection->push(self::dtoModule($templatePage, $module));
}

return [
'page' => [
'id' => $templatePage->id,
'name' => $templatePage->name,
],
'modules' => $modulesIntemplateCollection,
'modules_in_account' => $modulesInAccountCollection,
'url' => [
'store' => route('settings.personalize.template.template_page.module.store', [
'template' => $templatePage->template->id,
'page' => $templatePage->id,
]),
],
];
}

public static function dtoModule(TemplatePage $templatePage, Module $module): array
{
return [
'id' => $module->id,
'name' => $module->name,
'position' => $module->position,
'url' => [
'position' => route('settings.personalize.template.template_page.module.order.update', [
'template' => $templatePage->template->id,
'page' => $templatePage->id,
'module' => $module->id,
]),
'destroy' => route('settings.personalize.template.template_page.module.destroy', [
'template' => $templatePage->template->id,
'page' => $templatePage->id,
'module' => $module->id,
]),
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public static function dtoTemplatePage(Template $template, TemplatePage $page):
'name' => $page->name,
'position' => $page->position,
'url' => [
'show' => route('settings.personalize.template.template_page.show', [
'template' => $template->id,
'page' => $page->id,
]),
'update' => route('settings.personalize.template.template_page.update', [
'template' => $template->id,
'page' => $page->id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
class AssociateModuleToTemplatePage extends BaseService implements ServiceInterface
{
private array $data;
private Template $template;
private TemplatePage $templatePage;
private Module $module;

Expand All @@ -28,7 +27,6 @@ public function rules(): array
'template_id' => 'required|integer|exists:templates,id',
'template_page_id' => 'required|integer|exists:template_pages,id',
'module_id' => 'required|integer|exists:modules,id',
'position' => 'required|integer',
];
}

Expand All @@ -49,18 +47,20 @@ public function permissions(): array
* Associate a module with a template page.
*
* @param array $data
* @return TemplatePage
* @return Module
*/
public function execute(array $data): TemplatePage
public function execute(array $data): Module
{
$this->data = $data;
$this->validate();

$newPosition = $this->templatePage->modules()->max('position') + 1;

$this->templatePage->modules()->syncWithoutDetaching([
$this->module->id => ['position' => $data['position']],
$this->module->id => ['position' => $newPosition],
]);

return $this->templatePage;
return $this->module;
}

private function validate(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Services\Account\ManageTemplate;

use App\Models\Module;
use App\Models\Template;
use App\Models\TemplatePage;
use App\Services\BaseService;
use App\Interfaces\ServiceInterface;

class RemoveModuleFromTemplatePage extends BaseService implements ServiceInterface
{
private TemplatePage $templatePage;
private Module $module;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'template_id' => 'required|integer|exists:templates,id',
'template_page_id' => 'required|integer|exists:template_pages,id',
'module_id' => 'required|integer|exists:modules,id',
];
}

/**
* Get the permissions that apply to the user calling the service.
*
* @return array
*/
public function permissions(): array
{
return [
'author_must_belong_to_account',
'author_must_be_account_administrator',
];
}

/**
* Remove a module from a template page.
*
* @param array $data
* @return Module
*/
public function execute(array $data): Module
{
$this->validateRules($data);

$this->module = Module::where('account_id', $data['account_id'])
->findOrFail($data['module_id']);

$this->template = Template::where('account_id', $data['account_id'])
->findOrFail($data['template_id']);

$this->templatePage = TemplatePage::where('template_id', $data['template_id'])
->findOrFail($data['template_page_id']);

$this->templatePage->modules()->toggle([
$this->module->id,
]);

return $this->module;
}
}
Loading

0 comments on commit d7b2910

Please sign in to comment.