Skip to content

Commit

Permalink
refactor(controller): pass owner to validation makers
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 23, 2023
1 parent 17199ef commit 7983ec7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 10 additions & 4 deletions app/Controllers/BaseOwnedResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Exceptions\HTTPExceptionInterface;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Validation\Validation;

use App\Contracts\OwnedResource;
Expand All @@ -23,8 +24,11 @@ abstract protected static function getIndividualName(): string;
abstract protected static function getCollectiveName(): string;
abstract protected static function getModelName(): string;

abstract protected static function makeCreateValidation(): Validation;
abstract protected static function makeUpdateValidation(int $id): Validation;
abstract protected static function makeCreateValidation(User $owner): Validation;
abstract protected static function makeUpdateValidation(
User $owner,
int $resource_id
): Validation;

public static function getInfo(): OwnedResourceInfo {
return new OwnedResourceInfo(
Expand Down Expand Up @@ -133,8 +137,9 @@ public function show(int $id)

public function create()
{
$current_user = auth()->user();
$controller = $this;
$validation = $this->makeCreateValidation();
$validation = static::makeCreateValidation($current_user);
return $this
->useValidInputsOnly(
$validation,
Expand Down Expand Up @@ -185,8 +190,9 @@ function($request_data) use ($controller) {

public function update(int $id)
{
$current_user = auth()->user();
$controller = $this;
$validation = $this->makeUpdateValidation($id);
$validation = $this->makeUpdateValidation($current_user, $id);
return $this
->useValidInputsOnly(
$validation,
Expand Down
13 changes: 6 additions & 7 deletions app/Controllers/CurrencyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controllers;

use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Validation\Validation;

use App\Contracts\OwnedResource;
Expand All @@ -21,13 +22,12 @@ protected static function getModelName(): string {
return CurrencyModel::class;
}

protected static function makeCreateValidation(): Validation {
protected static function makeCreateValidation(User $owner): Validation {
$validation = static::makeValidation();
$individual_name = static::getIndividualName();
$table_name = static::getCollectiveName();

$current_user = auth()->user();
$user_id = $current_user->id;
$user_id = $owner->id;

$validation->setRule("$individual_name.code", "code", [
"required",
Expand Down Expand Up @@ -59,13 +59,12 @@ protected static function makeCreateValidation(): Validation {
return $validation;
}

protected static function makeUpdateValidation(int $id): Validation {
protected static function makeUpdateValidation(User $owner, int $resource_id): Validation {
$validation = static::makeValidation();
$individual_name = static::getIndividualName();
$table_name = static::getCollectiveName();

$current_user = auth()->user();
$user_id = $current_user->id;
$user_id = $owner->id;

$validation->setRule("$individual_name.code", "code", [
"required",
Expand All @@ -78,7 +77,7 @@ protected static function makeUpdateValidation(int $id): Validation {
"name->$individual_name.name",
"user_id=$user_id"
]),
"id=$id"
"id=$resource_id"
])."]"
]);
$validation->setRule("$individual_name.name", "name", [
Expand Down

0 comments on commit 7983ec7

Please sign in to comment.