Component can be created with tags from API

This commit is contained in:
Joseph Cohen
2015-02-18 03:21:30 -06:00
parent e30c95fad4
commit e3bb4a15de
2 changed files with 22 additions and 2 deletions

View File

@@ -3,6 +3,7 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
use CachetHQ\Cachet\Models\Tag;
use Dingo\Api\Routing\ControllerTrait;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Routing\Controller;
@@ -71,7 +72,26 @@ class ComponentController extends Controller
*/
public function postComponents()
{
return $this->component->create($this->auth->user()->id, Binput::all());
$component = $this->component->create(
$this->auth->user()->id,
Binput::except('tags')
);
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()->sync($componentTags);
}
return $component;
}
/**

View File

@@ -34,7 +34,7 @@ class Component extends Model implements TransformableInterface
protected $rules = [
'user_id' => 'integer|required',
'name' => 'required',
'status' => 'integer',
'status' => 'integer|required',
'link' => 'url',
];