Skip to content

Commit

Permalink
[PLA-1564] Scope queries mutation (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Jan 25, 2024
1 parent f9be1dc commit fd936e6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/GraphQL/Mutations/AddTokensMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields;
use Enjin\Platform\Beam\Models\Beam;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Rules\MaxTokenCount;
use Enjin\Platform\Beam\Rules\MaxTokenSupply;
use Enjin\Platform\Beam\Rules\TokensDoNotExistInBeam;
Expand Down Expand Up @@ -86,7 +87,11 @@ protected function rules(array $args = []): array
$beam = Beam::whereCode($args['code'])->first();

return [
'code' => ['filled', 'max:1024', 'exists:beams,code,deleted_at,NULL'],
'code' => [
'filled',
'max:1024',
new BeamExists(),
],
'tokens' => ['bail', 'array', 'min:1', new UniqueTokenIds()],
'tokens.*.attributes' => Rule::forEach(function ($value, $attribute) use ($args) {
if (empty($value)) {
Expand Down
7 changes: 6 additions & 1 deletion src/GraphQL/Mutations/DeleteBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enjin\Platform\Beam\GraphQL\Mutations;

use Closure;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Services\BeamService;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
Expand Down Expand Up @@ -68,7 +69,11 @@ public function resolve(
protected function rules(array $args = []): array
{
return [
'code' => ['filled', 'max:1024', 'exists:beams,code,deleted_at,NULL'],
'code' => [
'filled',
'max:1024',
new BeamExists(),
],
];
}
}
7 changes: 6 additions & 1 deletion src/GraphQL/Mutations/RemoveTokensMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enjin\Platform\Beam\GraphQL\Mutations;

use Closure;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Rules\TokensExistInBeam;
use Enjin\Platform\Beam\Services\BeamService;
use GraphQL\Type\Definition\ResolveInfo;
Expand Down Expand Up @@ -68,7 +69,11 @@ public function resolve(
protected function rules(array $args = []): array
{
return [
'code' => ['filled', 'max:1024', 'exists:beams,code,deleted_at,NULL'],
'code' => [
'filled',
'max:1024',
new BeamExists(),
],
'tokenIds' => [
'array',
'min:1',
Expand Down
7 changes: 6 additions & 1 deletion src/GraphQL/Mutations/UpdateBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields;
use Enjin\Platform\Beam\Models\Beam;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Rules\IsEndDateValid;
use Enjin\Platform\Beam\Rules\IsStartDateValid;
use Enjin\Platform\Beam\Rules\MaxTokenCount;
Expand Down Expand Up @@ -99,7 +100,11 @@ protected function rules(array $args = []): array
$beam = Beam::whereCode($args['code'])->first();

return [
'code' => ['filled', 'max:1024', 'exists:beams,code,deleted_at,NULL'],
'code' => [
'filled',
'max:1024',
new BeamExists(),
],
'name' => ['filled', 'max:255'],
'description' => ['filled', 'max:1024'],
'image' => ['filled', 'url', 'max:1024'],
Expand Down
26 changes: 26 additions & 0 deletions src/Rules/BeamExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Enjin\Platform\Beam\Rules;

use Closure;
use Enjin\Platform\Beam\Models\Beam;
use Illuminate\Contracts\Validation\ValidationRule;

class BeamExists implements ValidationRule
{
public function __construct(protected string $column = 'code')
{
}

/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!Beam::where($this->column, $value)->exists()) {
$fail('validation.exists')->translate();
}
}
}

0 comments on commit fd936e6

Please sign in to comment.