@@ -4,7 +4,6 @@ Route::api(['after' => 'allowed_domains', 'namespace' => 'CachetHQ\Cachet\Http\C
|
||||
// Components
|
||||
Route::get('components', 'ComponentController@getComponents');
|
||||
Route::get('components/{id}', 'ComponentController@getComponent');
|
||||
Route::get('components/{id}/incidents', 'ComponentController@getComponentIncidents');
|
||||
|
||||
// Incidents
|
||||
Route::get('incidents', 'IncidentController@getIncidents');
|
||||
|
||||
@@ -53,18 +53,6 @@ class ComponentController extends Controller
|
||||
return $this->component->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a component with incidents.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function getComponentIncidents($id)
|
||||
{
|
||||
return $this->component->with($id, ['incidents']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new component.
|
||||
*
|
||||
@@ -94,6 +82,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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,16 @@ use League\Fractal\TransformerAbstract;
|
||||
|
||||
class ComponentTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* List of resources possible to include.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [
|
||||
'incidents',
|
||||
'tags',
|
||||
];
|
||||
|
||||
/**
|
||||
* Transform a component model into an array.
|
||||
*
|
||||
@@ -27,4 +37,28 @@ class ComponentTransformer extends TransformerAbstract
|
||||
'updated_at' => $component->updated_at->timestamp,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Include component incidents.
|
||||
*
|
||||
* @return League\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeIncidents(Component $component)
|
||||
{
|
||||
$incidents = $component->incidents;
|
||||
|
||||
return $this->collection($incidents, new IncidentTransformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Include components tags.
|
||||
*
|
||||
* @return League\Fractal\Resource\Collection
|
||||
*/
|
||||
public function includeTags(Component $component)
|
||||
{
|
||||
$incidents = $component->tags;
|
||||
|
||||
return $this->collection($incidents, new TagTransformer());
|
||||
}
|
||||
}
|
||||
|
||||
27
src/Transformers/TagTransformer.php
Normal file
27
src/Transformers/TagTransformer.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Transformers;
|
||||
|
||||
use CachetHQ\Cachet\Models\Tag;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class TagTransformer extends TransformerAbstract
|
||||
{
|
||||
/**
|
||||
* Transform a tag model into an array.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Tag $tag
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Tag $tag)
|
||||
{
|
||||
return [
|
||||
'id' => (int) $tag->id,
|
||||
'name' => $tag->name,
|
||||
'description' => $tag->slug,
|
||||
'created_at' => $tag->created_at->timestamp,
|
||||
'updated_at' => $tag->updated_at->timestamp,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user