MeticPoint API
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Controllers\Api;
|
||||
|
||||
use Input;
|
||||
use Dingo\Api\Routing\ControllerTrait;
|
||||
use Illuminate\Routing\Controller;
|
||||
use CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository;
|
||||
|
||||
class MetricController extends Controller {
|
||||
|
||||
use ControllerTrait;
|
||||
|
||||
protected $metricpoint;
|
||||
|
||||
public function __construct(MetricPointRepository $metricpoint) {
|
||||
$this->metricpoint = $metricpoint;
|
||||
}
|
||||
/**
|
||||
* Get all metric points
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getMetricPoints() {
|
||||
return $this->metricpoint->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single metric point
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return MetricPoint
|
||||
*/
|
||||
public function getMetricPoint($id) {
|
||||
return $this->metricpoint->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new metric point
|
||||
*
|
||||
* @return MetricPoint
|
||||
*/
|
||||
public function postMetricPoints() {
|
||||
return $this->metricpoint->create(Input::all());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\MetricPoint;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
use MetricPoint;
|
||||
|
||||
class EloquentMetricRepository extends EloquentRepository implements MetricRepository {
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(MetricPoint $model) {
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function create(array $array) {
|
||||
$metric = new $this->model($array);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->saveOrFail();
|
||||
return $metric;
|
||||
}
|
||||
|
||||
public function update($id, array $array) {
|
||||
$metric = $this->model->findOrFail($id);
|
||||
$metric->fill($array);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->update($array);
|
||||
return $metric;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\MetricPoint;
|
||||
|
||||
interface MetricPointRepository {
|
||||
|
||||
public function all();
|
||||
|
||||
public function create(array $array);
|
||||
|
||||
public function findOrFail($id);
|
||||
|
||||
public function update($id, array $with);
|
||||
}
|
||||
20
app/CachetHq/Cachet/Transformers/MetricPointTransformer.php
Normal file
20
app/CachetHq/Cachet/Transformers/MetricPointTransformer.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Transformers;
|
||||
|
||||
use MetricPoint;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
class MetricPointTransformer extends TransformerAbstract {
|
||||
|
||||
public function transform(MetricPoint $metricPoint) {
|
||||
return [
|
||||
'id' => (int) $metricPoint->id,
|
||||
'metric_id' => $metricPoint->metric_id,
|
||||
'value' => $metricPoint->value,
|
||||
'created_at' => $metricPoint->created_at->timestamp,
|
||||
'updated_at' => $metricPoint->updated_at->timestamp,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,11 +9,13 @@ Route::api(['version' => 'v1', 'namespace' => 'CachetHQ\Cachet\Controllers\Api']
|
||||
Route::get('incidents/{id}', 'IncidentController@getIncident');
|
||||
Route::get('metrics', 'MetricController@getMetrics');
|
||||
Route::get('metrics/{id}', 'MetricController@getMetric');
|
||||
Route::get('metrics/points/{id}', 'MetricPointController@getMetricPoint');
|
||||
|
||||
Route::group(['protected' => true], function() {
|
||||
Route::post('components', 'ComponentController@postComponents');
|
||||
Route::post('incidents', 'IncidentController@postIncidents');
|
||||
Route::post('metrics', 'MetricController@postMetrics');
|
||||
Route::post('metrics/points', 'MetricPointController@postMetricPoints');
|
||||
|
||||
Route::put('components/{id}', 'ComponentController@putComponent');
|
||||
Route::put('incidents/{id}', 'IncidentController@putIncident');
|
||||
|
||||
Reference in New Issue
Block a user