check()) { $components = Component::query(); } else { $components = Component::enabled(); } $components->search(Binput::except(['sort', 'order', 'per_page'])); if ($sortBy = Binput::get('sort')) { $direction = Binput::has('order') && Binput::get('order') == 'desc'; $components->sort($sortBy, $direction); } $components = $components->paginate(Binput::get('per_page', 20)); return $this->paginator($components, Request::instance()); } /** * Get a single component. * * @param \CachetHQ\Cachet\Models\Component $component * * @return \Illuminate\Http\JsonResponse */ public function getComponent(Component $component) { return $this->item($component); } /** * Create a new component. * * @return \Illuminate\Http\JsonResponse */ public function postComponents() { try { $component = dispatch(new AddComponentCommand( Binput::get('name'), Binput::get('description'), Binput::get('status'), Binput::get('link'), Binput::get('order'), Binput::get('group_id'), (bool) Binput::get('enabled', true), Binput::get('meta', null) )); } catch (QueryException $e) { throw new BadRequestHttpException(); } 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 $this->item($component); } /** * Update an existing component. * * @param \CachetHQ\Cachet\Models\Component $component * * @return \Illuminate\Http\JsonResponse */ public function putComponent(Component $component) { try { dispatch(new UpdateComponentCommand( $component, Binput::get('name'), Binput::get('description'), Binput::get('status'), Binput::get('link'), Binput::get('order'), Binput::get('group_id'), (bool) Binput::get('enabled', true), Binput::get('meta', null), (bool) Binput::get('silent', false) )); } catch (QueryException $e) { throw new BadRequestHttpException(); } 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 $this->item($component); } /** * Delete an existing component. * * @param \CachetHQ\Cachet\Models\Component $component * * @return \Illuminate\Http\JsonResponse */ public function deleteComponent(Component $component) { dispatch(new RemoveComponentCommand($component)); return $this->noContent(); } }