-
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 #1442 from bakaphp/KA-207
feat: tag
- Loading branch information
Showing
20 changed files
with
836 additions
and
19 deletions.
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
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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Social\Mutations\Tags; | ||
|
||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Social\Follows\Actions\FollowAction; | ||
use Kanvas\Social\Follows\Actions\UnFollowAction; | ||
use Kanvas\Social\Follows\Repositories\UsersFollowsRepository; | ||
use Kanvas\Social\Tags\Actions\CreateTagAction; | ||
use Kanvas\Social\Tags\DataTransferObjects\Tag as TagData; | ||
use Kanvas\Social\Tags\Models\Tag; | ||
use Kanvas\SystemModules\DataTransferObject\SystemModuleEntityInput; | ||
use Kanvas\SystemModules\Models\SystemModules; | ||
use Kanvas\SystemModules\Repositories\SystemModulesRepository; | ||
|
||
class TagsManagement | ||
{ | ||
public function create(mixed $root, array $request): Tag | ||
{ | ||
$request['input']['app'] = app(Apps::class); | ||
$request['input']['user'] = auth()->user(); | ||
$request['input']['company'] = auth()->user()->getCurrentCompany(); | ||
|
||
$dto = TagData::from($request['input']); | ||
|
||
return (new CreateTagAction($dto))->execute(); | ||
} | ||
|
||
public function update(mixed $root, array $request): Tag | ||
{ | ||
$app = app(Apps::class); | ||
|
||
$tag = Tag::when(! auth()->user()->isAdmin(), function ($query) { | ||
return $query->where('users_id', auth()->user()->getId()); | ||
})->where('id', $request['id']) | ||
->fromApp($app) | ||
->firstOrFail(); | ||
|
||
$tag->update($request['input']); | ||
|
||
return $tag; | ||
} | ||
|
||
public function delete(mixed $root, array $request): bool | ||
{ | ||
$app = app(Apps::class); | ||
$tag = Tag::when(! auth()->user()->isAdmin(), function ($query) { | ||
return $query->where('users_id', auth()->user()->getId()); | ||
}) | ||
->where('id', $request['id']) | ||
->fromApp($app) | ||
->firstOrFail(); | ||
|
||
|
||
return $tag->delete(); | ||
} | ||
|
||
public function follow(mixed $root, array $request): bool | ||
{ | ||
$app = app(Apps::class); | ||
$tag = Tag::getById($request['id'], $app); | ||
$isFollowing = UsersFollowsRepository::isFollowing(auth()->user(), $tag); | ||
if ($isFollowing) { | ||
return (new UnFollowAction(auth()->user(), $tag))->execute(); | ||
} else { | ||
return (bool)(new FollowAction(auth()->user(), $tag))->execute(); | ||
} | ||
} | ||
|
||
public function attachTagToEntity(mixed $root, array $request): bool | ||
{ | ||
$app = app(Apps::class); | ||
$tag = Tag::getById($request['input']['tag_id'], $app); | ||
$user = auth()->user(); | ||
|
||
$systemModule = SystemModules::getByUuid($request['input']['system_module_uuid'], $app); | ||
|
||
//$entity = $systemModule->model_name::getById((int)$request['input']['entity_id'], $app); | ||
$entity = SystemModulesRepository::getEntityFromInput( | ||
new SystemModuleEntityInput( | ||
$systemModule->name, | ||
$systemModule->uuid, | ||
$request['input']['entity_id'] | ||
), | ||
$user, | ||
useCompanyReference: false | ||
); | ||
|
||
$tag->entities()->attach($entity->getId(), [ | ||
'entity_namespace' => $systemModule->model_name, | ||
'apps_id' => $tag->apps_id, | ||
'companies_id' => $user->getCurrentCompany()->getId(), | ||
'users_id' => $user->getId(), | ||
'taggable_type' => $systemModule->model_name, | ||
]); | ||
|
||
return true; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\GraphQL\Social\Queries\Tags; | ||
|
||
use Baka\Enums\StateEnums; | ||
use Kanvas\Apps\Models\Apps; | ||
use Kanvas\Social\Tags\Models\Tag; | ||
use Kanvas\SystemModules\Repositories\SystemModulesRepository; | ||
|
||
class TagsQueries | ||
{ | ||
public function getTagsBuilder(mixed $root, array $args): mixed | ||
{ | ||
$app = app(Apps::class); | ||
$systemModule = SystemModulesRepository::getByModelName($root::class, $app); | ||
|
||
return Tag::whereHas('taggables', function ($query) use ($root, $systemModule) { | ||
$query->where('entity_id', $root->getKey()); | ||
$query->where('entity_namespace', $systemModule->model_name); | ||
$query->where('apps_id', $systemModule->apps_id); | ||
})->where('is_deleted', StateEnums::NO->getValue()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
database/migrations/Social/2024_06_05_154618_tags_entities.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('tags_entities', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('tags_id')->index(); | ||
$table->integer('entity_id')->index(); | ||
$table->string('entity_namespace')->index(); | ||
$table->integer('companies_id')->index(); | ||
$table->integer('apps_id')->index(); | ||
$table->integer('users_id')->index(); | ||
$table->boolean('is_deleted')->default(0); | ||
$table->timestamp('created_at')->index()->useCurrent(); | ||
$table->datetime('updated_at')->nullable()->index(); | ||
$table->index(['tags_id', 'entity_id', 'is_deleted'], 'tags_entities_index_tag'); | ||
$table->index(['tags_id', 'entity_id', 'apps_id', 'is_deleted'], 'tags_entities_app_index'); | ||
$table->index(['tags_id', 'entity_id', 'companies_id', 'apps_id', 'is_deleted'], 'tags_entities_index'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('tags_entities'); | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
database/migrations/Social/2024_06_05_172750_taggable_type.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,27 @@ | ||
<?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('tags_entities', function (Blueprint $table) { | ||
$table->string('taggable_type')->nullable()->after('entity_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('tags_entities', function (Blueprint $table) { | ||
$table->dropColumn('taggable_type'); | ||
}); | ||
} | ||
}; |
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
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
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
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
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
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
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
Oops, something went wrong.