Skip to content

Commit

Permalink
Merge pull request #1727 from bakaphp/attribute-type
Browse files Browse the repository at this point in the history
[1.x] Attribute type inputs
  • Loading branch information
arfenis authored Jul 25, 2024
2 parents 1f86f75 + 7d98fcd commit 9968549
Show file tree
Hide file tree
Showing 12 changed files with 550 additions and 1 deletion.
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();
}
}
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
{
//
}
};
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
{
//
}
};
64 changes: 64 additions & 0 deletions graphql/schemas/Inventory/attributestype.graphql
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 src/Domains/Inventory/Attributes/Actions/CreateAttributeType.php
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 src/Domains/Inventory/Attributes/Actions/UpdateAttributeType.php
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;
}
}
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,
);
}
}
10 changes: 10 additions & 0 deletions src/Domains/Inventory/Attributes/Models/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Baka\Support\Str;
use Baka\Traits\DatabaseSearchableTrait;
use Baka\Traits\SlugTrait;
use Baka\Traits\UuidTrait;
use Dyrynda\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Casts\Attribute;
Expand All @@ -30,6 +31,7 @@
class Attributes extends BaseModel
{
use UuidTrait;
use SlugTrait;
use CascadeSoftDeletes;
use DatabaseSearchableTrait;

Expand All @@ -45,6 +47,14 @@ public function apps(): BelongsTo
return $this->belongsTo(Apps::class, 'apps_id');
}

/**
* apps.
*/
public function attributeType(): BelongsTo
{
return $this->belongsTo(AttributesTypes::class, 'attributes_type_id');
}

public function variantAttributes(): HasMany
{
return $this->hasMany(VariantsAttributes::class, 'attributes_id');
Expand Down
42 changes: 42 additions & 0 deletions src/Domains/Inventory/Attributes/Models/AttributesTypes.php
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');
}
}
Loading

0 comments on commit 9968549

Please sign in to comment.