Skip to content

Commit

Permalink
Apply tag fixes to API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk committed Jun 18, 2018
1 parent e4fba51 commit a09206c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions app/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
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\Tag;
use GrahamCampbell\Binput\Facades\Binput;
Expand Down Expand Up @@ -85,17 +87,16 @@ public function store()
}

if (Binput::has('tags')) {
// The component was added successfully, so now let's deal with the tags.
$tags = preg_split('/ ?, ?/', Binput::get('tags'));

// For every tag, do we need to create it?
$componentTags = array_map(function ($taggable) use ($component) {
return Tag::firstOrCreate([
'name' => $taggable,
])->id;
}, $tags);
$component->tags()->delete();

$component->tags()->sync($componentTags);
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return dispatch(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
dispatch(new ApplyTagCommand($component, $tag));
});
}

return $this->item($component);
Expand Down Expand Up @@ -128,14 +129,16 @@ public function update(Component $component)
}

if (Binput::has('tags')) {
$tags = preg_split('/ ?, ?/', Binput::get('tags'));
$component->tags()->delete();

// For every tag, do we need to create it?
$componentTags = array_map(function ($taggable) use ($component) {
return Tag::firstOrCreate(['name' => $taggable])->id;
}, $tags);

$component->tags()->sync($componentTags);
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return dispatch(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
dispatch(new ApplyTagCommand($component, $tag));
});
}

return $this->item($component);
Expand Down

0 comments on commit a09206c

Please sign in to comment.