Merge pull request #40 from jbrooksuk/metric-api
Create and update a metric.
This commit is contained in:
@@ -155,4 +155,47 @@
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new metric
|
||||
*
|
||||
* @return Metric
|
||||
*/
|
||||
public function postMetrics() {
|
||||
$metric = new Metric(Input::all());
|
||||
return $this->_saveMetric($metric);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing metric
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Metric
|
||||
*/
|
||||
public function putMetric($id) {
|
||||
$metric = $this->getMetric($id);
|
||||
$metric->fill(Input::all());
|
||||
return $this->_saveMetric($metric);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for saving a metric, and returning appropriate error codes
|
||||
*
|
||||
* @param Metric $metric
|
||||
*
|
||||
* @return Metric
|
||||
*/
|
||||
private function _saveMetric($metric) {
|
||||
if ($metric->isValid()) {
|
||||
try {
|
||||
$metric->saveOrFail();
|
||||
return $metric;
|
||||
} catch (Exception $e) {
|
||||
App::abort(500, $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
App::abort(404, $metric->getErrors()->first());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,32 @@
|
||||
<?php
|
||||
|
||||
class Metric extends Eloquent {
|
||||
use \Watson\Validating\ValidatingTrait;
|
||||
|
||||
class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface {
|
||||
use ValidatingTrait;
|
||||
|
||||
protected $rules = [
|
||||
'name' => 'required',
|
||||
'suffix' => 'required',
|
||||
'display_chart' => 'boolean',
|
||||
];
|
||||
|
||||
protected $fillable = ['name', 'suffix', 'description', 'display_chart'];
|
||||
|
||||
/**
|
||||
* Determines whether a chart should be shown.
|
||||
* @return bool
|
||||
*/
|
||||
public function getShouldDisplayAttribute() {
|
||||
return $this->display_chart === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transformer instance.
|
||||
*
|
||||
* @return ComponentTransformer
|
||||
*/
|
||||
public function getTransformer() {
|
||||
return new MetricTransformer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,10 @@
|
||||
Route::group(['protected' => true], function() {
|
||||
Route::post('components', 'ApiController@postComponents');
|
||||
Route::post('incidents', 'ApiController@postIncidents');
|
||||
Route::post('metrics', 'ApiController@postMetrics');
|
||||
|
||||
Route::put('incidents/{id}', 'ApiController@putIncident');
|
||||
Route::put('metrics/{id}', 'ApiController@putMetric');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
15
app/transformers/MetricTransformer.php
Normal file
15
app/transformers/MetricTransformer.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class MetricTransformer extends \League\Fractal\TransformerAbstract {
|
||||
public function transform(Metric $metric) {
|
||||
return [
|
||||
'id' => (int) $component->id,
|
||||
'name' => $component->name,
|
||||
'description' => $component->description,
|
||||
'suffix' => $component->suffix,
|
||||
'display' => $component->shouldDisplay,
|
||||
'created_at' => $component->created_at->timestamp,
|
||||
'updated_at' => $component->updated_at->timestamp,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user