refactor: move chat bot request to form request

This commit is contained in:
Roardom
2023-01-08 04:43:11 -06:00
parent d4b28c315c
commit f1d41ee5f0
3 changed files with 56 additions and 58 deletions
@@ -14,6 +14,7 @@
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\UpdateChatBotRequest;
use App\Models\Bot;
use Illuminate\Http\Request;
@@ -51,67 +52,12 @@ class ChatBotController extends Controller
/**
* Update the specified Bot resource in storage.
*/
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
public function update(UpdateChatBotRequest $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$bot = Bot::findOrFail($id);
if ($request->has('command') && $request->input('command') == $bot->command) {
$v = \validator($request->all(), [
'name' => 'required|min:3|max:255',
'command' => 'required|alpha_dash|min:3|max:255',
'position' => 'required',
'color' => 'required',
'icon' => 'required',
'emoji' => 'required',
'help' => 'sometimes|max:9999',
'info' => 'sometimes|max:9999',
'about' => 'sometimes|max:9999',
]);
} else {
$v = \validator($request->all(), [
'name' => 'required|min:3|max:255',
'command' => 'required|alpha_dash|min:3|max:255|unique:bots',
'position' => 'required',
'color' => 'required',
'icon' => 'required',
'emoji' => 'required',
'help' => 'sometimes|max:9999',
'info' => 'sometimes|max:9999',
'about' => 'sometimes|max:9999',
]);
}
$error = null;
$success = null;
$redirect = null;
if ($v->passes()) {
$bot->name = $request->input('name');
$bot->position = $request->input('position');
$bot->color = $request->input('color');
$bot->icon = $request->input('icon');
$bot->emoji = $request->input('emoji');
$bot->about = $request->input('about');
$bot->info = $request->input('info');
$bot->help = $request->input('help');
$bot->command = $request->input('command');
$bot->save();
$success = 'The Bot Has Been Updated';
}
if ($success === null) {
$error = 'Unable To Process Request';
if ($v->errors()) {
$error = $v->errors();
}
return \to_route('staff.bots.edit', ['id' => $id])
->withErrors($error);
}
Bot::where('id', '=', $id)->update($request->validated());
return \to_route('staff.bots.edit', ['id' => $id])
->withSuccess($success);
->withSuccess("The Bot Has Been Updated");
}
/**
@@ -0,0 +1,45 @@
<?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 UpdateChatBotRequest 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|min:3|max:255',
'command' => 'required|alpha_dash|min:3|max:255',
'position' => 'required',
'color' => 'required',
'icon' => 'required',
'emoji' => 'required',
'help' => 'sometimes|max:9999',
'info' => 'sometimes|max:9999',
'about' => 'sometimes|max:9999',
];
}
}
+7
View File
@@ -37,4 +37,11 @@ class Bot extends Model
protected $casts = [
'name' => 'string',
];
/**
* The attributes that aren't mass assignable.
*
* @var string[]
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
}