-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1727 from bakaphp/attribute-type
[1.x] Attribute type inputs
- Loading branch information
Showing
12 changed files
with
550 additions
and
1 deletion.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/GraphQL/Inventory/Mutations/Attributes/AttributeTypeMutation.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Inventory\Mutations\Attributes; | ||
|
||
use Kanvas\Inventory\Attributes\Actions\CreateAttributeType; | ||
use Kanvas\Inventory\Attributes\Actions\UpdateAttributeType; | ||
use Kanvas\Inventory\Attributes\DataTransferObject\AttributesType; | ||
use Kanvas\Inventory\Attributes\Models\Attributes as AttributeModel; | ||
use Kanvas\Inventory\Attributes\Models\AttributesTypes as AttributesTypesModel; | ||
use Kanvas\Inventory\Attributes\Repositories\AttributesTypesRepository; | ||
|
||
class AttributeTypeMutation | ||
{ | ||
/** | ||
* create. | ||
* | ||
* @param mixed $root | ||
* @param array $req | ||
* | ||
* @return AttributeModel | ||
*/ | ||
public function create(mixed $root, array $req): AttributesTypesModel | ||
{ | ||
$dto = AttributesType::viaRequest($req['input'], auth()->user()); | ||
$action = new CreateAttributeType($dto, auth()->user()); | ||
$attributeTypeModel = $action->execute(); | ||
|
||
return $attributeTypeModel; | ||
} | ||
|
||
/** | ||
* update. | ||
* | ||
* @param mixed $root | ||
* @param array $req | ||
* | ||
* @return AttributeModel | ||
*/ | ||
public function update(mixed $root, array $req): AttributesTypesModel | ||
{ | ||
$attribute = AttributesTypesRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); | ||
$dto = AttributesType::viaRequest($req['input'], auth()->user()); | ||
(new UpdateAttributeType($attribute, $dto, auth()->user()))->execute(); | ||
|
||
return $attribute; | ||
} | ||
|
||
/** | ||
* delete. | ||
* | ||
* @param mixed $root | ||
* @param array $req | ||
* | ||
* @return bool | ||
*/ | ||
public function delete(mixed $root, array $req): bool | ||
{ | ||
$attributeType = AttributesTypesRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); | ||
return $attributeType->delete(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
database/migrations/Inventory/2024_07_22_190318_attribute-type-migration.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,37 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('attributes_types_input', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('slug')->nullable(true); | ||
$table->bigInteger('apps_id')->unsigned(); | ||
$table->bigInteger('companies_id')->unsigned(); | ||
$table->bigInteger('users_id')->unsigned(); | ||
$table->timestamp('created_at')->useCurrent(); | ||
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate(); | ||
$table->boolean('is_deleted')->default(0); | ||
$table->index('apps_id'); | ||
$table->index('companies_id'); | ||
$table->index('is_deleted'); | ||
$table->index(['companies_id', 'apps_id'], 'companies_id_apps_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
database/migrations/Inventory/2024_07_22_191133_attribute-type-update-migration.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,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class () extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('attributes', function (Blueprint $table) { | ||
$table->bigInteger('attributes_type_id')->unsigned()->after('slug')->nullable(); | ||
$table->foreign('attributes_type_id')->references('id')->on('attributes_types_input'); | ||
$table->index('attributes_type_id'); | ||
}); | ||
|
||
Schema::table('attributes_types_input', function (Blueprint $table) { | ||
$table->boolean('is_default')->default(false); | ||
$table->index(['is_default', 'companies_id', 'apps_id'], 'is_default_companies_id_app'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |
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,64 @@ | ||
input AttributesTypeInput{ | ||
name: String! | ||
slug: String | ||
is_default: Boolean | ||
} | ||
|
||
input AttributeTypeUpdateInput { | ||
name: String | ||
slug: String | ||
is_default: Boolean | ||
} | ||
|
||
type AttributesType { | ||
id: ID! | ||
name: String | ||
slug: String | ||
is_default: Boolean | ||
created_at: String | ||
updated_at: String | ||
} | ||
|
||
|
||
extend type Mutation @guard { | ||
createAttributeType(input: AttributesTypeInput!): AttributesType! | ||
@field( | ||
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeTypeMutation@create" | ||
) | ||
updateAttributeType(id: ID!, input: AttributeTypeUpdateInput!): AttributesType! | ||
@field( | ||
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeTypeMutation@update" | ||
) | ||
deleteAttributeType(id: ID!): Boolean! | ||
@field( | ||
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeTypeMutation@delete" | ||
) | ||
|
||
} | ||
extend type Query @guard { | ||
attributestypes( | ||
search: String @search | ||
where: _ | ||
@whereConditions( | ||
columns: [ | ||
"uuid", | ||
"id", | ||
"name" | ||
] | ||
) | ||
orderBy: _ | ||
@orderBy( | ||
columns: [ | ||
"id" | ||
"created_at" | ||
"updated_at" | ||
"name" | ||
] | ||
) | ||
): [AttributesType!]! | ||
@paginate( | ||
defaultCount: 25 | ||
model: "Kanvas\\Inventory\\Attributes\\Models\\AttributesTypes" | ||
scopes: ["fromApp", "fromCompany", "notDeleted"] | ||
) | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Domains/Inventory/Attributes/Actions/CreateAttributeType.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Attributes\Actions; | ||
|
||
use Baka\Users\Contracts\UserInterface; | ||
use Kanvas\Companies\Repositories\CompaniesRepository; | ||
use Kanvas\Inventory\Attributes\DataTransferObject\AttributesType; | ||
use Kanvas\Inventory\Attributes\Models\AttributesTypes; | ||
|
||
class CreateAttributeType | ||
{ | ||
public function __construct( | ||
protected AttributesType $dto, | ||
protected UserInterface $user | ||
) { | ||
} | ||
|
||
/** | ||
* execute. | ||
* | ||
* @return AttributesTypes | ||
*/ | ||
public function execute(): AttributesTypes | ||
{ | ||
CompaniesRepository::userAssociatedToCompany( | ||
$this->dto->company, | ||
$this->user | ||
); | ||
|
||
return AttributesTypes::firstOrCreate([ | ||
'name' => $this->dto->name, | ||
'companies_id' => $this->dto->company->getId(), | ||
'apps_id' => $this->dto->app->getId(), | ||
], [ | ||
'users_id' => $this->user->getId(), | ||
'slug' => $this->dto->slug, | ||
'is_default' => $this->dto->isDefault, | ||
]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Domains/Inventory/Attributes/Actions/UpdateAttributeType.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Attributes\Actions; | ||
|
||
use Baka\Users\Contracts\UserInterface; | ||
use Kanvas\Companies\Repositories\CompaniesRepository; | ||
use Kanvas\Inventory\Attributes\DataTransferObject\AttributesType as AttributeTypeDto; | ||
use Kanvas\Inventory\Attributes\Models\Attributes; | ||
use Kanvas\Inventory\Attributes\Models\AttributesTypes; | ||
|
||
class UpdateAttributeType | ||
{ | ||
public function __construct( | ||
protected AttributesTypes $attributeType, | ||
protected AttributeTypeDto $dto, | ||
protected UserInterface $user | ||
) { | ||
} | ||
|
||
/** | ||
* execute. | ||
* | ||
* @return Attributes | ||
*/ | ||
public function execute(): AttributesTypes | ||
{ | ||
CompaniesRepository::userAssociatedToCompany( | ||
$this->dto->company, | ||
$this->user | ||
); | ||
|
||
$this->attributeType->update([ | ||
'name' => $this->dto->name, | ||
'slug' => $this->dto->slug, | ||
'is_default' => $this->dto->isDefault, | ||
]); | ||
|
||
return $this->attributeType; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Domains/Inventory/Attributes/DataTransferObject/AttributesType.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Attributes\DataTransferObject; | ||
|
||
use Baka\Contracts\AppInterface; | ||
use Baka\Contracts\CompanyInterface; | ||
use Baka\Support\Str; | ||
use Baka\Users\Contracts\UserInterface; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Companies\Models\Companies; | ||
use Spatie\LaravelData\Data; | ||
|
||
class AttributesType extends Data | ||
{ | ||
public function __construct( | ||
public CompanyInterface $company, | ||
public AppInterface $app, | ||
public string $name, | ||
public string $slug, | ||
public bool $isDefault = false, | ||
) { | ||
} | ||
|
||
public static function viaRequest(array $request, UserInterface $user): self | ||
{ | ||
return new self( | ||
isset($request['company_id']) ? Companies::getById($request['company_id']) : $user->getCurrentCompany(), | ||
app(Apps::class), | ||
$request['name'], | ||
$request['slug'] ?? Str::slug($request['name']), | ||
$request['is_default'] ?? false, | ||
); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/Domains/Inventory/Attributes/Models/AttributesTypes.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Attributes\Models; | ||
|
||
use Baka\Traits\SlugTrait; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Inventory\Attributes\Models\Attributes as ModelsAttributes; | ||
use Kanvas\Inventory\Models\BaseModel; | ||
|
||
/** | ||
* Class Attributes. | ||
* | ||
* @property int $id | ||
* @property int $apps_id | ||
* @property int $companies_id | ||
* @property string uuid | ||
* @property string $name | ||
*/ | ||
class AttributesTypes extends BaseModel | ||
{ | ||
use SlugTrait; | ||
|
||
public $table = 'attributes_types_input'; | ||
public $guarded = []; | ||
|
||
/** | ||
* apps. | ||
*/ | ||
public function apps(): BelongsTo | ||
{ | ||
return $this->belongsTo(Apps::class, 'apps_id'); | ||
} | ||
|
||
public function attributes(): HasMany | ||
{ | ||
return $this->hasMany(ModelsAttributes::class, 'attributes_id'); | ||
} | ||
} |
Oops, something went wrong.