Skip to content

Commit

Permalink
feat: manage religions (monicahq/chandler#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Nov 2, 2022
1 parent 4f35954 commit 785db8b
Show file tree
Hide file tree
Showing 29 changed files with 1,509 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function data(): array
'manage_gift_states' => route('settings.personalize.gift_states.index'),
'manage_group_types' => route('settings.personalize.group_types.index'),
'manage_post_templates' => route('settings.personalize.post_templates.index'),
'manage_religions' => route('settings.personalize.religions.index'),
],
];
}
Expand Down
77 changes: 77 additions & 0 deletions app/Domains/Settings/ManageReligion/Services/CreateReligion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Domains\Settings\ManageReligion\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Religion;
use App\Models\User;
use App\Services\BaseService;

class CreateReligion extends BaseService implements ServiceInterface
{
private array $data;

private Religion $religion;

/**
* 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',
'name' => 'required|string|max:255',
];
}

/**
* 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',
];
}

/**
* Create a religion.
*
* @param array $data
* @return Religion
*/
public function execute(array $data): Religion
{
$this->data = $data;

$this->validate();
$this->create();

return $this->religion;
}

private function validate(): void
{
$this->validateRules($this->data);
}

private function create(): void
{
// determine the new position of the religion
$newPosition = Religion::where('account_id', $this->data['account_id'])
->max('position');
$newPosition++;

$this->religion = Religion::create([
'account_id' => $this->data['account_id'],
'name' => $this->data['name'],
'position' => $newPosition,
]);
}
}
63 changes: 63 additions & 0 deletions app/Domains/Settings/ManageReligion/Services/DestroyReligion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Domains\Settings\ManageReligion\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Religion;
use App\Models\User;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;

class DestroyReligion extends BaseService implements ServiceInterface
{
private Religion $religion;

/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'religion_id' => 'required|integer|exists:religions,id',
'author_id' => 'required|integer|exists:users,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',
];
}

/**
* Destroy a religion.
*
* @param array $data
*/
public function execute(array $data): void
{
$this->validateRules($data);

$this->religion = Religion::where('account_id', $data['account_id'])
->findOrFail($data['religion_id']);

$this->religion->delete();

$this->repositionEverything();
}

private function repositionEverything(): void
{
DB::table('religions')->where('position', '>', $this->religion->position)->decrement('position');
}
}
71 changes: 71 additions & 0 deletions app/Domains/Settings/ManageReligion/Services/UpdateReligion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Domains\Settings\ManageReligion\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Religion;
use App\Models\User;
use App\Services\BaseService;

class UpdateReligion extends BaseService implements ServiceInterface
{
private array $data;

private Religion $religion;

/**
* 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',
'religion_id' => 'required|integer|exists:religions,id',
'name' => 'required|string|max:255',
];
}

/**
* 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 a religion.
*
* @param array $data
* @return Religion
*/
public function execute(array $data): Religion
{
$this->data = $data;
$this->validate();
$this->update();

return $this->religion;
}

private function validate(): void
{
$this->validateRules($this->data);
$this->religion = Religion::where('account_id', $this->data['account_id'])
->findOrFail($this->data['religion_id']);
}

private function update(): void
{
$this->religion->name = $this->data['name'];
$this->religion->save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace App\Domains\Settings\ManageReligion\Services;

use App\Interfaces\ServiceInterface;
use App\Models\Religion;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;

class UpdateReligionPosition extends BaseService implements ServiceInterface
{
private Religion $religion;

private int $pastPosition;

private array $data;

/**
* 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',
'religion_id' => 'required|integer|exists:religions,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 gift occasion's position.
*
* @param array $data
* @return Religion
*/
public function execute(array $data): Religion
{
$this->data = $data;
$this->validate();
$this->updatePosition();

return $this->religion;
}

private function validate(): void
{
$this->validateRules($this->data);

$this->religion = Religion::where('account_id', $this->data['account_id'])
->findOrFail($this->data['religion_id']);

$this->pastPosition = DB::table('religions')
->where('id', $this->religion->id)
->select('position')
->first()->position;
}

private function updatePosition(): void
{
if ($this->data['new_position'] > $this->pastPosition) {
$this->updateAscendingPosition();
} else {
$this->updateDescendingPosition();
}

DB::table('religions')
->where('id', $this->religion->id)
->update([
'position' => $this->data['new_position'],
]);
}

private function updateAscendingPosition(): void
{
DB::table('religions')
->where('position', '>', $this->pastPosition)
->where('position', '<=', $this->data['new_position'])
->decrement('position');
}

private function updateDescendingPosition(): void
{
DB::table('religions')
->where('position', '>=', $this->data['new_position'])
->where('position', '<', $this->pastPosition)
->increment('position');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Domains\Settings\ManageReligion\Web\Controllers;

use App\Domains\Settings\ManageReligion\Services\CreateReligion;
use App\Domains\Settings\ManageReligion\Services\DestroyReligion;
use App\Domains\Settings\ManageReligion\Services\UpdateReligion;
use App\Domains\Settings\ManageReligion\Web\ViewHelpers\PersonalizeReligionViewHelper;
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;

class PersonalizeReligionController extends Controller
{
public function index(): Response
{
return Inertia::render('Settings/Personalize/Religions/Index', [
'layoutData' => VaultIndexViewHelper::layoutData(),
'data' => PersonalizeReligionViewHelper::data(Auth::user()->account),
]);
}

public function store(Request $request): JsonResponse
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'name' => $request->input('name'),
];

$religion = (new CreateReligion())->execute($data);

return response()->json([
'data' => PersonalizeReligionViewHelper::dto($religion),
], 201);
}

public function update(Request $request, int $religionId): JsonResponse
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'religion_id' => $religionId,
'name' => $request->input('name'),
];

$religion = (new UpdateReligion())->execute($data);

return response()->json([
'data' => PersonalizeReligionViewHelper::dto($religion),
], 200);
}

public function destroy(Request $request, int $religionId): JsonResponse
{
$data = [
'account_id' => Auth::user()->account_id,
'author_id' => Auth::user()->id,
'religion_id' => $religionId,
];

(new DestroyReligion())->execute($data);

return response()->json([
'data' => true,
], 200);
}
}
Loading

0 comments on commit 785db8b

Please sign in to comment.