diff --git a/app/Commands/Component/AddComponentCommand.php b/app/Commands/Component/AddComponentCommand.php index 4952bf57..4f43e61e 100644 --- a/app/Commands/Component/AddComponentCommand.php +++ b/app/Commands/Component/AddComponentCommand.php @@ -61,9 +61,12 @@ class AddComponentCommand * @var string[] */ public $rules = [ - 'name' => 'required|string', - 'status' => 'required|integer', - 'link' => 'url', + 'name' => 'required|string', + 'description' => 'string', + 'status' => 'required|integer', + 'link' => 'url', + 'order' => 'integer', + 'group_id' => 'integer', ]; /** diff --git a/app/Commands/Component/UpdateComponentCommand.php b/app/Commands/Component/UpdateComponentCommand.php new file mode 100644 index 00000000..d34ee4d9 --- /dev/null +++ b/app/Commands/Component/UpdateComponentCommand.php @@ -0,0 +1,104 @@ + 'string', + 'description' => 'string', + 'status' => 'integer', + 'link' => 'url', + 'order' => 'integer', + 'group_id' => 'integer', + ]; + + /** + * Create a new update component command instance. + * + * @param \CachetHQ\Cachet\Models\Component $component + * @param string $name + * @param string $description + * @param int $status + * @param string $link + * @param int $order + * @param int $group_id + * + * @return void + */ + public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id) + { + $this->component = $component; + $this->name = $name; + $this->description = $description; + $this->status = (int) $status; + $this->link = $link; + $this->order = $order; + $this->group_id = $group_id; + } +} diff --git a/app/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Commands/ComponentGroup/UpdateComponentGroupCommand.php new file mode 100644 index 00000000..5f81c7ad --- /dev/null +++ b/app/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -0,0 +1,64 @@ + 'string', + 'order' => 'integer', + ]; + + /** + * Create a add component group command instance. + * + * @param \CachetHQ\Cachet\Models\ComponentGroup $group + * @param string $name + * @param int $order + * + * @return void + */ + public function __construct(ComponentGroup $group, $name, $order) + { + $this->group = $group; + $this->name = $name; + $this->order = (int) $order; + } +} diff --git a/app/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php index 073e183a..988eb653 100644 --- a/app/Commands/Incident/ReportIncidentCommand.php +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -62,20 +62,44 @@ class ReportIncidentCommand */ public $notify; + /** + * The date at which the incident occurred. + * + * @var string|null + */ + public $incident_date; + + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'name' => 'required|string', + 'status' => 'required|integer', + 'message' => 'string', + 'visible' => 'boolean', + 'component_id' => 'integer', + 'component_status' => 'integer', + 'notify' => 'boolean', + 'incident_date' => 'string', + ]; + /** * Create a new report incident command instance. * - * @param string $name - * @param int $status - * @param string $message - * @param int $visible - * @param int $component_id - * @param int $component_status - * @param bool $notify + * @param string $name + * @param int $status + * @param string $message + * @param int $visible + * @param int $component_id + * @param int $component_status + * @param bool $notify + * @param string|null $incident_date * * @return void */ - public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify) + public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $incident_date) { $this->name = $name; $this->status = $status; @@ -84,5 +108,6 @@ class ReportIncidentCommand $this->component_id = $component_id; $this->component_status = $component_status; $this->notify = $notify; + $this->incident_date = $incident_date; } } diff --git a/app/Commands/Incident/ReportMaintenanceCommand.php b/app/Commands/Incident/ReportMaintenanceCommand.php index 82105055..2f0a6405 100644 --- a/app/Commands/Incident/ReportMaintenanceCommand.php +++ b/app/Commands/Incident/ReportMaintenanceCommand.php @@ -41,6 +41,18 @@ class ReportMaintenanceCommand */ public $timestamp; + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'name' => 'required|string', + 'message' => 'string', + 'notify' => 'boolean', + 'timestamp' => 'string', + ]; + /** * Create a new report maintenance command instance. * diff --git a/app/Commands/Incident/UpdateIncidentCommand.php b/app/Commands/Incident/UpdateIncidentCommand.php new file mode 100644 index 00000000..4e9c493b --- /dev/null +++ b/app/Commands/Incident/UpdateIncidentCommand.php @@ -0,0 +1,123 @@ + 'string', + 'status' => 'integer', + 'message' => 'string', + 'visible' => 'boolean', + 'component_id' => 'integer', + 'component_status' => 'integer', + 'notify' => 'boolean', + ]; + + /** + * Create a new update incident command instance. + * + * @param \CachetHQ\Cachet\Models\Incident $name + * @param string $name + * @param int $status + * @param string $message + * @param int $visible + * @param int $component_id + * @param int $component_status + * @param bool $notify + * @param string|null $incident_date + * + * @return void + */ + public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $incident_date = null) + { + $this->incident = $incident; + $this->name = $name; + $this->status = $status; + $this->message = $message; + $this->visible = $visible; + $this->component_id = $component_id; + $this->component_status = $component_status; + $this->notify = $notify; + $this->incident_date = $incident_date; + } +} diff --git a/app/Commands/Metric/AddMetricCommand.php b/app/Commands/Metric/AddMetricCommand.php index b65072ce..e67df2d8 100644 --- a/app/Commands/Metric/AddMetricCommand.php +++ b/app/Commands/Metric/AddMetricCommand.php @@ -68,11 +68,14 @@ class AddMetricCommand * @var string[] */ public $rules = [ - 'name' => 'required', - 'suffix' => 'required', + 'name' => 'required|string', + 'suffix' => 'required|string', + 'description' => 'string', 'display_chart' => 'boolean', - 'default_value' => 'numeric', - 'places' => 'numeric|min:0|max:4', + 'default_value' => 'integer', + 'calc_type' => 'integer', + 'display_chart' => 'integer', + 'places' => 'integer|between:0,4', ]; /** diff --git a/app/Commands/Metric/AddMetricPointCommand.php b/app/Commands/Metric/AddMetricPointCommand.php index e0b676b8..9ecdad1a 100644 --- a/app/Commands/Metric/AddMetricPointCommand.php +++ b/app/Commands/Metric/AddMetricPointCommand.php @@ -34,21 +34,31 @@ class AddMetricPointCommand * * @var string */ - public $createdAt; + public $created_at; + + /** + * The validation rules. + * + * @var string[] + */ + public $rules = [ + 'value' => 'integer', + 'created_at' => 'string', + ]; /** * Create a new add metric point command instance. * * @param \CachetHQ\Cachet\Models\Metric $metric * @param int $value - * @param string $createdAt + * @param string $created_at * * @return void */ - public function __construct(Metric $metric, $value, $createdAt) + public function __construct(Metric $metric, $value, $created_at) { $this->metric = $metric; $this->value = $value; - $this->createdAt = $createdAt; + $this->created_at = $created_at; } } diff --git a/app/Commands/Metric/UpdateMetricCommand.php b/app/Commands/Metric/UpdateMetricCommand.php new file mode 100644 index 00000000..a858b055 --- /dev/null +++ b/app/Commands/Metric/UpdateMetricCommand.php @@ -0,0 +1,115 @@ + 'string', + 'suffix' => 'string', + 'description' => 'string', + 'display_chart' => 'boolean', + 'default_value' => 'numeric', + 'calc_type' => 'integer|in:0,1', + 'display_chart' => 'integer', + 'places' => 'numeric|min:0|max:4', + ]; + + /** + * Create a new update metric command instance. + * + * @param \CachetHQ\Cachet\Models\Metric $metric + * @param string $name + * @param string $suffix + * @param string $description + * @param float $default_value + * @param int $calc_type + * @param int $display_chart + * @param int $places + * + * @return void + */ + public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places) + { + $this->metric = $metric; + $this->name = $name; + $this->suffix = $suffix; + $this->description = $description; + $this->default_value = $default_value; + $this->calc_type = $calc_type; + $this->display_chart = $display_chart; + $this->places = $places; + } +} diff --git a/app/Commands/Metric/UpdateMetricPointCommand.php b/app/Commands/Metric/UpdateMetricPointCommand.php new file mode 100644 index 00000000..420a41e4 --- /dev/null +++ b/app/Commands/Metric/UpdateMetricPointCommand.php @@ -0,0 +1,74 @@ + 'integer', + 'created_at' => 'string', + ]; + + /** + * Create a new update metric point command instance. + * + * @param \CachetHQ\Cachet\Models\MetricPoint $point + * @param \CachetHQ\Cachet\Models\Metric $metric + * @param int $value + * @param string $created_at + * + * @return void + */ + public function __construct(MetricPoint $point, Metric $metric, $value, $created_at) + { + $this->point = $point; + $this->metric = $metric; + $this->value = $value; + $this->created_at = $created_at; + } +} diff --git a/app/Events/Component/ComponentWasUpdatedEvent.php b/app/Events/Component/ComponentWasUpdatedEvent.php new file mode 100644 index 00000000..7e776f09 --- /dev/null +++ b/app/Events/Component/ComponentWasUpdatedEvent.php @@ -0,0 +1,34 @@ +component = $component; + } +} diff --git a/app/Events/ComponentGroup/ComponentGroupWasUpdatedEvent.php b/app/Events/ComponentGroup/ComponentGroupWasUpdatedEvent.php new file mode 100644 index 00000000..400444d9 --- /dev/null +++ b/app/Events/ComponentGroup/ComponentGroupWasUpdatedEvent.php @@ -0,0 +1,34 @@ +group = $group; + } +} diff --git a/app/Events/Incident/IncidentWasUpdatedEvent.php b/app/Events/Incident/IncidentWasUpdatedEvent.php new file mode 100644 index 00000000..7979e62b --- /dev/null +++ b/app/Events/Incident/IncidentWasUpdatedEvent.php @@ -0,0 +1,34 @@ +incident = $incident; + } +} diff --git a/app/Events/Metric/MetricPointWasUpdatedEvent.php b/app/Events/Metric/MetricPointWasUpdatedEvent.php new file mode 100644 index 00000000..a1275d66 --- /dev/null +++ b/app/Events/Metric/MetricPointWasUpdatedEvent.php @@ -0,0 +1,34 @@ +point = $point; + } +} diff --git a/app/Events/Metric/MetricWasUpdatedEvent.php b/app/Events/Metric/MetricWasUpdatedEvent.php new file mode 100644 index 00000000..0cdcf0de --- /dev/null +++ b/app/Events/Metric/MetricWasUpdatedEvent.php @@ -0,0 +1,34 @@ +metric = $metric; + } +} diff --git a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php new file mode 100644 index 00000000..b639b864 --- /dev/null +++ b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -0,0 +1,55 @@ +component; + $component->update($this->filterComponentData($command)); + + event(new ComponentWasUpdatedEvent($component)); + + return $component; + } + + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Component\UpdateComponentCommand $command + * + * @return array + */ + protected function filterComponentData($command) + { + return array_filter([ + 'name' => $command->name, + 'description' => $command->description, + 'link' => $command->link, + 'status' => $command->status, + 'order' => $command->order, + 'group_id' => $command->group_id, + ]); + } +} diff --git a/app/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php new file mode 100644 index 00000000..579a3e43 --- /dev/null +++ b/app/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -0,0 +1,38 @@ +group; + $group->update([ + 'name' => $command->name, + 'order' => $command->order, + ]); + + event(new ComponentGroupWasUpdatedEvent($group)); + + return $group; + } +} diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index 99f4e826..3d3d37e0 100644 --- a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -13,8 +13,11 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; +use Illuminate\Support\Facades\Config; +use Jenssegers\Date\Date; class ReportIncidentCommandHandler { @@ -35,6 +38,16 @@ class ReportIncidentCommandHandler 'component' => $command->component_id, ]); + // The incident occurred at a different time. + if ($command->incident_date) { + $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); + + $incident->update([ + 'created_at' => $incidentDate, + 'updated_at' => $incidentDate, + ]); + } + // Update the component. if ($command->component_id) { Component::find($command->component_id)->update([ diff --git a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php new file mode 100644 index 00000000..ef25992c --- /dev/null +++ b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php @@ -0,0 +1,80 @@ +incident; + $incident->update($this->filterIncidentData($command)); + + // The incident occurred at a different time. + if ($command->incident_date) { + $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); + + $incident->update([ + 'created_at' => $incidentDate, + 'updated_at' => $incidentDate, + ]); + } + + // Update the component. + if ($command->component_id) { + Component::find($command->component_id)->update([ + 'status' => $command->component_status, + ]); + } + + // Notify subscribers. + if ($command->notify) { + event(new IncidentWasUpdatedEvent($incident)); + } + + return $incident; + } + + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand $command + * + * @return array + */ + protected function filterIncidentData($command) + { + return array_filter([ + 'name' => $command->name, + 'status' => $command->status, + 'message' => $command->message, + 'visible' => $command->visible, + 'component_id' => $command->component_id, + 'component_status' => $command->component_status, + 'notify' => $command->notify, + ]); + } +} diff --git a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php index 0f6ea677..962b1623 100644 --- a/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php +++ b/app/Handlers/Commands/Metric/AddMetricPointCommandHandler.php @@ -28,7 +28,7 @@ class AddMetricPointCommandHandler public function handle(AddMetricPointCommand $command) { $metric = $command->metric; - $createdAt = $command->createdAt; + $createdAt = $command->created_at; $data = [ 'metric_id' => $metric->id, diff --git a/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php b/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php new file mode 100644 index 00000000..11d0ce56 --- /dev/null +++ b/app/Handlers/Commands/Metric/UpdateMetricCommandHandler.php @@ -0,0 +1,57 @@ +metric; + + $metric->update($this->filterMetricData($command)); + + event(new MetricWasUpdatedEvent($metric)); + + return $metric; + } + + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand $command + * + * @return array + */ + protected function filterMetricData($command) + { + return array_filter([ + 'name' => $command->name, + 'suffix' => $command->suffix, + 'description' => $command->description, + 'default_value' => $command->default_value, + 'calc_type' => $command->calc_type, + 'display_chart' => $command->display_chart, + 'places' => $command->places, + ]); + } +} diff --git a/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php new file mode 100644 index 00000000..823d254e --- /dev/null +++ b/app/Handlers/Commands/Metric/UpdateMetricPointCommandHandler.php @@ -0,0 +1,48 @@ +point; + $metric = $command->metric; + $createdAt = $command->created_at; + + $data = [ + 'metric_id' => $metric->id, + 'value' => $command->value, + ]; + + if ($createdAt) { + $data['created_at'] = Carbon::createFromFormat('U', $createdAt)->format('Y-m-d H:i:s'); + } + + $point->update($data); + + event(new MetricPointWasUpdatedEvent($point)); + + return $point; + } +} diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 9aad79a5..c15ae168 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -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(); } diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index dcf691a3..596b8785 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -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(); } diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 34576b07..b5bf8e77 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -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(); } diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index cbf5c467..8d0f4f17 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -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(); } diff --git a/app/Http/Controllers/Api/MetricPointController.php b/app/Http/Controllers/Api/MetricPointController.php index dfe7f225..1c615ab4 100644 --- a/app/Http/Controllers/Api/MetricPointController.php +++ b/app/Http/Controllers/Api/MetricPointController.php @@ -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); } diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 84df5ec1..60df5ae0 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -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'))); } } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 64406f0c..a54a8396 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -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()) diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index 4fe2790e..a00b2bf2 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -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()) diff --git a/app/Http/Routes/FeedRoutes.php b/app/Http/Routes/FeedRoutes.php index 64ac33ea..acb2cd69 100644 --- a/app/Http/Routes/FeedRoutes.php +++ b/app/Http/Routes/FeedRoutes.php @@ -30,7 +30,7 @@ class FeedRoutes // Prevent access until the app is setup. $router->group([ 'middleware' => 'app.hasSetting', - 'setting' => 'app_name' + 'setting' => 'app_name', ], function ($router) { $router->get('/atom/{component_group?}', [ 'as' => 'feed.atom', diff --git a/resources/views/dashboard/components/groups/edit.blade.php b/resources/views/dashboard/components/groups/edit.blade.php index 114ff5e3..3c128a44 100644 --- a/resources/views/dashboard/components/groups/edit.blade.php +++ b/resources/views/dashboard/components/groups/edit.blade.php @@ -19,7 +19,7 @@