Update components through API
This commit is contained in:
@@ -94,6 +94,33 @@ class ComponentController extends Controller
|
||||
return $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing component.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function putComponent($id)
|
||||
{
|
||||
$component = $this->component->update($id, Binput::except('tags'));
|
||||
|
||||
if (Binput::has('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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing component.
|
||||
*
|
||||
|
||||
@@ -21,6 +21,16 @@ interface ComponentRepository
|
||||
*/
|
||||
public function create($userId, array $data);
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data);
|
||||
|
||||
/**
|
||||
* Finds a model by id.
|
||||
*
|
||||
|
||||
@@ -45,4 +45,23 @@ class EloquentComponentRepository extends EloquentRepository implements Componen
|
||||
|
||||
return $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model by id.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$component = $this->model->findOrFail($id);
|
||||
$component->fill($data);
|
||||
$this->validate($component);
|
||||
|
||||
$component->update($data);
|
||||
|
||||
return $component;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user