Added UpdateComponentGroupCommand

This commit is contained in:
James Brooks
2015-09-18 15:26:34 +01:00
parent d3cd8201a6
commit d4c1b23b3f
6 changed files with 152 additions and 7 deletions

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Commands\ComponentGroup;
use CachetHQ\Cachet\Models\ComponentGroup;
class UpdateComponentGroupCommand
{
/**
* The component group.
*
* @var \CachetHQ\Cachet\Models\ComponentGroup
*/
public $group;
/**
* The component group name.
*
* @var string
*/
public $name;
/**
* The component group description.
*
* @var int
*/
public $order;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => '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;
}
}

View File

@@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events\ComponentGroup;
use CachetHQ\Cachet\Models\ComponentGroup;
class ComponentGroupWasUpdatedEvent
{
/**
* The component group that was updated.
*
* @var \CachetHQ\Cachet\Models\ComponentGroup
*/
public $group;
/**
* Create a new component group was updated event instance.
*
* @return void
*/
public function __construct(ComponentGroup $group)
{
$this->group = $group;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Commands\ComponentGroup;
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Events\ComponentGroup\ComponentGroupWasUpdatedEvent;
use CachetHQ\Cachet\Models\ComponentGroup;
class UpdateComponentGroupCommandHandler
{
/**
* Handle the update component group command.
*
* @param \CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand $command
*
* @return \CachetHQ\Cachet\Models\ComponentGroup
*/
public function handle(UpdateComponentGroupCommand $command)
{
$group = $command->group;
$group->update([
'name' => $command->name,
'order' => $command->order,
]);
event(new ComponentGroupWasUpdatedEvent($group));
return $group;
}
}

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

@@ -17,6 +17,7 @@ use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
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;
@@ -272,18 +273,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

@@ -19,7 +19,7 @@
<fieldset>
<div class="form-group">
<label for="incident-name">{{ trans('forms.components.groups.name') }}</label>
<input type="text" class="form-control" name="group[name]" id="group-name" value="{{ $group->name }}" required>
<input type="text" class="form-control" name="name" id="group-name" value="{{ $group->name }}" required>
</div>
</fieldset>