-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modules management (monicahq/chandler#21)
- Loading branch information
Showing
18 changed files
with
1,173 additions
and
196 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...p/Controllers/Settings/Personalize/Templates/PersonalizeTemplatePageModulesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...llers/Settings/Personalize/Templates/PersonalizeTemplatePageModulesPositionController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...lers/Settings/Personalize/Templates/ViewHelpers/PersonalizeTemplatePageShowViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/Services/Account/ManageTemplate/RemoveModuleFromTemplatePage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.