Skip to content

Commit

Permalink
feat: crud template pages (monicahq/chandler#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Jan 10, 2022
1 parent ae7f231 commit 611ab77
Show file tree
Hide file tree
Showing 19 changed files with 1,133 additions and 55 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace App\Http\Controllers\Settings\Personalize\Templates;

use Inertia\Inertia;
use App\Models\Template;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Services\Account\ManageTemplate\CreateTemplate;
use App\Services\Account\ManageTemplate\UpdateTemplate;
use App\Services\Account\ManageTemplate\DestroyTemplate;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Http\Controllers\Vault\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplateShowViewHelper;
use App\Http\Controllers\Settings\Personalize\Templates\ViewHelpers\PersonalizeTemplateIndexViewHelper;

class PersonalizeTemplatesController extends Controller
Expand Down Expand Up @@ -67,4 +70,19 @@ public function destroy(Request $request, int $templateId)
'data' => true,
], 200);
}

public function show(Request $request, int $templateId)
{
try {
$template = Template::where('account_id', Auth::user()->account_id)
->findOrFail($templateId);
} catch (ModelNotFoundException) {
return redirect('vaults');
}

return Inertia::render('Settings/Personalize/Templates/Show', [
'layoutData' => VaultIndexViewHelper::layoutData(),
'data' => PersonalizeTemplateShowViewHelper::data($template),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static function dtoTemplate(Template $template): array
'id' => $template->id,
'name' => $template->name,
'url' => [
'show' => route('settings.personalize.template.show', [
'template' => $template->id,
]),
'update' => route('settings.personalize.template.update', [
'template' => $template->id,
]),
Expand Down
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,
]),
],
];
}
}
10 changes: 10 additions & 0 deletions app/Models/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public function account()
{
return $this->belongsTo(Account::class);
}

/**
* Get the template pages associated with the module.
*
* @return BelongsToMany
*/
public function templatePages()
{
return $this->belongsToMany(TemplatePage::class, 'module_template_page')->withTimestamps();
}
}
19 changes: 19 additions & 0 deletions app/Models/TemplatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class TemplatePage extends Model
'position',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'position' => 'integer',
];

/**
* Get the account associated with the template page.
*
Expand All @@ -32,4 +41,14 @@ public function template()
{
return $this->belongsTo(Template::class);
}

/**
* Get the modules associated with the template page.
*
* @return BelongsToMany
*/
public function modules()
{
return $this->belongsToMany(Module::class, 'module_template_page')->withTimestamps();
}
}
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 app/Services/Account/ManageTemplate/UpdateTemplatePagePosition.php
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');
}
}
Loading

0 comments on commit 611ab77

Please sign in to comment.