-
-
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: crud template pages (monicahq/chandler#19)
- Loading branch information
Showing
19 changed files
with
1,133 additions
and
55 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/Http/Controllers/Settings/Personalize/Templates/PersonalizeTemplatePagesController.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,63 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Settings\Personalize\Templates; | ||
|
||
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; | ||
|
||
class PersonalizeTemplatePagesController extends Controller | ||
{ | ||
public function store(Request $request, int $templateId) | ||
{ | ||
$data = [ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'template_id' => $templateId, | ||
'name' => $request->input('name'), | ||
]; | ||
|
||
$templatePage = (new CreateTemplatePage)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => PersonalizeTemplateShowViewHelper::dtoTemplatePage($templatePage->template, $templatePage), | ||
], 201); | ||
} | ||
|
||
public function update(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, | ||
'name' => $request->input('name'), | ||
]; | ||
|
||
$templatePage = (new UpdateTemplatePage)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => PersonalizeTemplateShowViewHelper::dtoTemplatePage($templatePage->template, $templatePage), | ||
], 200); | ||
} | ||
|
||
public function destroy(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, | ||
]; | ||
|
||
(new DestroyTemplatePage)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => true, | ||
], 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
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
55 changes: 55 additions & 0 deletions
55
...trollers/Settings/Personalize/Templates/ViewHelpers/PersonalizeTemplateShowViewHelper.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,55 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers; | ||
|
||
use App\Models\Template; | ||
use App\Models\TemplatePage; | ||
|
||
class PersonalizeTemplateShowViewHelper | ||
{ | ||
public static function data(Template $template): array | ||
{ | ||
$templatePages = $template->pages() | ||
->orderBy('position', 'asc') | ||
->get(); | ||
|
||
$collection = collect(); | ||
foreach ($templatePages as $templatePage) { | ||
$collection->push(self::dtoTemplatePage($template, $templatePage)); | ||
} | ||
|
||
return [ | ||
'template' => [ | ||
'id' => $template->id, | ||
'name' => $template->name, | ||
], | ||
'template_pages' => $collection, | ||
'url' => [ | ||
'settings' => route('settings.index'), | ||
'personalize' => route('settings.personalize.index'), | ||
'templates' => route('settings.personalize.template.index'), | ||
'template_page_store' => route('settings.personalize.template.template_page.store', [ | ||
'template' => $template->id, | ||
]), | ||
], | ||
]; | ||
} | ||
|
||
public static function dtoTemplatePage(Template $template, TemplatePage $page): array | ||
{ | ||
return [ | ||
'id' => $page->id, | ||
'name' => $page->name, | ||
'url' => [ | ||
'update' => route('settings.personalize.template.template_page.update', [ | ||
'template' => $template->id, | ||
'page' => $page->id, | ||
]), | ||
'destroy' => route('settings.personalize.template.template_page.destroy', [ | ||
'template' => $template->id, | ||
'page' => $page->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
79 changes: 79 additions & 0 deletions
79
app/Services/Account/ManageTemplate/AssociateModuleToTemplatePage.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,79 @@ | ||
<?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 AssociateModuleToTemplatePage extends BaseService implements ServiceInterface | ||
{ | ||
private array $data; | ||
private Template $template; | ||
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', | ||
'position' => 'required|integer', | ||
]; | ||
} | ||
|
||
/** | ||
* 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', | ||
]; | ||
} | ||
|
||
/** | ||
* Associate a module with a template page. | ||
* | ||
* @param array $data | ||
* @return TemplatePage | ||
*/ | ||
public function execute(array $data): TemplatePage | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
|
||
$this->templatePage->modules()->syncWithoutDetaching([ | ||
$this->module->id => ['position' => $data['position']], | ||
]); | ||
|
||
return $this->templatePage; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$this->module = Module::where('account_id', $this->data['account_id']) | ||
->findOrFail($this->data['module_id']); | ||
|
||
$this->template = Template::where('account_id', $this->data['account_id']) | ||
->findOrFail($this->data['template_id']); | ||
|
||
$this->templatePage = TemplatePage::where('template_id', $this->data['template_id']) | ||
->findOrFail($this->data['template_page_id']); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
app/Services/Account/ManageTemplate/UpdateTemplatePagePosition.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,106 @@ | ||
<?php | ||
|
||
namespace App\Services\Account\ManageTemplate; | ||
|
||
use App\Models\Template; | ||
use App\Models\TemplatePage; | ||
use App\Services\BaseService; | ||
use Illuminate\Support\Facades\DB; | ||
use App\Interfaces\ServiceInterface; | ||
|
||
class UpdateTemplatePagePosition extends BaseService implements ServiceInterface | ||
{ | ||
private Template $template; | ||
private TemplatePage $templatePage; | ||
|
||
/** | ||
* 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', | ||
'new_position' => 'required|integer', | ||
]; | ||
} | ||
|
||
/** | ||
* 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', | ||
]; | ||
} | ||
|
||
/** | ||
* Update the template page position. | ||
* | ||
* @param array $data | ||
* @return TemplatePage | ||
*/ | ||
public function execute(array $data): TemplatePage | ||
{ | ||
$this->data = $data; | ||
$this->validate(); | ||
$this->updatePosition(); | ||
|
||
return $this->templatePage; | ||
} | ||
|
||
private function validate(): void | ||
{ | ||
$this->validateRules($this->data); | ||
|
||
$this->template = Template::where('account_id', $this->data['account_id']) | ||
->findOrFail($this->data['template_id']); | ||
|
||
$this->templatePage = TemplatePage::where('template_id', $this->data['template_id']) | ||
->findOrFail($this->data['template_page_id']); | ||
|
||
$this->pastPosition = $this->templatePage->position; | ||
} | ||
|
||
private function updatePosition(): void | ||
{ | ||
if ($this->data['new_position'] > $this->pastPosition) { | ||
$this->updateAscendingPosition(); | ||
} else { | ||
$this->updateDescendingPosition(); | ||
} | ||
|
||
DB::table('template_pages') | ||
->where('template_id', $this->template->id) | ||
->where('id', $this->templatePage->id) | ||
->update([ | ||
'position' => $this->data['new_position'], | ||
]); | ||
} | ||
|
||
private function updateAscendingPosition(): void | ||
{ | ||
DB::table('template_pages') | ||
->where('template_id', $this->template->id) | ||
->where('position', '>', $this->pastPosition) | ||
->where('position', '<=', $this->data['new_position']) | ||
->decrement('position'); | ||
} | ||
|
||
private function updateDescendingPosition(): void | ||
{ | ||
DB::table('template_pages') | ||
->where('template_id', $this->template->id) | ||
->where('position', '>=', $this->data['new_position']) | ||
->where('position', '<', $this->pastPosition) | ||
->increment('position'); | ||
} | ||
} |
Oops, something went wrong.