Added AddComponentGroupCommand

This commit is contained in:
James Brooks
2015-08-30 21:37:29 +01:00
parent 698b05980d
commit 6ee697cf7f
6 changed files with 103 additions and 7 deletions

View File

@@ -0,0 +1,53 @@
<?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;
class AddComponentGroupCommand
{
/**
* The component group name.
*
* @var string
*/
public $name;
/**
* The component group description.
*
* @var int
*/
public $order;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required|string',
'order' => 'integer',
];
/**
* Create a add component group command instance.
*
* @param string $name
* @param int $order
*
* @return void
*/
public function __construct($name, $order)
{
$this->name = $name;
$this->order = (int) $order;
}
}

View File

@@ -0,0 +1,38 @@
<?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\AddComponentGroupCommand;
use CachetHQ\Cachet\Events\ComponentGroup\ComponentGroupWasAddedEvent;
use CachetHQ\Cachet\Models\ComponentGroup;
class AddComponentGroupCommandHandler
{
/**
* Handle the add component group command.
*
* @param \CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand $command
*
* @return void
*/
public function handle(AddComponentGroupCommand $command)
{
$group = ComponentGroup::create([
'name' => $command->name,
'order' => $command->order,
]);
event(new ComponentGroupWasAddedEvent($group));
return $group;
}
}

View File

@@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup;
use Exception;
@@ -56,10 +57,11 @@ class ComponentGroupController extends AbstractApiController
*/
public function postGroups()
{
$groupData = array_filter(Binput::only(['name', 'order']));
try {
$group = ComponentGroup::create($groupData);
$group = $this->dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0)
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -81,7 +83,6 @@ class ComponentGroupController extends AbstractApiController
try {
$group->update($groupData);
} catch (Exception $e) {
dd($e->getMessage());
throw new BadRequestHttpException();
}

View File

@@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
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\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
@@ -246,7 +247,10 @@ class ComponentController extends Controller
public function postAddComponentGroup()
{
try {
$group = ComponentGroup::create(Binput::get('group'));
$group = $this->dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0)
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.add')
->withInput(Binput::all())

View File

@@ -42,7 +42,7 @@ class ComponentGroup extends Model
* @var string[]
*/
public $rules = [
'name' => 'required',
'name' => 'required|string',
'order' => 'integer',
];

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" required>
<input type="text" class="form-control" name="name" id="group-name" required>
</div>
</fieldset>