refactor: move internal requests to form requests

This commit is contained in:
Roardom
2023-01-08 04:47:55 -06:00
parent db323fcb24
commit a7b06b3e0b
4 changed files with 88 additions and 42 deletions

View File

@@ -14,8 +14,9 @@
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreInternalRequest;
use App\Http\Requests\Staff\UpdateInternalRequest;
use App\Models\Internal;
use Illuminate\Http\Request;
/**
* @see \Tests\Feature\Http\Controllers\Staff\GroupControllerTest
@@ -45,26 +46,9 @@ class InternalController extends Controller
/**
* Save a group change.
*/
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
public function update(UpdateInternalRequest $request, int $id): \Illuminate\Http\RedirectResponse
{
$internal = Internal::findOrFail($id);
$internal->name = $request->input('name');
$internal->icon = $request->input('icon');
$internal->effect = $request->input('effect');
$v = \validator($internal->toArray(), [
'name' => 'required',
'icon' => 'required',
'effect' => 'required',
]);
if ($v->fails()) {
return \to_route('staff.internals.index')
->withErrors($v->errors());
}
$internal->save();
Internal::where('id', '=', $id)->update($request->validated());
return \to_route('staff.internals.index')
->withSuccess('Internal Group Was Updated Successfully!');
@@ -81,25 +65,9 @@ class InternalController extends Controller
/**
* Store A New Internal Group.
*/
public function store(Request $request): \Illuminate\Http\RedirectResponse
public function store(StoreInternalRequest $request): \Illuminate\Http\RedirectResponse
{
$internal = new Internal();
$internal->name = $request->input('name');
$internal->icon = $request->input('icon');
$internal->effect = $request->input('effect');
$v = \validator($internal->toArray(), [
'name' => 'required|unique:internals',
'icon',
'effect',
]);
if ($v->fails()) {
return \to_route('staff.internals.index')
->withErrors($v->errors());
}
$internal->save();
Internal::create($request->validated());
return \to_route('staff.internals.index')
->withSuccess('New Internal Group added!');

View File

@@ -0,0 +1,39 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Requests\Staff;
use Illuminate\Foundation\Http\FormRequest;
class StoreInternalRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required',
'icon' => 'required',
'effect' => 'required',
];
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author Roardom <roardom@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Requests\Staff;
use Illuminate\Foundation\Http\FormRequest;
class UpdateInternalRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name' => 'required',
'icon' => 'required',
'effect' => 'required',
];
}
}

View File

@@ -23,11 +23,11 @@ class Internal extends Model
use Auditable;
/**
* The Attributes That Aren't Mass Assignable.
*
* @var array
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = ['id'];
protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* Indicates If The Model Should Be Timestamped.