Merge pull request #950 from cachethq/update-commands

Update commands
This commit is contained in:
James Brooks
2015-10-01 20:14:02 +01:00
32 changed files with 1109 additions and 86 deletions

View File

@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use Exception;
@@ -101,7 +102,15 @@ class ComponentController extends AbstractApiController
public function putComponent(Component $component)
{
try {
$component->update(Binput::except('tags'));
$this->dispatch(new UpdateComponentCommand(
$component,
Binput::get('name'),
Binput::get('description'),
Binput::get('status'),
Binput::get('link'),
Binput::get('order'),
Binput::get('group_id')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}

View File

@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
@@ -81,7 +82,11 @@ class ComponentGroupController extends AbstractApiController
$groupData = array_filter(Binput::only(['name', 'order']));
try {
$group->update($groupData);
$group = $this->dispatch(new UpdateComponentGroupCommand(
$group,
Binput::get('name'),
Binput::get('order', 0)
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}

View File

@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Models\Incident;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
@@ -71,7 +72,8 @@ class IncidentController extends AbstractApiController
Binput::get('visible', true),
Binput::get('component_id'),
Binput::get('component_status'),
Binput::get('notify', true)
Binput::get('notify', true),
Binput::get('created_at')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
@@ -89,17 +91,18 @@ class IncidentController extends AbstractApiController
*/
public function putIncident(Incident $incident)
{
$incidentData = array_filter(Binput::only([
'name',
'message',
'status',
'component_id',
'notify',
'visible',
]));
try {
$incident->update($incidentData);
$incident = $this->dispatch(new UpdateIncidentCommand(
$incident,
Binput::get('name'),
Binput::get('status'),
Binput::get('message'),
Binput::get('visible', true),
Binput::get('component_id'),
Binput::get('component_status'),
Binput::get('notify', true),
Binput::get('created_at')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}

View File

@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
@@ -96,7 +97,16 @@ class MetricController extends AbstractApiController
public function putMetric(Metric $metric)
{
try {
$metric->update(Binput::all());
$metric = $this->dispatch(new UpdateMetricCommand(
$metric,
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();
}

View File

@@ -13,9 +13,9 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand;
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;
@@ -48,7 +48,11 @@ class MetricPointController extends AbstractApiController
public function postMetricPoints(Metric $metric)
{
try {
$metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp')));
$metricPoint = $this->dispatch(new AddMetricPointCommand(
$metric,
Binput::get('value'),
Binput::get('timestamp'))
);
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -66,15 +70,12 @@ class MetricPointController extends AbstractApiController
*/
public function putMetricPoint(Metric $metric, MetricPoint $metricPoint)
{
$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');
}
$metricPoint->update($metricPointData);
$metricPoint = $this->dispatch(new UpdateMetricPointCommand(
$metricPoint,
$metric,
Binput::get('value'),
Binput::get('timestamp')
));
return $this->item($metricPoint);
}

View File

@@ -16,6 +16,7 @@ use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Tag;
@@ -114,11 +115,12 @@ class ComponentController extends Controller
*/
public function updateComponentAction(Component $component)
{
$_component = Binput::get('component');
$tags = array_pull($_component, 'tags');
$componentData = Binput::get('component');
$tags = array_pull($componentData, 'tags');
try {
$component->update($_component);
$componentData['component'] = $component;
$component = $this->dispatchFromArray(AddComponentCommand::class, $componentData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.edit', ['id' => $component->id])
->withInput(Binput::all())
@@ -159,12 +161,11 @@ class ComponentController extends Controller
*/
public function createComponentAction()
{
$_component = Binput::get('component');
// We deal with tags separately.
$tags = array_pull($_component, 'tags');
$componentData = Binput::get('component');
$tags = array_pull($componentData, 'tags');
try {
$component = $this->dispatchFromArray(AddComponentCommand::class, Binput::get('component'));
$component = $this->dispatchFromArray(AddComponentCommand::class, $componentData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.add')
->withInput(Binput::all())
@@ -271,18 +272,20 @@ class ComponentController extends Controller
*/
public function updateComponentGroupAction(ComponentGroup $group)
{
$groupData = Binput::get('group');
try {
$group->update($groupData);
$group = $this->dispatch(new UpdateComponentGroupCommand(
$group,
Binput::get('name'),
Binput::get('order', 0)
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.group.edit', ['id' => $group->id])
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
->withInput(Binput::all())
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.components.groups.edit.failure')))
->withErrors($e->getMessageBag());
}
return Redirect::route('dashboard.components.group.edit', ['id' => $group->id])
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.groups.edit.success')));
}
}

View File

@@ -14,7 +14,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
@@ -22,10 +22,8 @@ use CachetHQ\Cachet\Models\IncidentTemplate;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
class IncidentController extends Controller
{
@@ -111,10 +109,6 @@ class IncidentController extends Controller
*/
public function createIncidentAction()
{
if ($createdAt = Binput::get('created_at')) {
$incidentDate = Date::createFromFormat('d/m/Y H:i', $createdAt, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone'));
}
try {
$incident = $this->dispatch(new ReportIncidentCommand(
Binput::get('name'),
@@ -123,15 +117,9 @@ class IncidentController extends Controller
Binput::get('visible', true),
Binput::get('component_id'),
Binput::get('component_status'),
Binput::get('notify', true)
Binput::get('notify', true),
Binput::get('created_at')
));
if (isset($incidentDate)) {
$incident->update([
'created_at' => $incidentDate,
'updated_at' => $incidentDate,
]);
}
} catch (ValidationException $e) {
return Redirect::route('dashboard.incidents.add')
->withInput(Binput::all())
@@ -241,18 +229,18 @@ class IncidentController extends Controller
*/
public function editIncidentAction(Incident $incident)
{
$incidentData = Binput::get('incident');
if (array_has($incidentData, 'created_at') && $incidentData['created_at']) {
$incidentDate = Date::createFromFormat('d/m/Y H:i', $incidentData['created_at'], Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone'));
$incidentData['created_at'] = $incidentDate;
$incidentData['updated_at'] = $incidentDate;
} else {
unset($incidentData['created_at']);
}
try {
$incident->update($incidentData);
$incident = $this->dispatch(new UpdateIncidentCommand(
$incident,
Binput::get('name'),
Binput::get('status'),
Binput::get('message'),
Binput::get('visible', true),
Binput::get('component_id'),
Binput::get('component_status'),
Binput::get('notify', true),
Binput::get('created_at')
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.incidents.edit', ['id' => $incident->id])
->withInput(Binput::all())

View File

@@ -14,6 +14,7 @@ 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\Commands\Metric\UpdateMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use GrahamCampbell\Binput\Facades\Binput;
@@ -132,7 +133,16 @@ class MetricController extends Controller
public function editMetricAction(Metric $metric)
{
try {
$metric->update(Binput::get('metric', null, false));
$this->dispatch(new UpdateMetricCommand(
$metric,
Binput::get('metric.name', null, false),
Binput::get('metric.suffix', null, false),
Binput::get('metric.description', null, false),
Binput::get('metric.default_value', null, false),
Binput::get('metric.calc_type', null, false),
Binput::get('metric.display_chart', null, false),
Binput::get('metric.places', null, false)
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.metrics.edit', ['id' => $metric->id])
->withInput(Binput::all())