Fix how tags are created. Fixes #3004

This commit is contained in:
James Brooks
2018-06-16 22:32:13 +01:00
parent 90568b3d67
commit b22f7abd28
15 changed files with 601 additions and 36 deletions

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Model;
/**
* This is the apply tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class ApplyTagCommand
{
/**
* The model to apply the tag to.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
/**
* The tag to apply.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new apply tag command instance.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Model $model, Tag $tag)
{
$this->model = $model;
$this->tag = $tag;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
/**
* This is the create tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class CreateTagCommand
{
/**
* The tag name.
*
* @var string
*/
public $name;
/**
* The tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new create tag command instance.
*
* @param string $name
* @param string|null $slug
*
* @return void
*/
public function __construct($name, $slug = null)
{
$this->name = $name;
$this->slug = $slug;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the delete tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class DeleteTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new delete tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the update tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class UpdateTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* The new tag name.
*
* @var string|null
*/
public $name;
/**
* The new tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new update tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
* @param string|null $name
* @param string|null $slug
*
* @return void
*/
public function __construct(Tag $tag, $name, $slug)
{
$this->tag = $tag;
$this->name = $name;
$this->slug = $slug;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Models\Taggable;
/**
* This is the apply tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class ApplyTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand $command
*
* @return void
*/
public function handle(ApplyTagCommand $command)
{
Taggable::firstOrCreate([
'tag_id' => $command->tag->id,
'taggable_id' => $command->model->id,
'taggable_type' => $command->model->getTable(),
]);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class CreateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(CreateTagCommand $command)
{
return Tag::firstOrCreate([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand;
/**
* This is the delete tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class DeleteTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(DeleteTagCommand $command)
{
$command->tag->delete();
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class UpdateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand $command
*
* @return void
*/
public function handle(UpdateTagCommand $command)
{
return $command->tag->update([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@@ -14,8 +14,9 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
@@ -85,17 +86,16 @@ class ComponentController extends AbstractApiController
} }
if (Binput::has('tags')) { if (Binput::has('tags')) {
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags. // The component was added successfully, so now let's deal with the tags.
$tags = preg_split('/ ?, ?/', Binput::get('tags')); Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
// For every tag, do we need to create it? })->map(function ($tag) {
$componentTags = array_map(function ($taggable) use ($component) { return dispatch(new CreateTagCommand($tag));
return Tag::firstOrCreate([ })->each(function ($tag) use ($component) {
'name' => $taggable, dispatch(new ApplyTagCommand($component, $tag));
])->id; });
}, $tags);
$component->tags()->sync($componentTags);
} }
return $this->item($component); return $this->item($component);
@@ -128,14 +128,16 @@ class ComponentController extends AbstractApiController
} }
if (Binput::has('tags')) { if (Binput::has('tags')) {
$tags = preg_split('/ ?, ?/', Binput::get('tags')); $component->tags()->delete();
// For every tag, do we need to create it? // The component was added successfully, so now let's deal with the tags.
$componentTags = array_map(function ($taggable) use ($component) { Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return Tag::firstOrCreate(['name' => $taggable])->id; return trim($tag);
}, $tags); })->map(function ($tag) {
return dispatch(new CreateTagCommand($tag));
$component->tags()->sync($componentTags); })->each(function ($tag) use ($component) {
dispatch(new ApplyTagCommand($component, $tag));
});
} }
return $this->item($component); return $this->item($component);

View File

@@ -15,11 +15,13 @@ use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Tag;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\View;
/** /**
@@ -132,15 +134,16 @@ class ComponentController extends Controller
->withErrors($e->getMessageBag()); ->withErrors($e->getMessageBag());
} }
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags. // The component was added successfully, so now let's deal with the tags.
$tags = preg_split('/ ?, ?/', $tags); Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
// For every tag, do we need to create it? })->map(function ($tag) {
$componentTags = array_map(function ($taggable) { return dispatch(new CreateTagCommand($tag));
return Tag::firstOrCreate(['name' => $taggable])->id; })->each(function ($tag) use ($component) {
}, $tags); dispatch(new ApplyTagCommand($component, $tag));
});
$component->tags()->sync($componentTags);
return cachet_redirect('dashboard.components.edit', [$component->id]) return cachet_redirect('dashboard.components.edit', [$component->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success'))); ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
@@ -187,14 +190,13 @@ class ComponentController extends Controller
} }
// The component was added successfully, so now let's deal with the tags. // The component was added successfully, so now let's deal with the tags.
$tags = preg_split('/ ?, ?/', $tags); Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
// For every tag, do we need to create it? })->map(function ($tag) {
$componentTags = array_map(function ($taggable) { return dispatch(new CreateTagCommand($tag));
return Tag::firstOrCreate(['name' => $taggable])->id; })->each(function ($tag) use ($component) {
}, $tags); dispatch(new ApplyTagCommand($component, $tag));
});
$component->tags()->sync($componentTags);
return cachet_redirect('dashboard.components') return cachet_redirect('dashboard.components')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success'))); ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));

View File

@@ -53,7 +53,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label>{{ trans('forms.components.tags') }}</label> <label>{{ trans('forms.components.tags') }}</label>
<input name="component[tags]" class="form-control" value="{{ $component->tagsList }}" placeholder="{{ trans('forms.components.tags') }}"> <input name="component[tags]" class="form-control" value="{{ $component->tags->implode(', ') }}" placeholder="{{ trans('forms.components.tags') }}">
<span class="help-block">{{ trans('forms.components.tags-help') }}</span> <span class="help-block">{{ trans('forms.components.tags-help') }}</span>
</div> </div>
<div class="checkbox"> <div class="checkbox">

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\ApplyTagCommandHandler;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the apply tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class ApplyTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'model' => new Component(),
'tag' => new Tag(),
];
$object = new ApplyTagCommand(
$params['model'],
$params['tag']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return ApplyTagCommandHandler::class;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\CreateTagCommandHandler;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the create tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class CreateTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'name' => 'Test',
'slug' => 'test',
];
$object = new CreateTagCommand(
$params['name'],
$params['slug']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return CreateTagCommandHandler::class;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\DeleteTagCommandHandler;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the delete tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class DeleteTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'tag' => new Tag(),
];
$object = new DeleteTagCommand(
$params['tag']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return DeleteTagCommandHandler::class;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\UpdateTagCommandHandler;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the update tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class UpdateTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'tag' => new Tag(),
'name' => 'Test',
'slug' => 'test',
];
$object = new UpdateTagCommand(
$params['tag'],
$params['name'],
$params['slug']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return UpdateTagCommandHandler::class;
}
}