diff --git a/src/Http/Controllers/Api/ComponentController.php b/src/Http/Controllers/Api/ComponentController.php index 13c1fc6e..d192142c 100644 --- a/src/Http/Controllers/Api/ComponentController.php +++ b/src/Http/Controllers/Api/ComponentController.php @@ -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. * diff --git a/src/Repositories/Component/ComponentRepository.php b/src/Repositories/Component/ComponentRepository.php index fdd3fcc7..1b80f9fa 100644 --- a/src/Repositories/Component/ComponentRepository.php +++ b/src/Repositories/Component/ComponentRepository.php @@ -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. * diff --git a/src/Repositories/Component/EloquentComponentRepository.php b/src/Repositories/Component/EloquentComponentRepository.php index 7420fff6..f3851d78 100644 --- a/src/Repositories/Component/EloquentComponentRepository.php +++ b/src/Repositories/Component/EloquentComponentRepository.php @@ -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; + } }