-
-
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: manage religions (monicahq/chandler#272)
- Loading branch information
Showing
29 changed files
with
1,509 additions
and
1 deletion.
There are no files selected for viewing
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
app/Domains/Settings/ManageReligion/Services/CreateReligion.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\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
63
app/Domains/Settings/ManageReligion/Services/DestroyReligion.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\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
71
app/Domains/Settings/ManageReligion/Services/UpdateReligion.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,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(); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
app/Domains/Settings/ManageReligion/Services/UpdateReligionPosition.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,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'); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
app/Domains/Settings/ManageReligion/Web/Controllers/PersonalizeReligionController.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,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); | ||
} | ||
} |
Oops, something went wrong.