Component can be created with tags from API
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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',
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user