Add and remove metrics and metric points commands
This commit is contained in:
committed by
James Brooks
parent
64ff4d73c2
commit
9581c5a394
@@ -11,14 +11,19 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
|
||||
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use Exception;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
class MetricController extends AbstractApiController
|
||||
{
|
||||
use DispatchesJobs;
|
||||
|
||||
/**
|
||||
* Get all metrics.
|
||||
*
|
||||
@@ -65,7 +70,15 @@ class MetricController extends AbstractApiController
|
||||
public function postMetrics()
|
||||
{
|
||||
try {
|
||||
$metric = Metric::create(Binput::all());
|
||||
$metric = $this->dispatch(new AddMetricCommand(
|
||||
Binput::get('name'),
|
||||
Binput::get('suffix'),
|
||||
Binput::get('description'),
|
||||
Binput::get('default_value'),
|
||||
Binput::get('calc_type', 0),
|
||||
Binput::get('display_chart'),
|
||||
Binput::get('places')
|
||||
));
|
||||
} catch (Exception $e) {
|
||||
throw new BadRequestHttpException();
|
||||
}
|
||||
@@ -100,7 +113,7 @@ class MetricController extends AbstractApiController
|
||||
*/
|
||||
public function deleteMetric(Metric $metric)
|
||||
{
|
||||
$metric->delete();
|
||||
$this->dispatch(new RemoveMetricCommand($metric));
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
|
||||
@@ -11,14 +11,20 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
|
||||
use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Models\MetricPoint;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
class MetricPointController extends AbstractApiController
|
||||
{
|
||||
use DispatchesJobs;
|
||||
|
||||
/**
|
||||
* Get a single metric point.
|
||||
*
|
||||
@@ -41,16 +47,8 @@ class MetricPointController extends AbstractApiController
|
||||
*/
|
||||
public function postMetricPoints(Metric $metric)
|
||||
{
|
||||
$metricPointData = Binput::all();
|
||||
$metricPointData['metric_id'] = $metric->id;
|
||||
|
||||
if ($timestamp = array_pull($metricPointData, 'timestamp')) {
|
||||
$pointTimestamp = Carbon::createFromFormat('U', $timestamp);
|
||||
$metricPointData['created_at'] = $pointTimestamp->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
try {
|
||||
$metricPoint = MetricPoint::create($metricPointData);
|
||||
$metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp')));
|
||||
} catch (Exception $e) {
|
||||
throw new BadRequestHttpException();
|
||||
}
|
||||
@@ -91,7 +89,7 @@ class MetricPointController extends AbstractApiController
|
||||
*/
|
||||
public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint)
|
||||
{
|
||||
$metricPoint->delete();
|
||||
$this->dispatch(new RemoveMetricPointCommand($metricPoint));
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
|
||||
@@ -12,15 +12,20 @@
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
|
||||
|
||||
use AltThree\Validator\ValidationException;
|
||||
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
|
||||
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Models\MetricPoint;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\Support\Facades\View;
|
||||
|
||||
class MetricController extends Controller
|
||||
{
|
||||
use DispatchesJobs;
|
||||
|
||||
/**
|
||||
* Shows the metrics view.
|
||||
*
|
||||
@@ -66,7 +71,7 @@ class MetricController extends Controller
|
||||
public function createMetricAction()
|
||||
{
|
||||
try {
|
||||
Metric::create(Binput::get('metric'));
|
||||
$this->dispatchFromArray(AddMetricCommand::class, Binput::get('metric'));
|
||||
} catch (ValidationException $e) {
|
||||
return Redirect::route('dashboard.metrics.add')
|
||||
->withInput(Binput::all())
|
||||
@@ -98,7 +103,7 @@ class MetricController extends Controller
|
||||
*/
|
||||
public function deleteMetricAction(Metric $metric)
|
||||
{
|
||||
$metric->delete();
|
||||
$this->dispatch(new RemoveMetricCommand($metric));
|
||||
|
||||
return Redirect::route('dashboard.metrics.index');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user